Import fails in terminal but works in PyCharm

隐身守侯 提交于 2020-08-09 10:15:59

问题


I'm using PyCharm for a project with the following file hierarchy:

And I'm running main.py in PyCharm with the following configurations:

Working Directory: /Users/me/longpath/project/amlproject/pca_mixtures.

When I try to run in terminal, it fails:

~/longpath/project/amlproject/pca_mixtures$ python main.py
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    from pca_mixtures.funcs import PCAMixture
ModuleNotFoundError: No module named 'pca_mixtures'

and nothing changes if I jump up to the parent folder:

~/longpath/project/amlproject$ python pca_mixtures/main.py 
Traceback (most recent call last):
  File "pca_mixtures/main.py", line 2, in <module>
    from pca_mixtures.funcs import PCAMixture
ModuleNotFoundError: No module named 'pca_mixtures'

The reason for using from pca_mixtures.funcs import PCAMixture instead of just from funcs import PCAMixture was so that PyCharm would recognize the import and not red-underline it, as I've described here. Now, it seems that this has lead to me not being able to run the project in the terminal.

How would you handle this? I want to be able to run it in the terminal because the PyCharm output is not fully sequential (error messages output before program output) which is annoying when debugging.

来源:https://stackoverflow.com/questions/54110329/import-fails-in-terminal-but-works-in-pycharm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!