Search code examples
pythonpycharm

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


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)


Solution

  • 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