나는 이것을 절대 바보라고 느낄 것이다. 금요일 늦었 기 때문인지 모르지만 나는 왜 문제가 있는지 이해하지 못하고있다. 이 놀라운 코드로ImportError : No module
디렉토리 구조 :
~/testapp/
~/testapp/__init__.py
~/testapp/settings.py
~/testapp/workers/a.py
~/testapp를/settings.py :
x = 1
~/testapp를/노동자/a.py :
from settings import x
실행 ~/testapp/workers/a.py PyCharm을 통해, 그것은 잘 실행됩니다.
[email protected]:~$ python testapp/workers/a.py
Traceback (most recent call last):
File "testapp/workers/a.py", line 1, in <module>
from settings import x
ImportError: No module named settings
내가 또한 ~/testapp를/노동자/a.py에 다음과 같은 시도 :
from testapp.settings import x
과 같은 오류가 발생했습니다 터미널에서 실행하는 경우 그러나, 나는 얻을.
from ..settings import x
하지만이 반환합니다 : 나는 또한 시도했다
[email protected]:~/$ python testapp/workers/a.py
Traceback (most recent call last):
File "python testapp/workers/a.py", line 1, in <module>
from ..settings import testvar
ValueError: Attempted relative import in non-package
내가이 같은 수입 방법을 사용 이전에 응용 프로그램을 많이 실행했으며, 문제가 생긴 적이 없다. 왜 갑자기 이런 일이 일어나는 지 모르겠습니다.
stackoverflow 및 google에서 이와 같은 다른 문제를 볼 때 모든 사람이 설정 및 시스템 경로 확인을 언급합니다. 언급했듯이 PyCharm을 통해이를 실행하면 문제가 없습니다. 가져 오기 전에 ~/testapp/workers/a.py를 변경하여 sys.path, sys.executable 및 os.getcwd()를 인쇄하면 PyCharm과 콘솔의 결과가 동일합니다.