6

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?

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that helped. It turns out that when I run this in the terminal, the directory /home/karnivaurus/caffe/distribute/python is on the path, but not when run in PyCharm. So it seems that PyCharm does not use the PYTHONPATH environment variable, but instead uses its own method to search for modules...
There is a dialog box in pycharm runtime configuration where you can set the PYTHONPATH environment variable. It will search it if you add it there. There is no reason for pycharm to use the environment variable you have modified in your .bashrc, because pycharm doesn't use bash.
1

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

Comments

0

Well this may be a redundant answer, however I think it's important to explicitly called out what causes this error. It happened to me many times and I got it fixed by making sure that IDE ( pycharm or vscode or any other) is set to same working directory where the code resided.

for example : I have two files train.py and config.py in mlproject/src directory. I'm trying to run import config in train.py

**When run in /mlproject/ directory, I get error when try to import config **

(ml) dude@vscode101:~/mlproject$ python
Python 3.7.6 (default, Jan  8 2020, 19:59:22) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import config
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'config'
>>> 

When run in /mlproject/src/` directory, I'm able to successfully import config

(ml) dude@vscode101:~/mlproject/src$ python
Python 3.7.6 (default, Jan  8 2020, 19:59:22) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import config
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.