Import statement works on PyCharm but not from terminal

Posted on

Question :

Import statement works on PyCharm but not from terminal

Pycharm 2016.2.3, Mac OS X 10.11.1, Python 3.5 (Homebrew);

I have this folder structure


So my expected output is


This works fine when run from PyCharm. However, when I run it from my terminal I get an ImportError:

$ pwd
$ /home/project (not the actual path; just omitting some personal stuff)
$ python3.5 somepackage/
File "", line 1, in <module>
ImportError: No module named 'somepackage'

I have found this question, which is about the same problem. However, none of the suggested solutions work for me, as I am indeed using the same Python interpreter as PyCharm does and I am currently in the folder that contains the somepackage/ folder.

Does anyone have any other suggestions about how to solve this issue? Thank you kindly!

Asked By: Pibborn


Answer #1:

You are running like a script, but you are really using it like a module. So the proper solution is to run it as a module:

python3 -m

For the record, another alternative is to edit your path like:


(Or you could put the absolute directory in there, and of course you should append any other directories that are already in your PYTHONPATH.) This is closer to what PyCharm does, but is less philosophically correct.

Answered By: Arthur Tacca

Answer #2:

i solved my problem by two steps on Linux:

  1. first step
    go to the root directory of your project and set:
  1. second step
    run python3 -m
    remember Without ‘.py’ suffix
Answered By: Oscar

Answer #3:

Setting PYTHONPATH is what makes it work, as noted above. I use the following VSCODE .env content so that it works for any project:


This is essentially what PyCharm does when you check “Add Content Roots to PYTHONPATH” in your run/debug configuration. It’s a helpful setting, but it spoils you because your code fails outside PyCharm.

Or, if you run in terminal, first export:

export PYTHONPATH=...

Took me days to work all this out.

Answered By: val

Answer #4:

I just had the same problem using scapy.layers.http Module,
this problem occurred on my Kali (linux-Debian) but run fine on Win-10 (after few modifications.)

packet was installed (scapy-http) correctly and the program was running in PyCharm but not as script (from terminal)

I tried solving it with reinstalling in main root, and messing with the sys.path but None have worked.

Troubleshoot & Solution

  1. I found that it looks for the http module in:


and got the ImportError:

from scapy.layers import http --->
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'http' from 'scapy.layers' (/usr/local/lib/python3.7/dist-packages/scapy/layers/

  1. So then I checked where scapy-http is really installed, which is module called, so I just copied it to:

      found the file in :/usr/local/lib/python3.7/dist-packages/scapy_http/

And that did it :)!

I know its a bit glitchie but that worked!

Answered By: Itamar Zalisher

Leave a Reply

Your email address will not be published. Required fields are marked *