how to install PIL with pip? - python

when I run the following
from tkinter import *
from PIL import ImageTk, Image
root.mainloop()
I got
Traceback (most recent call last):
File "image_viewer.py", line 2, in <module>
from PIL import ImageTk, Image
ImportError: No module named PIL
but I already install Pillow and everything is fine.

Use Pillow which is the "new" or the replacement of PIL, but has the same-named modules to preserve compatibility:
pip install pillow
Also, as suggested in the comments, maybe you are just using the wrong python binary, try to check if you're in/out of a virtual environment or check differences between python vs python3 vs python2 on your system:
python -m pip list
python2 -m pip list
python3 -m pip list

If you are sure that you already installed pillow use this command pip install pillow --upgrade, then you can use the command pip freeze to list all modules that have been installed.

The problem is that you are not running the same python interpreter. In addition, the problem could arise from this fact that python cannot find the path to the OpenCV.
You first need to find the path to the OpenCV installed on your OS.
First, open a python script and run this:
import cv2
PATH = cv2.__file__
print(PATH)
This will print the PATH to the OpenCV installed on your OS.
Then, add the following two lines to the top of your main script (before calling tkinter and PIL):
import sys
sys.path.append('PATH')
from tkinter import *
from PIL import ImageTk, Image
root.mainloop()
Alternative Solution:
The problem could be that you are not running the same python interpreter.
You first need to find the path to the python executable that is interpreting your python scripts.
Open a python script and run this:
import sys
PATH = sys.executable
print(PATH)
This will print the path to the python executable that is interpreting your python scripts.
Now, you can install pillow in the found path as follows:
PATH -m pip install pillow

Related

Import Cv2 not working despite opencv installed

I have installed OpenCV with the command pip install opencv-contrib-python and when I run the command pip list I see opencv-contrib-python has been installed and yet import cv2 does not work for me and it throws
Traceback (most recent call last):
File "\[filename here\]", line 1, in \<module\>
import cv2
ImportError: No module named cv2
and I have tried uninstalling and reinstalling OpenCV and it still does not work. I tried different development environments like Pycharm and visual studio code. I also have my pip upgraded and my OpenCV is version 4.5.5 which is the latest version. I have tried using python 3.10 and 3.7 but the same error still shows. I have also tried using
import sys
sys.path.append('/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages')
which was recommended in some answers and I have tried just installing opencv-python and using that and the same error still shows. I checked and I have cv2 in
my folder
but despite this, it keeps showing no module named cv2 every time I try to run import cv2
I use macOS Monterey version 12.2.1 and for my terminal, I use Darwan OS which is a Linux distro.
I tried:
pip install opencv-python
pip install opencv-contrib-python
putting this at the first lines of code:
import sys
sys.path.append('/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages')
You have to use python2.7. As the manual says: "Currently, only the Python 2 version of the cv2 module is built and included in the latest release."
You can only use python3 if you manually build from source code.

Where do python modules get saved to in a root directory?

So, I'm a bit new to python and have mastered javascript.
I know that javascript modules are saved in node_modules folder in a directory
I did: $ pip install pyautogui
but when I'm about to run my code, it says :
'Traceback (most recent call last):
File "bruh.py", line 1, in <module>
import pyautogui, time
ImportError: No module named pyautogui'
where do python modules get saved to? 'python_modules' folder?????
also, idk what to do, can anyone help me with this error?
To see the path where your modules are saved once you pip them.
You can open a python shell, import sys and print the sys.path variable. One of the path would end with site-packages, thats the path where modules are saved.
$ python3
>>> import sys
>>> print(sys.path)
[‘’, ‘/Users/uname/.local/lib/python3.6/site-packages/‘]
could be a number of things. do you have multiple versions of python on your machine? If so you'd need to pip install to the correct python version, e.g.:
pip3 install pyautogui vs pip install pyautogui

Tesseract-OCR / Pillow not working with Pycharm

I have installed Tesseract-OCR using the provided installer and added it to the path. I have also installed Pillow using pip through CMD. But when I attempt to import them into pycharm it says that the modules do not exist.
When I type 'tesseract' in CMD, it outputs the correct information, so I think I must be missing a step somewhere.
I am attempting to import them using the Following:
from PIL import Image
import pytesseract
Please help...

ImportError: No module named scenedetect on Linux Ubuntu

I'm trying to use pyscenedetect library on python for videos but I get this error when using the python interface and when I use the command line interface I get the error "ModuleNotFoundError: No module named 'cv2'"
even though I believe I installed both correctly according to the documentations.
I have trying to look for different ways to import opencv for the second error but to no avail. As for the first error i can't find any answers to my problem.
import cv2
import numpy as numpy
import os
import scenedetect
from scenedetect.video_manager import VideoManager
from scenedetect.scene_manager import SceneManager
from scenedetect.frame_timecode import FrameTimecode
from scenedetect.stats_manager import StatsManager
from scenedetect.detectors import ContentDetector
If you have pip you can try
pip install opencv-python
If you have anaconoda, you can try
conda install -c conda-forge opencv
it's probable that you installed it on a different python installation in your PC.
To know where your python installation is you can launch python and:
import sys
sys.path
To get the list of everything you have installed you can:
pip freeze > installed_modules.txt
Try only running
import cv2
So that you can test it
I found the problem. As Ivan was pointing out, the problem was with openCV.
I used the following command:
sudo apt install python3-opencv

ImportError: No module named pytesseract

I was following this guide https://realpython.com/blog/python/setting-up-a-simple-ocr-server/ and got to the part where I run cli.py python flask_server/cli.py but I get
python cli.py
Traceback (most recent call last):
File "cli.py", line 3, in <module>
import pytesseract
ImportError: No module named pytesseract
How can I solve this ?
I also saw that I have multiple versions of python. I have linux-kali installed with the latest updates.
Something else: he runs the command like python flask_server/cli.py- where is that flask_server located ? I simply ran it like python cli.py(I was in some directory in which I created the file).
Python import errors usually boils down to one of those three cases (whether is is modules you developed; or modules distributed as packages):
You did no install the required package. Googling pytesseracttells me its an OCR that is distributed and installable using Python package manager tool pip by running pip install pytesseract in your favorite shell.
You did install the package, but is is not in your python path.
(Less often) You did install the package, and it is in your python path, but you used a name already in user by Python, and the two are conflicting.
In your case, I strongly think this is the first one. Case 2. and 3. can be assessed by calling python -v your_script.pyas described in this answer.
I had a similar error. So I hope to help people with this kind of problems.
In my case,
I tried to run python code using pytesseract lib on Raspberry pi 3.
$ pip install pillow
$ pip install pytesseract
(followed by https://www.pyimagesearch.com/2017/07/10/using-tesseract-ocr-python/)
and then, I made an example.py to test.
example.py
try:
import Image
except ImportError:
from PIL import Image
from pytesseract import *
print(pytesseract.image_to_string(Image.open('YOUR_IMAGE_PATH')))
and then, when I run this code, I got below error like you.
ImportError: No module named pytesseract
After I saw the #Bertrand Caron's answer, I found a solution.
My problem was package library path.
I also have multiple versions of python, 2.7 and 3.5, like a writer.
When I run command $python --version on linux, the result is Python 2.7.13.
In my case, when I installed a pytesseract package,
it was stored in "/usr/local/lib/python3.5/dist-packages/pytesseract".
And When I ran $python -v example.py, I found the referenced packages path hadn't been same with upper pytesseract package directory.
cf.
installed pytesseract path : /usr/local/lib/python3.5/dist-packages/pytesseract
actual referenced lib path, when running : /usr/lib/python2.7/dist-packages/
So, I copied pytesseract, located in "/usr/local/lib/python3.5/dist-packages/pytesseract" to "/usr/lib/python2.7/dist-packages/"
Then, Solved!
In my case, I was running it in Jupyter so I used this command,
! pip install --user pytesseract
But I forgot to restart the kernal. You need to restart the kernal after installing the pakcage
I had the same error. My solution is
$ pip3 install pytesseract
since I have python 2 and python 3 installed together.

Categories