14

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?

user9064103
  • 193
  • 1
  • 3
  • 12
  • 1
    One possible explanation is that you are running different versions of python in terminal and in pycharm. – AlanSTACK Feb 01 '18 at 03:05
  • 1
    do you have __init__.py in your module? can you post your directory structure here? – labheshr Feb 01 '18 at 03:05
  • Both are running the same version of python. I have `init.py` in the module, and i'll post the directory structure in a bit. – user9064103 Feb 01 '18 at 03:18
  • See my answer here: https://stackoverflow.com/questions/48532134/python-code-runs-from-ide-but-not-from-terminal/48547472#48547472 – r.ook Feb 01 '18 at 04:15

3 Answers3

5

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

John Mee
  • 50,179
  • 34
  • 152
  • 186
3

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.

tomerpacific
  • 4,704
  • 13
  • 34
  • 52
2

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\\')
adir abargil
  • 5,495
  • 3
  • 19
  • 29