Search code examples
pythonpycharm

Python code runs fine in pycharm but gives module error while running in terminal


I have my flask project project structure as below which I have deployed to ec2 .

myproject 
 --src 
     -- cache  
             --- cache.py
     -- storeservice 
            --- upload.py
           
     -- esService 
            --- es.py
     -- ui
           --- html
           --- static 
    -- main
          --- controller.py 

In Controller I have the import

from storeservice.upload import Upload 

This works fine on pycharm but when I run via terminal it gives error

command which I use is python3 controller.py

from storeservice.upload import Upload
ModuleNotFoundError: No module named 'storeservice'

Is there a way to execute it without making any code change ?


Solution

  • you need to add the folder /....../myproject/src to your PYTHONPATH, pycharm does that by default, you need to do that manually outside of pycharm.

    one of the most portable ways to do that is to create a shell script that modifies your environment with the correct path then executes the python script, you then call that one instead of calling python directly.