Visual studio code not finding Python modules - python

I have activated my venv in the terminal, I have insured that Pillow is installed and checked that I have the right Python interpreter selected in VSCode I am still getting these errors.
(env) PS C:\Users\Pixel\Projects\Python\DiceDistributionV2> python -u "c:\Users\Pixel\Projects\Python\DiceDistributionV2\main.py"
Traceback (most recent call last):
File "c:\Users\Pixel\Projects\Python\DiceDistributionV2\main.py", line 2, in <module>
from PIL import Image, ImageTk
ModuleNotFoundError: No module named 'PIL'
(env) PS C:\Users\Pixel\Projects\Python\DiceDistributionV2> pip list
Package Version
------------- -------
customtkinter 5.0.2
darkdetect 0.7.1
Pillow 9.4.0
pip 22.0.4
setuptools 58.1.0
Any idea what I can try next?

Please see your terminal command
(env) PS C:\Users\Pixel\Projects\Python\DiceDistributionV2> python -u "c:\Users\Pixel\Projects\Python\DiceDistributionV2\main.py"
Obviously you're using Code Runner to execute the script, if you're using the Python extension the command would be something like this:
Selecting an interpreter is a feature of Python extensions, so what interpreter you choose will not affect Code Runner.
Please use the official extension Python to run the script and you will get correct results.
PS: You can use the following code to verify the interpreter used by Code Runner
import sys
print(sys.executable)
As shown in the picture above, I selected the python3.11, but Code Runner still uses the python3.10 interpreter.
Python extension has no problem.
If you still get the error, make sure you are using the correct interpreter (the one with Pillow installed). Or use the above code to output the full path of the interpreter, and then use the python command of the full path to install the Pillow package for the current interpreter.

Related

ImportError : No Module Named pytesseract

I have installed pytesseract successfully but still getting this error in vscode.
I tried installing tesseract in my venv in vscode. and it was successfully installed. but still , I'm getting this error.
I used a simple code i.e.,
from PIL import Image
import pytesseract as pt
def tesseract():
path_to_tesseract = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image_path="test.jpg"
pt.tesseract_cmd = path_to_tesseract
text = pt.image_to_string(Image.open(image_path))
print(text)
tesseract()
Import errors occur either due to the package not being installed or being installed in a different path.
Since you said you installed pytesseract, I’m guessing it’s the latter. Try running your script in verbose mode with the -v flag to see the path in which Python looks for your packages.
Then you can manually check if pytesseract is installed inside there or somewhere else.
While in your venv, can you try running
pip3 freeze > installed-modules.txt
and share the output here? That should show us what modules are installed in your venv and we can continue from there. Also, do you have the Python Environment Manager extension installed in VS Code?
So sorry but last initial troubleshooting step, in the bottom right corner of VS code, when you have your python file open, does it state that it's using the venv? This can be really telling if its just not utilizing your venv
vs-code demonstration venv
The most common way this ImportError can occur is if pip install refers to a different Python version than VS Code is using, or the virtual environment is not activated in VS Code, or it was not activated when pytesseract was installed.
You can check that pip matches the Python version VS Code is using by running:
pip -V
at the command line, and remember it's output. Then, in VS Code run the following Python script and make sure they match. If they match, then there shouldn't be an issue. If they don't match, some combinations of the virtual environment and the Python Version are not setup correctly.
import sys, subprocess
subprocess.check_call([sys.executable, "-m", "pip", "-V"])
If this is the issue, it can be solved by installed pytesseract by running the following Python script in VS Code.
import sys, subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "pytesseract"])
import pytesseract
To explain the command, subprocess.check_call executes a call on the command line, as if you had typed it at the terminal. The difference is uses the full path to the Python Interpreter, from sys.executable (including the virtual environment) to make sure it is setup right.
If this completes without an ImportError, then pytesseract should be working now.

My Python library will not load and adding path to .zprofile did not work

I've had python 3 insalled on my system for a few months and have never had an issue importing libraries before. After using pip3 to install pygame, IDLE couldn't find the pygame library:
Traceback (most recent call last):
File "/Users/williamh/unit-6-project-music-pyer/code/player pygame rewrite.py", line 3, in <module>
import pygame.mixer as mixer
ModuleNotFoundError: No module named 'pygame'
I have tested a sample program from command line, and pygame seems to be working and installed. Both IDLE and Visual Studio Code give modulenotfound errors. I have tried updating .zprofile with the path pygame was installed under:
# Setting PATH for Python 3.9
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
export PATH
PATH="/opt/homebrew/lib/python3.9/site-packages:$PATH"
export PATH
eval "$(/opt/homebrew/bin/brew shellenv)"
The only new lines added to .zprofile are the 2nd "PATH" command and the 2nd "export PATH" After restarting my mac, it still wont load. Here's my system specs:
MacBook Pro (13-inch, M1, 2020)
Chip: Apple M1
Memory: 8 GB
Terminal Default Shell: -zsh
I have found a simple solution. M1 macs default to -zsh shell for terminal. This was the problem that caused pygame to install to the wrong place. Running this code in terminal switches your default shell to -bash:
chsh -s /bin/bash
Then after that, I installed pygame again.
pip3 install pygame
Now pygame imports and works perfectly.
This is a common problem, it means you don't have Pygame library installed, first open your command prompt (for windows) or terminal (for Linux & mac). then you need to check for python installation :
python --version
then you go ahead and check for pip installation:
pip --version
if everything seems right you can install Pygame:
pip install pygame
if your system recognize python, pip and pygame is already installed, you should change your python interpreter in your IDE.
if you are using VScode you can change it from the bottom right corner of the app, and when you click on it "Select Interpreter" will appear then you can go ahead and select the environment you want to use.

Python 3.9 import problem (modulenotfounderror)

I have python 3.9 installed in windows. I use pycharm.
I downloaded the module qrcode using the code 'pip install qrcode' in the terminal.
This has successfully installed as when I type 'pip show qrcode' in the terminal in pycharm, I get;
Name: qrcode
Version: 7.2
Summary: QR Code image generator
Home-page: https://github.com/lincolnloop/python-qrcode
Author: Lincoln Loop
Author-email: info#lincolnloop.com
License: BSD
Location: c:\users\user\appdata\local\programs\python\python39\lib\site-packages
Requires: colorama
Required-by:
The problem occurs when I try to import qrcode into my project (a qrcode generator), after typing import qrcode, I get the following message;
C:\Users\USER\PycharmProjects\MyProjects\venv\Scripts\python.exe "C:/Users/USER/PycharmProjects/MyProjects/QR Code Generator.py"
Traceback (most recent call last):
File "C:\Users\USER\PycharmProjects\MyProjects\QR Code Generator.py", line 1, in <module>
import qrcode
ModuleNotFoundError: No module named 'qrcode'
If i type from colorama import qrcode, it still doesn't work as it says colorama is not found even though I installed it.
I have tried finding the sys path and if I can't find the location of my site packages in it I will just use the append function to add them. However, it is still there as you can see below;
['C:\\Users\\USER\\PycharmProjects\\MyProjects', 'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip', 'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\DLLs', 'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\lib', 'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39', 'C:\\Users\\USER\\PycharmProjects\\MyProjects\\venv', 'C:\\Users\\USER\\PycharmProjects\\MyProjects\\venv\\lib\\site-packages']
Just a side note, when I click file, settings, project interpreter, this is what I see;
[Screenshot]
https://i.stack.imgur.com/83omh.png
The location of python.exe on my laptop is C:\Users\USER\AppData\Local\Programs\Python\Python39
The location of python 3.9 (64 bit) on my laptop is C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.9
I started python just a few weeks ago, so I don't know that much.
Anyways, hope I can get an answer.
You are using a virtualenv inside of PyCharm and you are installing the qrcode package on the global python installation.
Take a look at the package install location and the location of the python executable getting called from within PyCharm
qrcode:
c:\users\user\appdata\local\programs\python\python39\lib\site-packages
PyCharm python.exe
C:\Users\USER\PycharmProjects\MyProjects\venv\Scripts\python.exe
You need to source the virtualenv and then install qrcode in the virtualenv.
Start reading here...
EDIT
navigate to correct folder C:\Users\USER\PycharmProjects\MyProjects
run this command: .\env\Scripts\activate.bat (or .\env\Scripts\activate.ps1 if using PowerShell)
your commandline should now indicate you are using a virtualenv.
run this command: pip install qrcode
try running your code now...
Another option would be to create a requirements.txt file in the same directory as your project, and put qrcode as a single line in there. IIRC PyCharm will pop up a notification asking if you want to install qrcode. (I'm not 100% sure about this one as I switched to Visual Studio Code)
I think the python interpreter for your project is directed to an interpreter in the virtualenv of your project (created by pycharm when you created the project), but your package got installed on your native python installation, according to your installation response. From what i see, you can either:
a) change your project's python interpreter to your own python installation (just add C:\Users\USER\AppData\Local\Programs\Python\Python39\python.exe as an interpreter and set it as your project interpreter); or
EDIT:
b) Install the package using the virtualenv's python interpreter. Activate your project's virtual environment with 'C:\Users\USER\PycharmProjects\MyProjects\venv\Scripts\activate' (your prompt should change showing you are now using the virtual environment). Then use pip install qrcode. If this doesn't work try 'C:\Users\USER\PycharmProjects\MyProjects\venv\Scripts\python.exe -m pip install qrcode' on your terminal. If this doesn't work maybe try 'C:\Users\USER\PycharmProjects\MyProjects\venv\Scripts\python.exe -m C:\Users\USER\PycharmProjects\MyProjects\venv\Scripts\pip.exe install qrcode'.
END OF EDIT
This link also contains a bit about how to manage your interpreter and install/upgrade packages for your project Install, unninstall and upgrade packages-jetbrains

ModuleNotFoundError: No module named: openCV

I have installed opencv for example, and when importing in sublimetext or in a terminal I jump "ModuleNotFoundError: No module named". If I do it from the python idle, it does not
I reproduced your error and was able to fix it as following:
This error often happens when you did not added your Python path correctly.
To check wether your python is installed an configured correctly go to your terminal and type:
python -V
and then:
pip -v
The two versions have to match.
Also simply run:
python
and check wether an interactive shell opens.
If your versions are matching and the the python command works, your python is configured correctly and you can skip the next step. If not follow the steps below, if everthing works skip this:
For Windows try this:
https://superuser.com/questions/143119/how-do-i-add-python-to-the-windows-path
For Mac OS try this:
export PATH=~/path/to/your/python/bin:$PATH
For example:
export PATH=~/anaconda2/bin:$PATH
Now the next thing...
CV2 has two prerequisites there for run:
pip install matplotlib
pip install numpy
and finally:
pip install opencv-python
Now you are ready to go an you can import opencv in your code as following:
import cv2
When you are using pyhton3 use:
python3 test.py
to run your code.
If you want to use python instead of python3 do this:
The last Python you install that registers itself in the environment is the default.
If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3

Running python files from command prompt

I have written a python program in eclipse that imports the mechanize module. It works perfectly there. When I run the .py file from the command prompt, it shows this error: "No module named mechanize". How do I rectify this?
Make sure that Eclipse and prompt are using the same python version. Simply typing $ python on the command line show you the version you are using from there.
The mechanize module must be in your site-packages folder in order for python to find it.
(C:\Python\Lib\site-packages)
If the module is not in your site-packages folder then you can install it as follows:
Download the source code from http://pypi.python.org/packages/source/m/mechanize/mechanize-0.2.5.tar.gz
Now extract and install the package (This is what you do on Linux, on Mac or Win. this might be slightly different)
$ tar zxvf mechanize-0.2.5.tar.gz)
$ sudo python setup.py install

Categories

Resources