ModuleNotFoundError: No module named 'IPython', but ipython is installed - python

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

Related

Module not found error in Visual Studio Code despite the fact that I have just installed it

I want to run a python script with the cherrypy module. I use pip install cherrypy to install it and all looks ok. Then I click the "Run python file" button and I face the error ModuleNotFoundError: No module named 'cherrypy'.
Trying to reinstall cherrypy I see many Requirement already satisfied responses.
If it helps I have already edit, in the user path variables panel, the Path variable and added C:\Users\username\AppData\Local\Programs\Python\Python37-32\Scripts as a path.
edit: I suspect some problem with the path. So i run the following.
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Python25'
it returns: 'C:\\Users\\username\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0'
Have you tried to install cherrypy using C:\Users\username\AppData\Local\Programs\Python\Python37-32\Scripts\pip install cherrypy
When I install py libraries I always run where python and find the right python copy it and modify the path so it goes in Scripts then run pip install cherrypy
Also I noticed that when I install py libraries it seems VSCode won't recognize it even though it runs. To fix this you have to restart VSCode again
Another possibility is that your VSCode isn't using correct version of Python. To check this look at VSCode bottom left you should see your python version. If VSCode can't recognize it you will have to enter the Python path manually by Open Command Palette Ctrl+Shift+P and choose Python: Select Interpreter then click enter Interpreter path

Error occurring while installing and importing pynput

So I am trying to install and import pynput in VSCode but its showing me an error every time I try to do it. I used VSCode's in-built terminal to install it using pip and typed the following :
pip install pynput but this error is shown : Fatal error in launcher: Unable to create process using '"c:\users\vicks\appdata\local\programs\python\python38-32\python.exe" "C:\Users\vicks\AppData\Local\Programs\Python\Python38-32\Scripts\pip.exe" install pynput': The system cannot find the file specified
After receiving the following error, I tried using CMD to install it but the same error is shown. I also tried using python pip install pynput and it shows Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases. even though I have python 3.9.7 and I have selected it as my interpreter in VSCode and I have IDLE(Python 64 bit) installed. How may I resolve the following error? Any help regarding the same is appreciated
Thanks in advance :)
There's no such thing as an in-built terminal in VS code. When you open a terminal in VS Code, it opens the default, which on Windows is usually equivalent to opening up CMD.
If you selected Python 3.9.7 as your default interpreter in VS Code, it does not mean that it will visible to your CMD / terminal. It just means that the VS Code IDE will refer to that instance of Python when launching the program from VS Code itself using the green button (or F5), and when scanning your code to point out missing packages, etc.
CMD will only automatically detect your Python if it's in your PATH environment variable. You should add the Python 3.9.7 base and Scripts path to this.
Also, it would be best if you could first uninstall conflicting versions (like your 3.8.x) of Python and remove them from PATH, assuming that this won't cause any problems for you. Perhaps keep a record all the installed packages in this old version of Python for future reference using pip freeze or pip list.
Check if c:\users\vicks\appdata\local\programs\python\python38-32\python.exe exists by typing cd c:\users\vicks\appdata\local\programs\python\python38-32

Import does not look into pythonpath when importing personal modules

I have build my own package named "XXX" on my computer by setting a __init__.py file in the C:\path\to\module\XXX folder, which contain my python script. Then I added C:\path\to\module\XXX to my pythonpath manually. When I run python on a cmd prompt and try to import the module it fails. When I print sys.path, it contains C:\path\to\module\XXX. If I try to load some other personnal modules, the same problem occures but when I try to load module not of my own, installed with pip (i.e. numpy) it works perfectly. So my guess is that for some reason python is not looking into my pythonpath anymore to load module.
I am using anaconda with python 3.6.5 with a vitual environnement on windows 7. Python is installed with anaconda via conda-forge repo.
I have tested this issue with multiple approach, first by running the script below with Pycharm and then with Spider. Then I used anaconda prompt to run the commands manualy activating my virtual environnement first. Finally I ran the commands on a DOS prompt with python. Whatever the way, the results where the same. This error just occured after installing fbprohete in my virtual environnement. Before that I was able to import my modules. I tried to remove that module but the problem remains the same. A lot of mess append during the installation of fbprohet. After the installation I ran a conda clean command to clean the environnement but it didn't help. I suspect that something is corrupted now but I would like to avoid reinstalling everything. If you have an idea where to look, which module in particular should be corrupted, I will be glad to know more about it.
import sys
sys.path
['', 'C:\path\to\module\XXX', ...]
import XXX
ModuleNotFoundError: No module named 'XXX'
Whith the script above, I expect the module to be loaded without error since the path to the package is in the pythonpath and the __init__.py is in the folder C:\path\to\module\XXX
Thanks!
So it was a bit silly actually. I should have replaced C:\path\to\module\XXX by C:\path\to\module in the pythonpath because __ini__.py is in the C:\path\to\module\XXX folder. Thanks Giacomo Alzetta for your comment.

Installing Python packages for Visual Studio Code

I'm trying to import a package called "termcolor" that can run on VSC. I know the import is working because when I run the program from my mac terminal it executes fine. However, when I run from VSC, I get the ImportError "no module named termcolor".
I know it has been installed in the my virtual environment where I am building the program:
Kaylas-iMac:Ermes Marana rahme$ source "/Users/rahme/Desktop/Programming/Python/Ermes Marana/env/bin/activate"
(env) Kaylas-iMac:Ermes Marana rahme$ python3 -m pip install termcolor
Requirement already satisfied: termcolor in ./env/lib/python3.7/site-packages (1.1.0)
And I think I'm importing from the correct path. I saw in another post to use sys.path.append() to explicitly look for packages in the correct path, so I tried that. Here is my code:
import sys
sys.path.append('users/Rahme/Desktop/Programming/Python/Ermes\ Marana/env/lib/python3.7/site-packages')
import termcolor
print(termcolor.colored(text = "I'm Rahme", color ="red", on_color="on_grey"))
sys had a warning about the space in the directory /Ermes\ Marana and suggested adding an r to the backslash which I did and nothing changed. What am I doing wrong?
How can I install packages on VSC in the future so that I can use the built in run in terminal?
Just because you switched to your virtual environment in the mac terminal doesn't mean that the VSC terminal is operating in that virtual environment. To get VSC to know you want it to run in your environment, you need to hit ctrl+shift+p to pull up the command pallette. Then you type python: select interpreter. You will get a list of all the virtual environments that VSCode can see and you just select your environment from that list. Once you do that, VSC should operate fine inside your environment.
try
sys.path.append(r'users/Rahme/Desktop/Programming/Python/Ermes/
Marana/env/lib/python3.7/site-packages')
What is the purpose of the space?
Edit: keep the space if it is part of the folderpath
Please do see the r I have added infront of the folderpath

"ImportError: No module named" when trying to run Python script

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

Categories

Resources