应用程序在PyCharm中运行,但在命令行中不运行(ModuleNotFoundError)。

huangapple go评论49阅读模式
英文:

application runs from pycharm but not from command line (ModuleNotfounderror)

问题

以下是翻译好的部分:

我有一个应用程序,其文件夹树如下:

MyApp
  |
  |-- Code
  |     |
  |     |-- main.py
  |     |-- settings.py
  |     |
  |-- vev

venv - 是该项目的虚拟环境。

我可以在PyCharm中运行main.py而不会出现任何问题。

我尝试从命令行运行它:

  1. 我激活了venv

  2. 我从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:

  1. I activate the venv

  2. 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

huangapple
  • 本文由 发表于 2023年7月23日 14:25:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76746885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定