ImportError: cannot import name _imaging - python

I installed Pillow, and after I want to do:
from PIL import Image
I get the following error:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 61, in <module>
ImportError: cannot import name _imaging
However, if I import these separately, everything is fine, ie:
import _imaging
import Image
Do you know what the problem might be?

I had the same problem and I solved that by upgrading this package using the command below:
pip install -U Pillow

This also happens if you built Pillow in one OS and then copied the contents of site-packages to another one. For example, if you are creating AWS Lambda deployment package, that's the error you will face when running the Lambda function. If that's the case, then Pillow needs to be installed in a Amazon Linux instance and you have to use the resulting site-packages in your deployment package. See instructions and details here:
http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html

I ran into this problem as well. It can happen if you have PIL installed, then install Pillow on top of it.
Go to /usr/local/lib/python2.7/dist-packages/ and delete anything with "PIL" in the name (including directories). If the Pillow .egg file is there you might as well delete that too.
Then re-install Pillow.
substitute "python2.7" for the version of python you're using.

What is your version of pillow?
Pillow >= 2.1.0 no longer supports import _imaging. Please use from PIL.Image import core as _imaging instead. Here's the official documentation.

I have got the same error with Python 3.6. Upgrading Pillow did the job for me.
sudo python3.6 -m pip install Pillow --upgrade
Probably for other python versions use your version instead of 3.6.

This can happen if you're trying to run Pillow installed on a Mac in a Linux environment (for example, e.g. building an AWS Lambda on a Mac then deploying it to a Linux runtime).
To make sure you're installing it for the right platform do the following:
pip3 install --platform manylinux1_x86_64 --only-binary=:all:
The --only-binary=:all: is required when specifying --platform and the platform itself can be found by looking at https://pypi.org/project/Pillow/7.2.0/#files (for example) - the platform is the last part of the filename e.g. win32, manylinux1_x86_64, manylinux1_i686 etc.
This avoids the need to be running Linux to install the Linux build of Pillow.

This may be a niche solution but I was able to fix this problem on Pycharm by going to file->settings->python interpreter and clicking the upgrade symbol next to the pillow package.

For pillow to work PIL must be in /usr/local/lib/python2.7 or3/dist-packages/PIL.py.
In dist-packages PIL.py should have a folder.
sudo apt-get update
pip install Pillow
PIL != PiL

I had the same problem when it tried to deploy a lambda package, the thing is that you have to precompile the package emulating the lambda architecture/runtime that you are going to use, otherwise you'll get cannot import name _imaging. 2 ways of solving this:
1 - spin up an EC2 Amazon Linux instance.( i will only cover this part)
2 - Use dockers.
Short solution
Install Python 3 in Amazon Linux 2 intance. (Must be python3.X you plan to use in lambda)
Install a virtual environment under the ec2-user home directory.
Activate the environment, and then install Boto 3.
Install Pillow
Create a ZIP archive with the contents of the library(PIL and Pillow.libs)
Add your function code to the archive.
Update your the lambda.(AWS CLI)
Long solution
If Python 3 isn't already installed, then install the package using the yum package manager.
`$ sudo yum install python3 -y`
Create a virtual environment under the ec2-user home directory
The following command creates the app directory with the virtual environment inside of it. You can change my_app to another name. If you change my_app, make sure that you reference the new name in the remaining resolution steps.
`$ python3 -m venv my_app/env`
Activate the virtual environment and install Boto 3
Attach an AWS Identity and Access Management (IAM) role to your EC2 instance with the proper permissions policies so that Boto 3 can interact with the AWS APIs. For other authentication methods....For a quick use you can set your credential using $ aws confifure see documentation ( you will need this in step 7)
3.1 Activate the environment by sourcing the activate file in the bin directory under your project directory.
`$ source ~/my_app/env/bin/activate`
3.2. Make sure that you have the latest pip module installed within your environment.
$ pip install pip --upgrade
3.3 Use the pip command to install the Boto 3 library within our virtual environment.
`pip install boto3`
Install libraries with pip.
$ pip install Pillow
4.1 Deactivate the virtual environment.
`$ deactivate`
Create a ZIP archive with the contents of the library.
change directory to where pip is installes. it should be something like /my_app/env/lib/python3.x/site-packages.
IMPORTANT: the key here is to zip the file inside site-packages into
your lambda.(i only used PIL and Pillow.libs to save space but you can
zip everything)
5.1 ZIP everything thats inside the PIL folder.
`zip -r9 PIL.zip ./PIL/`
add the Pillow.libs to your ZIP
`zip -gr PIL.zip Pillow.libs`
Add your function code to the archive.
you can do this in the console if it just on file of code, but i recomend doing it in this step.If you don't have your code,just create a file using vi or nano and save it with the name that your lambda handler will use (in this case will use lambda_function.py).
`zip -g PIL.zip lambda_function.py`
Update your the lambda.(AWS CLI)
if you haven't create a lambda function,do it now before updating the function from the aws cli, make sure that you have the right permission to update lambda from the aws cli.
change LAMBDAFUNCTIONNAME for your function name
aws lambda update-function-code --function-name LAMBDAFUNCTIONNAME P --zip-file fileb://PIL.zip
Getting out of the first loop of hell
go to your lambda console and test your code, make sure you use the same runtime/python version you used in the EC2 instance

Quick solution - import PyQt5 as well,
you will not get that error message.
import PyQt5
from PIL import ImageGrab

As some other answers have alluded to, this can happen when you build Pillow on MacOS and try to import PIL in another OS like some Amazon Linux flavor.
My exact use-case was to package imagehash as a Lambda layer which includes pillow as a dependency. The following guideline has worked great for me for all python packages.
Install the SAM CLI SAM Installation
Create your python script with the lambda handler defined
Create your template.yml file with your Lambda function defined. Your CodeUri should be the relative path to your python script.
Add the package you are trying to create a layer for to your requirements.txt.
Run the following SAM command sam build -t path_to_template
You will now have the following directory .aws-sam/build/{Logical ID Of Lambda Function}. Inside you will see that your python packages and their dependencies have been installed just as if you ran pip download package and unzipped the wheel files.
Now, the python files have been prepped by SAM specifically for Lambda and you can continue with creating your Lambda Layer as desired. Configuring Lambda Layers
Since I use AWS SAM CLI already for running Lambda functions locally, this has been the easiest method for me to create my layers.

Just uninstall pillow:
pip uninstall pillow
then install pillow again:
pip install pillow
works great

I'm using Flask with Google App Engine. I have the module Pillow installed via this command:
pip install -t lib pillow
I fixed this error by defined PIL in my app.yaml file:
libraries:
- name: PIL
version: latest

Solution
pip uninstall PIL
pip uninstall Pillow
pip install Pillow

Related

python - pip: What are the files installed exactly?

I am trying to upgrade a python package called "bokeh", that is containing an "examples" directory in its ditribution files:
Here the link to the distribution file:
https://pypi.python.org/pypi/bokeh/0.12.10
And here an image showing the content of the tar.gz file:
I can see the "examples" directory that i am after is present.
However if i pip install --upgrade bokeh, only the directory "bokeh" (the first one in the image) is installed on my machine.
How can i have this "example" directory to be install as well eventually?
It is meant that way because of the sample data size needed for the examples provided. This is from the documentation:
Some of the Bokeh examples rely on sample data that is not included in
the Bokeh GitHub repository or released packages, due to their size.
Once Bokeh is installed, the sample data can be obtained by executing
the following command at a Bash or Windows prompt:
bokeh sampledata
or, run this in your python interpreter:
import bokeh.sampledata
bokeh.sampledata.download()
Normal pip installations are not supposed to be "looked into" by the user. Depending on your system they might end up in
/usr/local/lib/python2.7/site-packages
or
$PYTHON_HOME/lib/python2.7/site-packages
or somewhere entirely else.
Instead you should Git clone the repo (or download the tarball from there) and install the directory (an editable install)
git clone https://github.com/bokeh/bokeh.git
pip install -e bokeh/
that way the library you are using is directly next to the examples you are using:
import bokeh
directly imports the code you just downloaded.
Alternatively you can install bokeh from PyPI and just git clone or download the repository to get the examples. But you should be aware that
import bokeh
doesn't import the library you just downloaded, but the one you installed earlier.

Unable to use azure SDK in Python

I did this:
sudo pip install azure azure-storage azure-servicebus azure-mgmt azure-servicemanagement-legacy
from azure import *
Traceback (most recent call last): File "", line 1, in
ImportError: No module named azure
from azure.storage import BlobService
Traceback (most recent call last): File "", line 1, in
ImportError: No module named azure.storage
Python package installed thru cmd sudo pip install exists at the paths /usr/lib/python2.7, /usr/local/python2.7, etc and their sub-folder dist-packages.
You can code import sys and print sys.path in the Python Interpreter to show the completed path list for current python environment.
Iif you installed successfully some packages like azure & azure-storage, you can find these files relate to the package in the python library paths.
However, you got the error in Import Error: No module named <package-name> when you run the code import <package-name> or from <package-name> import <class or object name>. There are two scenes that would be cause the issue normally.
Package not installed successfully.
The library path included package that not exists in Python system environment path sys.path in python or PYTHONHOME in environment variables.
So I think you can try to solve the issue thru three ways below.
Dynamically add the package path into sys.path thru the method sys.path.append('<package path>') in python code.
Check the environment variable PYTHONHOME whether or not set up. If set up PYTHONHOME, python will add package path based on PYTHONHOME into sys.path.
If your python environment encounter some unknown fault that is unrecoverable, you can try to reinstall Python thru commands sudo apt-get remove python python-pip, sudo apt-get update, sudo apt-get install python python-pip on Ubuntu. It's a simple way.
BlobService belongs to azure.storage.blob rather than the azure.storage
it should rather be
from azure.storage.blob import BlobService
Link - https://azure.microsoft.com/en-us/documentation/articles/storage-python-how-to-use-blob-storage/
If it still doesn't work for you, you might would like to use virtualEnv and do the pip install again while in virtualenv
http://docs.python-guide.org/en/latest/dev/virtualenvs/
I had very similar issue. There was a lot of confusion between python2 and python3 package versions as there was no virtual env used and I also had to ungrade pip to 18.
But anyway, this is is how I resolved the part in question.
Locate where the package was installed:
pip show azure
The output will show the location of the package in the Location section:
Name: azure
Version: 4.0.0
Summary: Microsoft Azure Client Libraries for Python
Home-page: https://github.com/Azure/azure-sdk-for-python
Author: Microsoft Corporation
Author-email: azpysdkhelp#microsoft.com
License: MIT License
Location: /usr/local/lib/python3.6/dist-packages
Requires: azure-servicefabric, azure-cosmosdb-table, azure-datalake-store, azure-loganalytics, azure-eventgrid, azure-servicemanagement-legacy, azure-servicebus, azure-graphrbac, azure-storage-blob, azure-mgmt, azure-storage-file, azure-batch, azure-applicationinsights, azure-keyvault, azure-storage-queue
Required-by:
If you do:
python -c "import sys;print(sys.path)"
You will see a list of pip package locations:
['/app', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
At the begining of my python file I added:
sys.path.insert( 0, '/usr/local/lib/python3.6/dist-packages' )
This will make sure this package location will be checked in the first place.
UPDATE
Thinking about it in the morning, things opened from a new perspective for me. I saw, that I had #!/usr/bin/python at the beginning of my python file, which says to use the wrong interpreter and look for pip packages in the wrong place.
azure metapackage is deprecated
and azure-storage is not being maintained anymore
Please use azure-storage-blob >= 12.0
pip install azure-storage-blob
from azure.storage.blob import BlobServiceClient
PS: I write SDKs for azure

Installing GDAL Python binding in Ubuntu to use as standalone module

I am trying to export a GeoTiff with Blender using the Blender Python API (based on Python 3), so I've decided to install GDAL on Ubuntu (14.04). What I would like is to get the module as a standalone folder that I could put in the modules directory of Blender (/home/user/.config/blender/2.73/scripts/modules).
The thing is I've run through several different problems trying to install GDAL. I've tried to install from source (for GDAL 2.0.0) here : Official PyPi Gdal
I ran sudo apt-get install libgdal-dev gdal-bin (I list it here because it may be important)
When I am in the extracted GDAL folder, using python setup.py build & python setup.py install, the library installs to /usr/local/lib/python2.7/dist-packages/osgeo. However, when I run python from command line, running from osgeo import osr returns ImportError: No module named _gdal
Following GDAL via pip , I used pip (pip install GDAL) to install the library, and the folder it went to was /usr/lib/python3/dist-packages/osgeo (using pip show ...). Again, running python3 and trying to import results in the same error. Of course, when I copy-paste each folder in the blender module directory, I get the same error in the Blender Python console.
So I decided to compile the sources using ./configure --with-python & make & make install in the source folder. I then copied the folder GDAL-x.x.x/build/lib.linux-x86_64-3.4/osgeo to the blender modules directory and got this time the error when importing : ImportError: /home/yvesu/.config/blender/2.73/scripts/modules/osgeo/_gdal.so: undefined symbol: _Py_ZeroStruct.
Trying to compile with python3 using python3 setup.py build returns the error error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
EDIT 1:
I think I've found the solution : I went to the directory swig/python (not found in a GDAL-1.11.0 folder but gdal-1.11.0 fodler, can't remember where I downloaded it from), ran python3 setup.py build & python3 setup.py install and could finally find the folder in /usr/local/lib/python3.4/dist-packages/GDAL-1.11.0-py3.4-linux-x86_64.egg/osgeo. When I put this osgeo folder oni the Blender modules directory, I was able to import osgeo in Blender. I will report if anything went wrong.
I think I've listed all my attempts at installing GDAL on Ubuntu. Can anyone point me in the right direction? Do you think it is even possible to install it as a standalone module, or do I need linked libraries through LD_LIBRARY_PATH?
Here is the solution I've found :
Download Gdal sources (v2.0.0 is the current stable release) from ftp://ftp.remotesensing.org/gdal/2.0.0/ or http://download.osgeo.org/gdal/2.0.0/ and untar
Go to the directory gdal2.0.0/swig/python
Run python3 setup.py build & python3 setup.py install
Finally find the module folder in, on Ubuntu : /usr/local/lib/python3.4/dist-packages/GDAL-2.0.0-py3.4-linux-x86_64.egg/osgeo
I can now use it in Blender (copying in the modules directory)

How to change default install location for pip

I'm trying to install Pandas using pip, but I'm having a bit of trouble. I just ran sudo pip install pandas which successfully downloaded pandas. However, it did not get downloaded to the location that I wanted. Here's what I see when I use pip show pandas:
---
Name: pandas
Version: 0.14.0
Location: /Library/Python/2.7/site-packages/pandas-0.14.0-py2.7-macosx-10.9-intel.egg
Requires: python-dateutil, pytz, numpy
So it is installed. But I was confused when I created a new Python Project and searched under System Libs/lib/python for pandas, because it didn't show up. Some of the other packages that I've downloaded in the past did show up, however, so I tried to take a look at where those were. Running pip show numpy (which I can import with no problem) yielded:
---
Name: numpy
Version: 1.6.2
Location: /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Requires:
Which is in a completely different directory. For the sake of confirming my error, I ran pip install pyquery to see where it would be downloaded to, and got:
Name: pyquery
Version: 1.2.8
Location: /Library/Python/2.7/site-packages
Requires: lxml, cssselect
So the same place as pandas...
How do I change the default download location for pip so that these packages are downloaded to the same location that numpy is in?
Note: There were a few similar questions that I saw when searching for a solution, but I didn't see anything that mentioned permanently changing the default location.
According to pip documentation at
http://pip.readthedocs.org/en/stable/user_guide/#configuration
You will need to specify the default install location within a pip.ini file, which, also according to the website above is usually located as follows
On Unix and Mac OS X the configuration file is: $HOME/.pip/pip.conf
On Windows, the configuration file is: %HOME%\pip\pip.ini
The %HOME% is located in C:\Users\Bob on windows assuming your name is Bob
On linux the $HOME directory can be located by using cd ~
You may have to create the pip.ini file when you find your pip directory. Within your pip.ini or pip.config you will then need to put (assuming your on windows) something like
[global]
target=C:\Users\Bob\Desktop
Except that you would replace C:\Users\Bob\Desktop with whatever path you desire. If you are on Linux you would replace it with something like /usr/local/your/path
After saving the command would then be
pip install pandas
However, the program you install might assume it will be installed in a certain directory and might not work as a result of being installed elsewhere.
You can set the following environment variable:
PIP_TARGET=/path/to/pip/dir
https://pip.pypa.io/en/stable/user_guide/#environment-variables
Open Terminal and type:
pip config set global.target /Users/Bob/Library/Python/3.8/lib/python/site-packages
except instead of
/Users/Bob/Library/Python/3.8/lib/python/site-packages
you would use whatever directory you want.
Follow these steps
pip config set global.target D:\site-packages to change install path
or py -m pip config --user --editor notepad edit
[global]
target = D:\site-packages
set environment variable to use download import xxx
PIP_TARGET=site-packages
PYTHONPATH=site-packages
3.pip config unset global.target, to upgrade pip py -m pip install --upgrade pip
#Austin's answer is outdated, here for more up-to-date solution:
According to pip documentation at
https://pip.pypa.io/en/stable/topics/configuration/
You will need to specify the default install location within a configuration file, which, also according to the website above is usually located as follows
Mac OS
$HOME/Library/Application Support/pip/pip.conf if directory $HOME/Library/Application Support/pip exists else $HOME/.config/pip/pip.conf.
The legacy “per-user” configuration file is also loaded, if it exists: $HOME/.pip/pip.conf.
The $HOME folder can be located by navigating to ~/ (cmd+shift+G in Finder; cmd+shift+. to show hidden files).
Windows
%APPDATA%\pip\pip.ini
The legacy “per-user” configuration file is also loaded, if it exists: %HOME%\pip\pip.ini
The %HOME% is located in C:\Users\Bob on windows assuming your username is Bob
Unix
$HOME/.config/pip/pip.conf, which respects the XDG_CONFIG_HOME environment variable.
The legacy “per-user” configuration file is also loaded, if it exists: $HOME/.pip/pip.conf.
On linux the $HOME directory can be located by using cd ~
You may have to create the configuration file when you find your pip directory. Put something like
[global]
target = /Library/Frameworks/Python.framework/Versions/Current/lib/python3.10/site-packages/
if you are on a Mac. Except that you would replace /Library/Frameworks/Python.framework/Versions/Current/lib/python3.10/site-packages/ with whatever path you desire. If you are on Linux you would replace it with something like /usr/local/your/path
After saving the command would then be
pip install pandas
However, the program you install might assume it will be installed in a certain directory and might not work as a result of being installed elsewhere.
Please note that
pip3 install pandas
might be the solution if your packages gets installed in the Python2 folder vs Python3.

Install m2crypto on a virtualenv without system packages

I have created a virtual environment without the system packages with python's virtualenv in Ubuntu and installed m2crypto, but when I execute a shell and I try to import M2Crypto i get the following error:
ImportError: /home/imediava/.virtualenvs/myenv/local/lib/python2.7/site-
packages/M2Crypto/__m2crypto.so: undefined symbol: SSLv2_method
From outside the environment I run into the same problem unless from ubuntu I install python-m2crypto with apt-get. I know that I could create the environment with the system packages but I would prefer not to do it.
Is there anyway that I could create a virtual environment without the system packages and then install m2crypto with pip without running into the SSLv2_method?
There seems to be a regression bug from an earlier version of M2Crypto.
After placing M2Crypto's source in your virtualenv, you can try to patch it with the diff code below.
You do this by downloading the source code, untar it via:
tar -xzf M2Crypto-0.21.1.tar.gz
This should create the directory M2Crypto-0.21.1 which will contain the SWIG directory
In SWIG you'll find _ssl.i, which is the file to be patched. In the same directory create a file called _ssl.i.patch for example using the nano editor and paste the complete diff code listed below into it.
Next issue the patch _ssl.i _ssl.i.patch command to merge the patch into the code. (Afterwards you may remove the patch file if you want.)
Finally issue the commands:
python setup.py build
followed by:
python setup.py install
to install manually.
diff code:
--- SWIG/_ssl.i 2011-01-15 20:10:06.000000000 +0100
+++ SWIG/_ssl.i 2012-06-17 17:39:05.292769292 +0200
## -48,8 +48,10 ##
%rename(ssl_get_alert_desc_v) SSL_alert_desc_string_long;
extern const char *SSL_alert_desc_string_long(int);
+#ifndef OPENSSL_NO_SSL2
%rename(sslv2_method) SSLv2_method;
extern SSL_METHOD *SSLv2_method(void);
+#endif
%rename(sslv3_method) SSLv3_method;
extern SSL_METHOD *SSLv3_method(void);
%rename(sslv23_method) SSLv23_method;
You can install this lib in your global environment and then just copy from your global site-packages to virtualenv.
M2Crypto 0.22.3 (the current version in pypi) fixes this problem, so the simplest solution is now simply:
pip install --upgrade M2Crypto
M2Crypto 0.22.3 has been released from martinpaljak's github repository, rather than from the original M2Crypto repository.
I had the same problem with the current release (M2Crypto-0.22.5). The latest RC build worked for me.
pip install M2Crypto==0.22.6rc4
There is a patch slated for 0.26.1.
In the meantime, you can clone the repo, apply the patch, and install from source.
git clone https://gitlab.com/m2crypto/m2crypto.git
(
cd m2crypto
git checkout 0.25.1
curl 'https://gitlab.com/m2crypto/m2crypto/merge_requests/117.diff' | git apply -v
)
pip install -U m2crypto

Categories