Unable to import PIL.Image for qrcode - python

I run into this weird error, I need to use qrcode with pillow, so I did pip install pillow qrcode (after initiating the virtual environment). Then, the following thing happens
>>> from PIL import Image
>>> Image
<module 'PIL.Image' from '/vagrant/env/local/lib/python2.7/site-packages/PIL/Image.pyc'>
>>> import qrcode;
>>> qrcode.make("1").show()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/vagrant/env/local/lib/python2.7/site-packages/qrcode/main.py", line 8, in make
return qr.make_image()
File "/vagrant/env/local/lib/python2.7/site-packages/qrcode/main.py", line 186, in make_image
from qrcode.image.pil import PilImage
File "/vagrant/env/local/lib/python2.7/site-packages/qrcode/image/pil.py", line 5, in <module>
import Image
ImportError: No module named Image
from PIL import Image works but qrcode doesn't work. Not sure what is going on

install PIL on Ubuntu:
https://askubuntu.com/questions/156484/how-do-i-install-python-imaging-library-pil
header for your program, code:
##some magic for 3rd party packages
import site
site.main()
##headers
from PIL import Image
##programm
#here your program!!!
--
Your friend!!!

Related

moduleerror pil not found

when i run my program in python it works but when i use pyinstaller it doesnt work. my app (login.py) imports another file (app.py) which imports PIL
even though i use hidden import to import PIL, i get this when i launch the exe
Traceback (most recent call last):
File "login.py", line 5, in
from app import App
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "app.py", line 2, in
from PIL import Image, ImageTk
ModuleNotFoundError: No module named 'PIL'
can anyone please help
You should use import Image or from PIL import Image instead of import PIL, as you can check in this answer.

Python dev_appserver cant import `.so` file (Pillow)

Inside my GAE application I try this:
from PIL import Image
And get this:
Traceback (most recent call last):
...
File "/home/sheena/Workspace/Waxed/code/waxed_backend/src/waxed_backend/concerns/misc/views.py", line 57, in home
from PIL import Image
File "libs/PIL/Image.py", line 56, in <module>
from . import _imaging as core
File "/home/sheena/Workspace/Waxed/venvs/wxt_comp/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 1024, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named PIL._imaging
/path/to/libs/PIL/imaging.so exists. And I have no problem importing anything else from that libs file from within dev_appserver.
And this works fine:
cd /path/to/libs/
python
>>> from PIL import Image
I can only conclude that either dev_appserver somehow breaks impotrt functionality so that .so files aren't recognised.
Has anyone else seen this before? Any idea how to fix it?
_imaging is a PIL dependency written in C. Because it's C and not Python, including the library in your lib folder won't work. You need to define that library in your app.yaml file:
libraries:
- name: PIL
version: latest

Python importing a specific library only works under home directory

The python interpreter I was using is located to '~/miniconda2/bin/python',2.7.11.
The problem is as follows:
If the current directory is home directory, I start the python in command line and import skimage.filters library. It works perfectly.
Name:~ Name $ python
>>> import skimage.filters
>>>
But when I tried to cd to another directory and did the same thing. I would get a importError as follows:
Name:~ Name $ cd dir/
Name: dir Name $ python
>>> import skimage.filters
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Name/miniconda2/lib/python2.7/site-packages/skimage/filters/__init__.py", line 1, in <module>
from .lpi_filter import inverse, wiener, LPIFilter2D
File "/Users/Name/miniconda2/lib/python2.7/site-packages/skimage/filters/lpi_filter.py", line 7, in <module>
from scipy.fftpack import ifftshift
File "/Users/Name/miniconda2/lib/python2.7/site-packages/scipy/fftpack/__init__.py", line 95, in <module>
from .basic import *
File "/Users/Name/miniconda2/lib/python2.7/site-packages/scipy/fftpack/basic.py", line 12, in <module>
from . import _fftpack
ImportError: cannot import name _fftpack
>>>
It's really odd because the paths of the python interpreter and the sys.path of them are the same. I also tried os.chdir('home directory') to make sure the os.getcwd() of both are the same as well. I tried to update the libraries and reinstall them. But the problem is still there.
Solved by reinstalling Python.

Python import problems with image2gif

I know I have all packages installed (pip freeze)
I am on a Windows 10 machine and am running python3.4 and am having weird dependency problems
test.py:
from images2gif import writeGif
from PIL import Image, ImageSequence
import os
file_names = ['output\donkey-1.png', 'output\donkey-2.png']
images = [Image.open(fn) for fn in file_names]
size = (600,350)
for im in images:
im.thumbnail(size, Image.ANTIALIAS)
filename = "test.gif"
writeGif(filename, images, duration=0.5, subRectangles=False)
running test.py gives the following errors, could not find this error anywhere else on the web
Traceback (most recent call last):
File "test.py", line 2, in <module>
from images2gif import writeGif
File "C:\Python34\lib\site-packages\images2gif\__init__.py", line 1, in <module>
from images2gif import readGif as readGif
ImportError: cannot import name 'readGif'
I just looked at the actual repository source code: it uses Python 2 style relative imports. That is, images2gif does not support Python 3 out of the box.
One solution for now might be to download the sourcefile from pypi, extract that (not sure if Windows likes tar.gz files), cd into the directory and run 2to3 on it:
2to3 -w .
and then install it manually:
python3.4 setup.py install
But probably easier, just seeing the results of 2to3: manually change the two import statements in your already installed package (in __init__.py):
-from images2gif import readGif as readGif
-from images2gif import writeGif as writeGif
+from .images2gif import readGif as readGif
+from .images2gif import writeGif as writeGif
because it appears everything else is PY3K compatible; the dots in front of images2gif in those two import statements make it proper PY3K relative imports.

Python - Cannot import name - 3.4

I am working with numpy and Pillow (replacement for PIL in 3.4), but have been having problems with the importing of Pillow. I found a similar post here:
ImportError: Cannot import name X
However, this post was using his own created libraries and the problem was that his modules imported each other, creating circular dependent imports.
My code however does not use my own module, here is the code:
import PIL
from PIL import ImageGrab
import numpy
img = ImageGrab.grab()
imgLoad = img.load()
size = img.size()
And this then returns the error:
Traceback (most recent call last):
File "E:/Family Documents/Matthew's Documents/Python/PIL.py", line 1, in <module>
import PIL
File "E:/Family Documents/Matthew's Documents/Python\PIL.py", line 2, in <module>
from PIL import ImageGrab
ImportError: cannot import name 'ImageGrab'
One other interesting thing about this is that when I first installed Pillow (PIL), I tried it in the shell and the "from PIL import ImageGrab" worked.
Also, if I restart the shell (close it and re-open it) the commands, being manually typed in, work as well. This suggests to me that something is bugging out with python, since retyping "import PIL" throws the same error message "cannot import name 'ImageGrab'".
Thanks for any help
Ha, this already bit me multiple times.
Your traceback shows the file name:
E:/Family Documents/Matthew's Documents/Python/PIL.py
Your PIL.py is found first, so you're trying to import names from the module that's just executing, not from the actual library you have installed.

Categories