9

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!

Karnivaurus
  • 1,024

3 Answers3

11

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.

Javier Rivera
  • 35,434
5

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.

0

Open the following shortcut (Ctrl + Alt + S), and click (Project: xxxx), now choose (Project Structure) and select (/home/yourUser/PycharmProjects/XXXX/venv/bin) and right-click (Excluded) or (Alt + E).

Now click (+ Add Content Root) and see if it points to the following location (/home/ yourUser/PycharmProjects/XXXXX/venv/bin) and click OK. Now close the IDE and open it again.

damadam
  • 2,873