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 issue arises due to the ways in which the command line IPython interpreter uses your current path vs. the way a separate process does (be it an IPython notebook, external process, etc). IPython will look for modules to import that are not only found in your sys.path, but also on your current working directory. When starting an interpreter from the command line, the current directory you're operating in is the same one you started ipython in. If you run
import os
os.getcwd()
you'll see this is true.
However, let's say you're using an ipython notebook, run os.getcwd() and your current working directory is instead the folder in which you told the notebook to operate from in your ipython_notebook_config.py file (typically using the c.NotebookManager.notebook_dir setting).
The solution is to provide the python interpreter with the path-to-your-module. The simplest solution is to append that path to your sys.path list. In your notebook, first try:
import sys
sys.path.append('my/path/to/module/folder')
import module_of_interest
If that doesn't work, you've got a different problem on your hands unrelated to path-to-import and you should provide more info about your problem.
The better (and more permanent) way to solve this is to set your PYTHONPATH, which provides the interpreter with additional directories look in for python packages/modules. Editing or setting the PYTHONPATH as a global var is os dependent, and is discussed in detail here for Unix or Windows.
Just create an empty python file with the name __init__.py under the folder which showing error, while you running the python project.
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.
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
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.
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)
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.
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.
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.
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
Had a similar problem, fixed it by calling python3 instead of python, my modules were in Python3.5.
This problem occurs due to the different versioning - e.g. if the Python installed on your machine is installed in a folder called path_to_lib/python3.6 but your notebook is running in Python 3 - the spacing in the naming matters!
How to solve it?
When creating a new jupyter notebook, just choose the Python with the same versioning as yours (watch out for spaces!). See the attached image.
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...)
Solution without scripting:
Open Spyder -> Tools -> PYTHONPATH manager
Add Python paths by clicking "Add Path".
E.g: 'C:\Users\User\AppData\Local\Programs\Python\Python37\Lib\site-packages'
Click "Synchronize..." to allow other programs (e.g. Jupyter Notebook) use the pythonpaths set in step 2.
Restart Jupyter if it is open
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.
This answer applies to this question if
You don't want to change your code
You don't want to change PYTHONPATH permanently
Temporarily modify PYTHONPATH
path below can be relative
PYTHONPATH=/path/to/dir python script.py
import sys
sys.path.append('/Users/{user}/Library/Python/3.7/lib/python/site-packages')
import ta
If anyone comes across this issue using conda with Jupyter Notebook in MSVS Code, the solution is to make sure you're using the correct kernel. The kernel is in a box in the top right corner of the interface and looks like this:
I pointed mine to the version of Python that also matched my application path -- problem solved!
This is what worked for me: I just changed my working directory inside my notebook
import os
os.chdir("my/path/to/module")
os.getcwd()
I have a similar issued with my Jupyter Lab setup which I resolved by checking the Jupyter Lab log on opening. This informed me that the virtual environment (pipenv) couldn't locate the Jupyter Lab so it used a shared version (from an earlier installation of Python).
I created a requirements.txt and discovered I hadn't installed Jupyter Lab in this new environment. Installing it resolved the import error.
Remove pathlib and reinstall it. Delete the pathlib in sitepackages folder and reinstall the pathlib package by using pip command:
pip install pathlib
Related
I have tried to put this script in the sitecustomize.py file, to be able to see colored error messages in visual studio code.
the script:
import sys
from IPython.core.ultratb import ColorTB
sys.excepthook = ColorTB()
I got the error message above even though I have used pip to install ipython.
EDIT:
I tried to use coloredlogs instead of ipython and it showed me the same message again
The IPython module only works inside iPython, not inside any other IDE/Shell/etc.
Please use the command "pip show ipython" to check the installation location of the module:
(If it does not display the location of the module, it means that the terminal used environment is inconsistent with the selected one.)
Then find the folder of the module in this python environment, try to delete it, and then reinstall it:
Run:
I needed to reinstall python on my computer. there were multiple versions of it, and because of that there were multiple versions of pip
I apologize if what I'm trying to achieve is not Pythonic - I recently moved to the language.
I have a project directory structured like so:
root
--proj1
----venv
----main.py
--proj2
----venv
----main.py
Both proj1 and proj2 run under their own virtual environments. I am trying to call proj2/main.py from proj1/main.py, whilst executing proj2/main.py under its own venv. I have tried:
import subprocess
s2_out = subprocess.check_output([sys.executable, r"..\proj2\__main__.py", "arg"])
This invokes successfully, but I am getting all manner of not found exceptions, etc. I am guessing this is the reason why.
Please let me know if there is a better approach!
You can do this:
import subprocess
subprocess.call(["python_interpreter location (python.exe)", "python file"])
So you could do:
import subprocess
subprocess.call(["../proj2/bin/python.exe", "proj2/main.py"])
For Mac OS and Linux, the python interpreter path for a venv would be folder/bin/python.exe, or in your case ../proj2/bin/python.exe.
For Windows, the python interpreter path for a venv would be folder/scripts/python.exe.
You may need to include the full paths.
Another way to do this could be using subprocess.call, if you need the output:
import subprocess
output = subprocess.call("%s %s" %("../proj2/bin/python.exe", "proj2/main.py"))
print(output)
Both ways will work just fine :)
Hey this is not a complete answer but is how I would approach it.
If you use pyenv then this would be the approach:
Make a separate virtualenv for each project. pyenv virtualenv 3.8 proj1 and pyenv virtualenv 3.7 proj1 or whatever the python versions are.
use pyenv local in each directory to link the dirs to the venvs
cd to each directory and in each one the python env should activate. Use pip install to install the libs to each venv.
Now you should have access to different python executables that use separate libs, e.g. ~/.pyenv/versions/proj1/bin/python
So from proj1 code you should theoretically be able to do:
import os;
os.system("~/.pyenv/versions/proj2/bin/python ../proj2/main.py")
or something like that.
I have not actually tried this but I am fairly certain it would work for using separate libs.
Here is pyenv: https://github.com/pyenv/pyenv
I will try it myself tomorrow when I am not sleepy.
Having some weird troubles installing python modules on my work computer (read: no admin/root rights), I'm using 2.7.5. I downloaded and unpacked the tarball and ran 'setup.py', but it had no effect: When I open the python shell, it can't find the module (this specific one is fuzzywuzzy). However, if I right click -> edit with IDLE the setup.py, and then run the shell from that file, it loads and works perfectly fine. Or, if I then open a new file from that shell, use the module and run it, it works fine. -__-
I've tried using:
import sys
sys.path.append('path here')
to append the location where the module is installed, but this doesn't help, nor does the path stay in the sys.path list when I close/reopen the shell.
This is actually driving me insane. Can anyone help? I'm relatively new to programming and python.
The best and easy way provided by python to install/uninstall packages is to use PIP.
use this
python -m pip install packagename==version
same way to uninstall
python -m pip uninstall packagename==version
if you are using windows you need to set path variable first usually python file will be in path C:\Python27 to set path variable
PATH=%PATH%;C:\Python27;
I've downloaded the binaries here for devsim for ubuntu.
If you go to the bin folder there is an executable called devsim which basically seems like a python interpreter with the devsim module preloaded as ds. What I would like is instead of dropping into an python interpreter dropping into an ipython one.
Is this an easy thing to do? I tried thigs like:
ipython devsim
but gave me an error. I also tried running ipython and attempted to import devsim which also failed.
To be honest I had low hopes that the above two tricks would work. Any advice guidance?
/usr/bin/ipython is a python script. Devsim starts its own embedded python 2.7 interpreter.
On my ubuntu 12.04 system, the following works for me:
devsim /usr/bin/ipython
The devsim packages currently online are for ubuntu 12.04. If you need any help getting it to work for your system, please file a ticket at http://www.github.com/devsim/devsim.
This answer is inspired by the answer given by Juan. I thought of editing his answer but then this evolved a lot so decided to keep it separately.
Some of the steps below will require root access and assume a *nix based system.
Create a file with the following:
#!/bin/bash
/path/to/executable/devsim /usr/bin/ipython "$#"
Save the file as /usr/bin/devsim
Make it executable by running chmod uoa+x /usr/bin/devsim
Now whenever you type devsim at the command prompt you'll be dropped into an ipython session. To access the devsim module you simply do:
import ds or from ds import *
depending on your preference.
I'm pretty new to python and twisted and I tried to run a simple twisted script but failed.
My environment:
MacOX 10.7.2 + Python 2.7.1 + Twisted 11.0.0 + eclipse + PyDev
The script called test.py:
from twisted import reactor
reactor.run()
I tried to run it in the terminal and everything works fine.
Then I opened eclipse and created a new PyDev project then adding a py file called test.py and
typed the code above in.
When I tried to run it I got errors like:
Traceback (most recent call last):
File "/Users/user/Documents/workspace/TwistedDemo/test.py", line 2, in <module>
from twisted import reactor
ImportError: No module named twisted
Then I checked the PYTHONPATH and added the "twisted source folder" in the external libraries tab but the errors was still there.(nothing changed)
Hope someone can help me out here, thanks in advance :)
Several things to try here -
Since your env is Mac OSX. I installed commandline tools from Xcode which solved this problem. Xcode 4.4 and later install Command Line Tools
For the sake of completeness. If this on Ubuntu, then apt-get install python-twisted generally works.
from your eclipse put this in your python script -
python -c 'import sys;print sys.path'
This tells all the paths that python looks for when you import something. If you don't find twisted path there then add it to this like so - sys.path.append(twisted_dir_path)
Finally, if all the above does not help. type -p python in a shell will tell you which version you're using; I would mostly be /usr/bin/ or some variant of it.
Then /usr/bin/python2.7 -c 'from twisted.internet.protocol import Factory,Protocol' ought to succeed.
This has happened to me so many times, but I figured out previously. Basically, if you have too many versions of python on your mac, Aptana Studio for some reason gets confused, so make sure that your twisted installation is using the same python as the command line (even though you already added twisted to Aptana). I'll get into more specifics later
Ok, I think this is how you do it:
Grab the python path of the python that has twisted installed correctly, using this
import sys
print sys.path
And just grab the base path, nothing to specific.
Go to Aptana-preferences-PyDev-Interpreter-Python
And then just add a new interpreter (in my case I just called it python1 and set is the first one. If that doesn't work then just set a new PYTHONPATH right below it.
You need to install incremental before twisted
pip install --upgrade incremental
pip install Twisted
This should solve the problem.
Not sure how you installed twisted, did you try easy_install or pip, or install in manually?
If you want to make sure it's installed properly, try opening up a terminal, typing python, and then "import twisted". If it comes back without errors, then it's installed. You might need to set the PYTHONPATH variable to include the source folder.
Then, since you're using pydev in eclipse, you need to refresh your configuration each time you add a new library.
On the mac, this is under preferences -> PyDev -> Interperter-Python
I find it works best to remove my configuration and re-add it, for it to pick up everything. But you can click on new folder (select the folder with the init.py), or the new egg, if it's an easy_install egg (a .egg file is a zip file, if it's unzipped, you'll see a EGG-INFO folder in the subdirectory of the folder you want to select).
You should set the correct PYTHONPATH in Eclipse as following:
right click your project.
select Properties
select pyDev-PYTHONPATH
add your twised in the external libraries