Import statement runs well in Pycharm, but not in terminal

Viewed 20

Here is the structure of my project

project
  /package_one
    /dir_one
     __init__.py   
     Class_one.py
   __init__.py
   main.py
   /Frontend
     ...
   /Backend
     ...(need to import from package_one)

  

I need to import package_one as a library to my backend, and also I need it to run separately with main.py

In my /project/package_one/__init__.py

from package_one.dir_one import Class_one

In my proejct/package_one/main.py I tried both

from package_one.dir_one import Class_one

and

from dir_one import Class_one

Both methods above work well in Pycharm, but when I run it in the terminal with python3 -m main.py or python3 main.py It prompts an error ModuleNotFOundError: No Module named 'package_one


I tried to change the working directory by adding sys.chdir('..') and change the export PYTHONPATH=[path_to_project] but doesn't work.

0 Answers
Related