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

My default binary for Python is set to the one that has the Anaconda Python distribution. It can be found in /home/karnivaurus/anaconda/bin/python, and I did it by default, added to my file is .bashrcthe following: export PATH=/home/karnivaurus/anaconda/bin:$PATH.

I also have a Python package called caffe, which is located in /home/karnivaurus/caffe/distribute/python, and I said it in the search path of the package, adding to my file is .bashrcthe following: export PYTHONPATH=${PYTHONPATH}:/home/karnivaurus/caffe/distribute/python.

Now I have a simple Python file called test.pywith the following contents:

import caffe
print "Done."

If I run this by typing python test.pyinto the terminal, it works fine by printing "Finish." The problem I am facing is that I am running it in PyCharm. In PyCharm, I installed the interpreter as /home/karnivaurus/anaconda/bin/python. But when I open test.pyin PyCharm and run the file in the IDE, I get the following error:

ImportError: No module named caffe

So my question is: why PyCharm cannot find the module caffewhen running the Python script, but can it be found when the script is run from the terminal?

Thanks!

+4
source share
2 answers

There are several things that can cause this. To debug, please change your test.pylike this:

# 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."

, .


edit: sys.path, PYTHONPATH. .bashrc, PyCharm.

+2

pycharm . . . , , . .

, :

$ 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

pycharm (pycharm , bash):

$ charm
0

Source: https://habr.com/ru/post/1611278/


All Articles