46
loading...
This website collects cookies to deliver better user experience
init-hook
을 설정해주면 되는데 이게 뭘까 찾아보았다. 구글링을 해보는데 Import error에 대한 해결 방법으로 init-hook
을 설정해주면 된다라는 내용은 많이 있었는데 구체적인 내용은 찾기가 힘들었다. init-hook
은 initialization hooks를 의미하고 pylint가 초기실행(initialize)될 때 파이썬 코드를 실행시켜주는 옵션이다. 주로 sys.path
를 설정하는데 사용된다. 이번 문제도 path와 관련된 문제로 pylint에게 모듈이 있는 path를 알려주면 pylint가 모듈을 찾을 수 있게 되고 import
를 정상적으로 할 수 있게 되는 것이다.init-hook
설정은 .vscode 폴더 안에 settings.json 에 다음과 같은 설정을 추가해 주면 된다."python.linting.pylintArgs": [
"--init-hook",
"import sys; sys.path.append('path to folder your module is in')"
]
append()
안에 자신의 모듈이 있는 디렉터리를 적어주면 끝!import foo
를 하게 되면 이게 어떤 foo를 말하는 것인지 알기 어려워지는 상황이 되었기 때문이다. As Python's library expands, more and more existing package internal modules suddenly shadow standard library modules by accident. It's a particularly difficult problem inside packages because there's no way to specify which module is meant.
import
하라고 명시하고 있다.Do not use relative names in imports. Even if the module is in the same package, use the full package name. This helps prevent unintentionally importing a package twice.
from 패키지 import 모듈
python 패키지/모듈.py
방식으로 파이썬을 실행하면 ModuleNotFoundError: No module named '모듈'
이라는 에러 메세지를 받게 될것이다. python 실행시 패키지 디렉터리를 sys.path 제일 첫번째에 (sys.path[0]
) 지정하게 되기 때문에 이미 그 패키지 디렉터리 안에 들어와 있는 상황이니 해당 패키지를 찾을 수 없는 것이다. 따라서 파이썬 실행을 python -m 패키지.모듈
방식으로 해주어야 한다.