Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing a Python module works from command line, but not from PyCharm

My default Python binary is set to the one with the Anaconda distribution of Python. This is found at /home/karnivaurus/anaconda/bin/python, and I have made this the default by adding to my .bashrc file the following: export PATH=/home/karnivaurus/anaconda/bin:$PATH.

I also have a Python package called caffe, which is located at /home/karnivaurus/caffe/distribute/python, and I have added this to the package search path by adding to my .bashrc file the following: export PYTHONPATH=${PYTHONPATH}:/home/karnivaurus/caffe/distribute/python.

Now, I have a simple Python file, called test.py, with the following contents:

import caffe
print "Done."

If I run this by entering python test.py into the terminal, it runs fine, printing out "Done.". The problem I am having is when I run this in the PyCharm IDE. In PyCharm, I have set the interpreter to be /home/karnivaurus/anaconda/bin/python. But when I open test.py in PyCharm, and run the file in the IDE, I get the following error:

ImportError: No module named caffe

So my question is: Why can PyCharm not find the caffe module when it runs the Python script, but it can be found when I run the script from the terminal?

like image 586
Karnivaurus Avatar asked Oct 12 '15 12:10

Karnivaurus


People also ask

Why import is not working in PyCharm?

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.

How do I import a module into PyCharm?

Type the name of the package and hit Alt-Enter , then choose Install and Import package . PyCharm will do both: you'll see a notification during the installation, then the import will be generated in the right way, according to your project styles.

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.


2 Answers

There are a few things that can cause this. To debug, please modify your test.py like so:

# Is it the same python interpreter? 
import sys
print(sys.executable)

# Is it the same working directory? 
import os
print(os.getcwd())

# Are there any discrepancies in sys.path? 
# this is the list python searches, sequentially, for import locations
# some environment variables can fcuk with this list
print(sys.path)

import caffe
print "Done."

Try again in both situations to find the discrepancy in the runtime environment.


edit: there was a discrepancy in sys.path caused by PYTHONPATH environment variable. This was set in the shell via .bashrc file, but not set in PyCharm's runtime environment configuration.

like image 152
wim Avatar answered Sep 30 '22 17:09

wim


For an additional option, you can use pycharm by terminal. And export the corresponding environment paths beforehand. This works for me. And I think it's better than make some changes in the code. You gonna need run the code by terminal after your debugging.

For example, in terminal type:

$ export LD_LIBRARY_PATH=~/build_master_release/lib:/usr/local/cudnn/v5/lib64:~/anaconda2/lib:$LD_LIBRARY_PATH
$ export PYTHONPATH=~/build_master_release/python:$PYTHONPATH

Then run pycharm by charm (pycharm can be soft linked by charm bash):

$ charm
like image 44
Moonlight Knight Avatar answered Sep 30 '22 17:09

Moonlight Knight