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?

Thank you!


Solution 1:

As ByteCommander said in a comment, PyCharm doesn't use bashrc, so it doesn't know where your library is.

In the same screen where you added the interpreter you can see a wheel icon, click it, it will show you a menu, click on more. You should see a screen like this:

PyCharm Interpreter configuration

You should select your interpreter and click on the last button. This should open this window:

Interpreter paths configuration

Now clicking on the plus icon you should be able to add your own paths for libraries.

Solution 2:

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.