英文:
application runs from pycharm but not from command line (ModuleNotfounderror)
问题
以下是翻译好的部分:
我有一个应用程序,其文件夹树如下:
MyApp
|
|-- Code
| |
| |-- main.py
| |-- settings.py
| |
|-- vev
venv
- 是该项目的虚拟环境。
我可以在PyCharm中运行main.py而不会出现任何问题。
我尝试从命令行运行它:
-
我激活了
venv
-
我从
MyApp
文件夹运行了以下命令:python Code\main.py
然后我遇到了以下错误:
ModuleNotFoundError: No module named 'Code';
我如何能够从命令行运行main.py
(如果可能的话,不需要更改任何代码)?
英文:
I have app with the following folder tree:
MyApp
|
|-- Code
| |
| |-- main.py
| |-- settings.py
| |
|-- vev
venv
- is the virtual environment of that project.
I can run main.py from pycharm without any problem.
I'm trying to run it from command line:
-
I activate the
venv
-
I run the following command from
MyApp
folder:python Code\main.py
and I'm getting the following error:
ModuleNotFoundError: No module named 'Code'
How can I run main.py
from command line (without any code change if it possible)
答案1
得分: 1
因为PyCharm通常会为您设置PYTHONPATH,所以会出现这种情况。
要手动执行您的代码,您需要设置新的PYTHONPATH变量。
在Linux/mac上,应该是:
export PYTHONPATH=$(pwd)
然后您应该能够运行您的代码。
英文:
It happens because pycharm usually sets PYTHONPATH for you.
To manually execute your code you need to set new PYTHONPATH variable.
On linux/mac it should be:
export PYTHONPATH=$(pwd)
And after this you should be able to run your code
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论