Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - "No module named my_module" in terminal, but not in PyCharm

Tags:

python

pycharm

I have a python script that i'm trying to run. When i run it from within PyCharm it runs without a problem, but when i run it through the terminal using:

python my_script.py

i get:

Traceback (most recent call last):
  File "folder/folder/my_script.py", line 4, in <module>
    from my_module import me1, me2, me3
ImportError: No module named my_module

What could be the problem?

like image 752
user9064103 Avatar asked Feb 01 '18 03:02

user9064103


3 Answers

adding the following code to the head of the script did the trick for me:

import sys
sys.path.append('C:\\path\to\\my\\awesome\\project\\')
like image 152
adir abargil Avatar answered Nov 19 '22 06:11

adir abargil


The PYTHONPATH in your terminal environment doesn't contain 'my_module'.

Configure the PYTHONPATH to include the directory containing your module

It works in pycharm because it sets up the path for you automagically.

Learn about the module search path

like image 4
John Mee Avatar answered Nov 19 '22 06:11

John Mee


Another reason might be you had started your project in pycharm with virtual enviroment.

If it is the case go to your projects venv\Scripts folder via your terminal and run activate.bat .

You will see "(venv)" on leftmost in your terminal line. Then go to your project folder and run

python my_script.py

Now it should work.

like image 3
Kerem Yapar Avatar answered Nov 19 '22 04:11

Kerem Yapar