Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- review
- paper_review
- git
- computer
- language_model
- natural_language_processing
- error
- Ai
- computer_setting
- text
- machinelearning
- install
- Vim
- cs231n
- seq2seq
- nlp
- code
- Standford
- cs224n
- gensim
- terminal
- pip
- deeplearning
- linux
- slideshare
- Stanford
- github
- tab
- json
- Statistics
Archives
- Today
- Total
NLP/AI/Statistics
python에서 다른 위치의 파일 import하는 방법 본문
만약 다른 경로에 있는 모듈을 import할 경우엔 기존에 동일한 위치에 있을 때와 다르게 불러와야한다.
만약 원하는 모듈 (module_name)이 동일한 경로에 있을 경우엔, 아래와 같이 .을 사용해서 import 할 수 있다.
$ from . import module_name
현재 위치보다 하위 경로에 있을 경우엔, 아래와 같이 하위폴더명을 지정하여 import 할 수 있다.
$ from sub_folder import module_name
즉, sub_folder 안에 있는 module_name으로부터 import할 경우 위와 같이 import 하면 된다.
그리고 현재 위치보다 상위 경로에 있을 경우엔, sys와 os를 사용해서 상위 폴더의 위치를 현재와 동일하게 만든다.
$ import sys, os
sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
from parent_folder import module_name
상위 폴더 parent folder에 있는 module_name을 import할 수 있다.
가장 간단한 방법으로는 절대경로를 지정해주는 것이다.
$ import sys
sys.path.append(file_path)
from . import module_name
원하는 모듈 (module_name)이 위치한 경로 (file_path)를 지정하여 현재와 동일하게 만들어 import 할 수 있다.
'Computer Setting > Python & ETC' 카테고리의 다른 글
gensim 설치 에러 (0) | 2021.08.09 |
---|---|
python 지수 표현 변경 (0) | 2021.06.11 |
python json 파일 한글 깨짐 현상 (0) | 2021.06.09 |
python json 파일 저장 및 불러오기 (0) | 2021.06.09 |
docker 비밀번호 변경 (0) | 2020.10.22 |
Comments