I'm trying to run a script that launches, amongst other things, a python script. I get a ImportError: No module named ..., however, if I launch ipython and import the same module in the same way through the interpreter, the module is accepted.
What's going on, and how can I fix it? I've tried to understand how python uses PYTHONPATH but I'm thoroughly confused. Any help would greatly appreciated.
This question is related to
python
ipython
jupyter-notebook
importerror
If you are running it from command line, sometimes python interpreter is not aware of the path where to look for modules.
Below is the directory structure of my project:
/project/apps/..
/project/tests/..
I was running below command:
>> cd project
>> python tests/my_test.py
After running above command i got below error
no module named lib
lib was imported in my_test.py
i printed sys.path and figured out that path of project i am working on is not available in sys.path list
i added below code at the start of my script my_test.py
.
import sys
import os
module_path = os.path.abspath(os.getcwd())
if module_path not in sys.path:
sys.path.append(module_path)
I am not sure if it is a good way of solving it but yeah it did work for me.
Doing sys.path.append('my-path-to-module-folder')
will work, but to avoid having to do this in IPython every time you want to use the module, you can add export PYTHONPATH="my-path-to-module-folder:$PYTHONPATH"
to your ~/.bash_profile
file.
Solution without scripting:
Remove pathlib
and reinstall it. Delete the pathlib in sitepackages
folder and reinstall the pathlib package by using pip command:
pip install pathlib
The main reason is the sys.paths of Python and IPython are different.
Please refer to lucypark link, the solution works in my case. It happen when install opencv by
conda install opencv
And got import error in iPython, There are three steps to solve this issue:
import cv2
ImportError: ...
1. Check path in Python and iPython with following command
import sys
sys.path
You will find different result from Python and Jupyter. Second step, just use sys.path.append
to fix the missed path by try-and-error.
2. Temporary solution
In iPython:
import sys
sys.path.append('/home/osboxes/miniconda2/lib/python2.7/site-packages')
import cv2
the ImportError:..
issue solved
3. Permanent solution
Create an iPython profile and set initial append:
In bash shell:
ipython profile create
... CHECK the path prompted , and edit the prompted config file like my case
vi /home/osboxes/.ipython/profile_default/ipython_kernel_config.py
In vi, append to the file:
c.InteractiveShellApp.exec_lines = [
'import sys; sys.path.append("/home/osboxes/miniconda2/lib/python2.7/site-packages")'
]
DONE
This answer applies to this question if
path below can be relative
PYTHONPATH=/path/to/dir python script.py
I found yet another source of this discrepancy:
I have ipython installed both locally and in commonly in virtualenvs. My problem was that, inside a newly made virtualenv with ipython, the system ipython was picked up, which was a different version than the python and ipython in the virtualenv (a 2.7.x vs. a 3.5.x), and hilarity ensued.
I think the smart thing to do whenever installing something that will have a binary in yourvirtualenv/bin
is to immediately run rehash
or similar for whatever shell you are using so that the correct python/ipython gets picked up. (Gotta check if there are suitable pip
post-install hooks...)
This is how I fixed it:
import os
import sys
module_path = os.path.abspath(os.getcwd() + '\\..')
if module_path not in sys.path:
sys.path.append(module_path)
I have found that the solution to this problem was extensively documented here:
https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/
Basically, you must install the packages within the Jupyter environment, issuing shell commands like:
!{sys.executable} -m pip install numpy
Please check the above link for an authoritative full answer.
Make sure they are both using the same interpreter. This happened to me on Ubuntu:
$ ipython3 -c 'import sys; print(sys.version)'
3.4.2 (default, Jun 19 2015, 11:34:49) \n[GCC 4.9.1]
$ python3 -c 'import sys; print(sys.version)'
3.3.0 (default, Nov 27 2012, 12:11:06) \n[GCC 4.6.3]
And sys.path
was different between the two interpreters. To fix it, I removed Python 3.3.
Just create an empty python file with the name __init__.py
under the folder which showing error, while you running the python project.
Had a similar problem, fixed it by calling python3
instead of python
, my modules were in Python3.5.
This kind of errors occurs most probably due to python version conflicts. For example, if your application runs only on python 3 and you got python 2 as well, then it's better to specify which version to use. For example use
python3 .....
instead of
python
Before installing ipython, I installed modules through easy_install; say sudo easy_install mechanize
.
After installing ipython, I had to re-run easy_install for ipython to recognize the modules.
This is probably caused by different python versions installed on your system, i.e. python2 or python3.
Run command $ pip --version
and $ pip3 --version
to check which pip is from at Python 3x. E.g. you should see version information like below:
pip 19.0.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
Then run the example.py
script with below command
$ python3 example.py
Happened to me with the directory utils
. I was trying to import this directory as:
from utils import somefile
utils
is already a package in python. Just change your directory name to something different and it should work just fine.
import sys sys.path.append('/Users/{user}/Library/Python/3.7/lib/python/site-packages') import ta
Source: Stackoverflow.com