Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

project   /somepackage     /subpackage      __init__.py         bar.py    __init__.py    foo.py 

foo.py:

import somepackage.subpackage.bar print("foo") 

bar.py:

print("bar") 

So my expected output is

bar foo 

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/foo.py File "foo.py", line 1, in <module> import somepackage.subpackage.bar 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?

like image 775
Pibborn Avatar asked Oct 28 '16 11:10

Pibborn


People also ask

Why is PyCharm not recognizing import?

Troubleshooting: Try installing/importing a package from the system terminal (outside of PyCharm) using the same interpreter/environment. In case you are using a virtualenv/conda environment as your Project Interpreter in PyCharm, it is enough to activate that environment in the system terminal and then do the test.

Why can't I import modules in Python?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

Why can't I run PyCharm programs in terminal?

Pycharm uses a virtual environment. When you try run your program in your terminal this enviroment isn't active. I too have had this issue - and the PYTHONPATH setting set by PyCharm did seem to be the issue.

How do I set pythonpath in PyCharm?

Outside of PyCharm, PYTHONPATH is not normally set. The first entry in sys.path refers to the current working directory where the script was run from. As long as you run your script with your terminal's working directory as the folder containing Dev, it should be able to find the Dev.test module, regardless of the extra entry added to sys.path.

Where does PyCharm run scripts from?

When running a script from within PyCharm, it runs it in an environment with PYTHONPATH set to the list of all the folders that are marked "Sources Root" (with a blue folder icon) in the project explorer. Outside of PyCharm, PYTHONPATH is not normally set.

Does PyCharm read bashrc?

Programs started from the Ubuntu launcher do not read .bashrc. As an alternative to setting the paths in PyCharm, you can simply start PyCharm from a Bash shell to give it access to the environment variables you set in .bashrc. Show activity on this post.


2 Answers

You are running foo.py 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 somepackage.foo 

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

export PYTHONPATH=. 

(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.

like image 88
Arthur Tacca Avatar answered Sep 23 '22 18:09

Arthur Tacca


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

PYTHONPATH=${PROJ_DIR}:${PYTHONPATH} 

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.

like image 44
val Avatar answered Sep 23 '22 18:09

val