Can't import PIL after installing Pillow with Poetry - python

I'm trying to install and use Pillow with Python 3.9.2 (managed with pyenv). I'm using Poetry to manage my virtual environments and dependencies, so I ran poetry add pillow, which successfully added Pillow = "^8.2.0" to my pyproject.toml. Per the Pillow docs, I added from PIL import Image in my script, but when I try to run it, I get:
File "<long/path/to/file.py>", line 3, in <module>
from PIL import Image
ModuleNotFoundError: No module named 'PIL'
When I look in the venv Poetry is creating for me, I can see a PIL directory (/long/path/lib/python3.9/site-packages/PIL/) and an Image.py file inside it.
What am I missing here? I've tried:
Downcasing to from pil import Image per this; did not work
Downgrading to lower versions of Python and PIL; works, but defeats the purpose
ETA: Exporting a requirements.txt file from Poetry, creating a virtualenv with venv, and installing the packages manually; works, but cuts me off from using Poetry/pyproject.toml
Any help would be tremendously appreciated.

I couldn't find a way to solve this either (using poetry 1.1.13).
Ultimately, I resorted to a workaround of poetry add pillow && pip install pillow so I could move on with my life. :P
poetry add pillow gets the dependency in to the TOML, so consumers of the package should be OK.

capitalizing "Pillow" solved it for me:
poetry add Pillow

Related

No module named 'PIL' after pip installing it

I'm trying to use Pillow in my python(tkinter) project. I used pip install Pillow to install the package.
When I type from PIL import ImageTk, Image and try to run my code I get the error ModuleNotFoundError: No module named 'PIL. I tried several things like updating pip, uninstalling Pillow and reinstalling it again, using different commands to install it etc.
I don't know what to do now since I never had this problem before. Does anybody know how to fix this?

cannot import Image from PIL in Python 3.8

I am using macOS and installed pillow in the terminal with code pip3 install pillow and I got the version 8.0.1.
But I cannot import it in Pycharm. I typed from PIL import Image and an error message show ModuleNotFoundError: No module named 'PIL' . then I changed PIL to pillow from pillow import Image, it works but cannot find Image class.
How can I fix it?
Is very likely that your Pycharm has created a virtual environment for the project and is not using the same library as the python on the system.
If that is the case, go to Settings > Project > Python Interpreter, press on the plus sign (+), write Pillow on the search bar, select the right package, and add it to your interpreter running on PyCharm.
I recommend the use of the pipenv tool to manage your project dependencies for each project. It helps you to maintain the project dependencies separated from the system or other projects.
Pillow not pillow.
run pip uninstall pillow then run pip install Pillow.
Not sure if the solutions given have worked for everyone but try but writing the imports out separately. I tried everything and by accident this worked. So try as below, I hope this works for anyone who needs the help
from PIL import Image
from PIL import ImageTk

Import working in Python shell, but not when run from file

I'm trying to get JPEG images to work with Tkinter, so I decided to use the PIL package. I've imported PIL in order to do this. However, this will only work when I launch the python shell. If I run an import in a .py file, then run that file in command line, the error thrown is -
"line 1, in <module>
from PIL import Image
ImportError: No module named PIL"
I've seen a lot about different ways of declaring PIL between that and Pillow. I've attempted to declare the import both ways, neither of which work until I use python in the command shell. I have also ensured my PIL is compatible with my version of Python(3.7), Pillow (5.4.1). I have also uninstalled and reinstalled. Has anyone ever encountered something like this? There's probably a very simple solution but I cant find it anywhere.
If you used pip on python 3.x to install pillow ensure that you have a shebang as the first line of your code to ensure the interpreter knows what version to use:
#!/usr/bin/env python3
Additionally, are you making sure to run the python file using the right python version? So if you installed pillow with python3 -m pip install pillow then you should make sure that you are running your file with python3 [filepath]
you have to frist install module "PIL",for installing in your command prompt type
pip install PIL

How do I add cv2 as a requirement in a python package?

I'm trying to make my own package which uses OpenCV Python module cv2. However when using PyCharm, it warns that the
Package requirement is not satisfied.
I suspect this is because I used the recommended method of copy/pasting cv2.pyd into my python dir. Note that pip install cv2 doesn't work.
What's the right method to ensure that requirements are met when this package is brought in?
EDIT:
My setup.py file is as follows
from setuptools import setup
setup(name='image_processing',
version='0.1',
install_requires=['numpy', 'scipy', 'cv2'],
description='Collection of useful image processing functions',
url='',
author='Bill',
license='MIT',
packages=['image_processing'],
zip_safe=False)
This is where the error shows up when trying to package my code. Normally I have no issues importing numpy or cv2. I installed Numpy using pip, and cv2 via the method mentioned above. Everything works if I just run scripts using cv2, but it's this packaging that's tricking me up.
You need to add opencv-python to requirements.
The package is here: https://pypi.python.org/pypi/opencv-python
Then, your requirements.txt file is OK, but sometimes PyCharm keeps whining. If this is the case, then next to the "Install requirement" label there is "Ignore requirement". The latter is your way to go.
(current - 2018.2 PyCharm version does not complain about cv2 when opencv-python is specified in requirements)
You have to specify the version of the opencv python package.
"pip install opencv-python==2.4.9"
Find the proper version from here.
If you want to install latest opencv python package, use
"pip install opencv-python"

Problems with installation of Pillow

I am running Python 2.7.1.1 with Anaconda2 4.0.0 64-bit on a Windows 7 machine. I'm trying to install Pillow for imaging, and after having read through every thread I could find, I am still unable to reach a solution. I have installed and uninstalled Pillow through various means including:
pip install Pillow
conda install Pillow
easy_install Pillow
I've gone to the Anaconda site-packages list and, lo and behold, the package for Pillow-3.2.0-py2.7.egg-info exists.
I've tried importing the package through both:
import Image
from PIL import Image
But I encounter the following ImportError:
from PIL import Image
ImportError: No module named PIL
I've already uninstalled the original PIL library that I tried to install to ensure that only the Pillow package exists. Any help would be greatly appreciated!
It sounds like Anaconda isn't playing nice with your system due to the fact that you have two interpreters installed (Anaconda's and Python 2.7.1.1). I would remove everything (Python, Anaconda, etc) and either reinstall Anaconda fresh, or the get the newest version of Python from python.org (2.7.12).
Personally, I'd go for the Python 2.7.12 from python.org (I've always had issues with prepackaged distros like Anaconda).
If you go that route, after your environment is clean I would make sure Pip is all up to date (pip install pip --upgrade) and then install Pillow from whl file provided by the University of California Irvine.
To do that, just go here:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
Download the Pillow whl file for Windows 64bit. Make sure Python is set up in your path and then just go to the directory where you downloaded Pillow and enter the following (replacing the filename with the one you downloaded):
pip install pillowfile.whl
good luck, and happy coding!
If you can't import a package into Python but it is definitely there in the site-packages folder, then you are more than likely running the wrong Python interpreter.
You can check this by running python from the command line and then entering:
import sys
sys.executable
That will return a string pointing to the current running Python interpreter.
Wrong Python
If that doesn't point to your Anaconda installation then you have a paths problem.
On windows you can set the PATH through My Computer / Properties / Advanced. Look at the Environment Variables and ensure that the Anaconda path string is before any other Python path (If the Anaconda path isn't there, then something is very messed up and you may want to simply re-install Anaconda).
Right Python
If the path returned by sys.executable is correct then the installation of Pillow must be broken somehow. You can try un-installing and then re-installing. As an absolute last-resort you could also try deleting the Pillow folder manually and re-installing it.

Categories

Resources