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."
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
**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 >>>
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.,You should select your interpreter and click on the last button. This should open this window:,Connect and share knowledge within a single location that is structured and easy to search.
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
When a Python module or package is imported, __name__ is set to the module’s name. Usually, this is the name of the Python file itself without the .py extension:,Some modules contain code that is intended for script use only, like parsing command-line arguments or fetching data from standard input. If a module like this was imported from a different module, for example to unit test it, the script code would unintentionally execute as well.,Note that importing __main__ doesn’t cause any issues with unintentionally running top-level code meant for script use which is put in the if __name__ == "__main__" block of the start module. Why does this work?,the Python module or package passed to the Python interpreter with the -m argument:
>>>
import configparser
>>>
configparser.__name__ 'configparser'
>>> from concurrent.futures
import process
>>>
process.__name__ 'concurrent.futures.process'
>>> __name__
'__main__'
$ python3 helloworld.py Hello, world!
$ python3 - m tarfile usage: tarfile.py[-h][-v](...)
$ echo "import this" | python3
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
...
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.,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?,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:,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
Last Updated : 14 Jul, 2022
Directory Structure
-Folder_1
-
main.py -
Folder_2 -
module1.py
Suppose we have a directory structure like this:
-project
-
Folder_1 -
main.py -
Folder_2 -
subfolder -
new.py