Karnivaurus
Karnivaurus

Reputation: 24111

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?

Upvotes: 6

Views: 6563

Answers (3)

Nim J
Nim J

Reputation: 1033

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

Upvotes: 0

Moonlight Knight
Moonlight Knight

Reputation: 313

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

Upvotes: 1

wim
wim

Reputation: 362478

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.

Upvotes: 4

Related Questions