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

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

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.

XLS2XXLSX: No module named 'currency-symbols'

I'm trying to use the xls2xlsx module to convert several .xls files to .xlsx format, but I get the following error message:
ModuleNotFoundError: No module named 'currency-symbols'
The code:
import os
from xls2xlsx import XLS2XLSX
path = r'./ammcfiles'
p = os.listdir(path)
for f in p:
if f.endswith('.xlsx'):
x2x = XLS2XLSX(f)
x2x.to_xlsx(f)
I tried pip installing the module, but it didn't solve the problem.
My Python version is 3.10.4.
Note: More of the stacktrace would have been helpful to see the full issue.
Had a similar issue with a script that was developed and previously run with Python 3.6.
Running the script in Python 3.10 returned the following error:
Traceback (most recent call last):
File "<virtual env>/lib/python3.10/site-packages/xls2xlsx/htmlxls2xlsx.py", line 37, in
import currency_symbols.constants as currency_symbols_constant
ModuleNotFoundError: No module named 'currency_symbols.constants'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/script/./script.py", line 20, in
from xls2xlsx import XLS2XLSX
File "<virtual env>/lib/python3.10/site-packages/xls2xlsx/init.py", line 3, in
from .htmlxls2xlsx import HTMLXLS2XLSX
File "<virtual env>/lib/python3.10/site-packages/xls2xlsx/htmlxls2xlsx.py", line 40, in
currency_symbols_constants = importlib.import_module('currency-symbols.constants')
File "/usr/lib/python3.10/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'currency-symbols'
Investigation
Ensure the packages are installed
python -m pip install xls2xlsx currency-symbols
Lines 36-40 of <virtual env>/lib/python3.10/site-packages/xls2xlsx/htmlxls2xlsx.py
try:
import currency_symbols.constants as currency_symbols_constants
except Exception:
import importlib
currency_symbols_constants = importlib.import_module('currency-symbols.constants')
This code seems to be responsible for loading the currency-symbols module regardless of the Python version and by extension the module version.
Therefore, the original error was received because both import attempts failed.
<virtual env>/lib/python3.10/site-packages/currency_symbols/ contains the file
_constants.py and not constants.py.
Note the folder is currency_symbols and not currency-symbols, underscore (_) vs dash (-). Therefore, htmlxls2xlsx.py is using the new currency-symbols module name currency_symbols but not the new name of the constants sub module _constants
Fix
Edit htmlxls2xlsx.py to use _constants.py
try:
import currency_symbols._constants as currency_symbols_constants
This fixes the import and usage issues.
As Yuli L mentioned, inside the htmlxls2xlsx.py which is in xls2xlsx module directory I found next:
import currency_symbols.constants as currency_symbols_constants
And change by (take care with typos):
import currency_symbols._constants as currency_symbols_constants

Python script using PIL works in IDE doesn't when run by cmd and gives 'PIL' has no attribute 'Image'

When I run the following in the Spyder IDE on Win10:
import PIL
import os
Inboundfile = "C:/Test/test/Overlay.png"
image = PIL.Image.open(os.path.abspath(Inboundfile))
image.show()
It works fine. Opens the picture and outputs:
runfile('C:/Users/bob/Anaconda3/envs/NewEnvironment/bob/test3.py', wdir='C:/Users/bob/Anaconda3/envs/NewEnvironment/bob')
But when I call the same script from a cmd prompt:
C:\Users\bob\Anaconda3\envs\NewEnvironment\bob>python test3.py
It results in:
Traceback (most recent call last):
File "test3.py", line 14, in <module>
image = PIL.Image.open(os.path.abspath(Inboundfile))
AttributeError: module 'PIL' has no attribute 'Image'
I have verified that the environment path is pointing to the same python environment as the IDE. ( I think ) Here is my Path variable from OS
C:\Users\bob\Anaconda3\envs\NewEnvironment\
It's probably something very simple but I'm very new at this and any help would be appreciated.
I tried adding the line:
from PIL import Image
It still runs fine in the IDE but the error message changes to:
C:\Users\bb\Anaconda3\envs\NewEnvironment\bob>python test3.py
Traceback (most recent call last):
File "test3.py", line 11, in <module>
from PIL import Image
File "C:\Users\bob\Anaconda3\envs\NewEnvironment\lib\site-packages\PIL\Image.py", line 69, in <module>
from . import _imaging as core
ImportError: DLL load failed: The specified module could not be found.
This is even more confusing as I can follow that path and see Image.py sitting in the directory. What is the IDE doing that CMD is not?
All of my problems vanished when I simply closed and restarted the console I was using to call the python script. I have learned that the console saves things like the path variables when it starts up and must be restarted to get changes.

Matplotlib with Google App Engine local development server

I want to use matplotlib in my Google App Engine project. I followed the steps, described here in the official docs. What I did:
1) Created a directory named lib in my application root directory.
2) Created a file appengine_config.py in my application root directory and added there these lines:
from google.appengine.ext import vendor
vendor.add('lib')
3) Since the docs say, that the only version of matplotlib working is 1.2.0, I executed the following command in the Terminal:
pip install -t lib matplotlib==1.2.0
There is also step 0 in the docs, which says
Use pip to install the library and the vendor module to enable importing packages from the third-party library directory.
But I don't understand what it actually means. If this is something essential, please, explain to me what it's meant here. I found this answer here on stackoverflow, and It seems there is nothing different from what I have done.
Also, I added
libraries:
- name: matplotlib
version: "1.2.0"
to app.yaml.
So, after all these steps I add the line
import matplotlib
to main.py and start a local server with
python ~/path/google_appengine/dev_appserver.py app.yaml
But when I try to access http://localhost:8080/, the error is raised:
raise ImportError('No module named %s' % fullname)
ImportError: No module named _ctypes
The whole output, if needed, looks like this:
ERROR 2016-08-11 16:26:51,621 wsgi.py:263]
Traceback (most recent call last):
File "/home/magnitofon/Загрузки/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/home/magnitofon/Загрузки/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/home/magnitofon/Загрузки/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/home/magnitofon/realec-inspector/main.py", line 20, in <module>
import matplotlib
File "/home/magnitofon/realec-inspector/lib/matplotlib/__init__.py", line 151, in <module>
from matplotlib.rcsetup import (defaultParams,
File "/home/magnitofon/realec-inspector/lib/matplotlib/rcsetup.py", line 20, in <module>
from matplotlib.colors import is_color_like
File "/home/magnitofon/realec-inspector/lib/matplotlib/colors.py", line 52, in <module>
import numpy as np
File "/home/magnitofon/Загрузки/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 705, in load_module
module = self._find_and_load_module(fullname, fullname, [module_path])
File "/home/magnitofon/Загрузки/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 446, in _find_and_load_module
return imp.load_module(fullname, source_file, path_name, description)
File "/usr/local/lib/python2.7/dist-packages/numpy/__init__.py", line 180, in <module>
from . import add_newdocs
File "/usr/local/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module>
from .type_check import *
File "/usr/local/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "/usr/local/lib/python2.7/dist-packages/numpy/core/__init__.py", line 22, in <module>
from . import _internal # for freeze programs
File "/usr/local/lib/python2.7/dist-packages/numpy/core/_internal.py", line 14, in <module>
import ctypes
File "/usr/lib/python2.7/ctypes/__init__.py", line 10, in <module>
from _ctypes import Union, Structure, Array
File "/home/magnitofon/Загрузки/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 963, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named _ctypes
What am I doing wrong?
matplotlib is one of the Google-provided 3rd party libs, so you should be following just the Requesting a library instructions and
not the Installing a library ones.
Sadly they're now both on the same documentation page, called Using Built-in Libraries in Python 2.7 - very confusing for the unaware as the vendoring technique should be used for libraries which are not GAE built-in/provided. Filed Issue 13202.
Note: pay attention to the Using libraries with the local development server section, it applies to matplotlib. You may need to install some packages on your system, but not in the application itself (which could negatively affect your deployment on GAE) - they need to be accessible by the development server, not directly by your application.
Duh, I just noticed the Using matplotlib section, on the same page :)
It mentions:
Note: The experimental release of matplotlib is not supported on the development server. You can still add matplotlib to the
libraries list, but it will raise an ImportError exception when
imported.
Some searching into this issue surfaced this (old) recipe:
http://code.activestate.com/recipes/578393-gae-matplotlib-demo/
be sure to read the comments.
However I tried, I was not able to make it work. It be this approach just does not work with the current version of the app engine sandbox anymore, or I just was not able to follow all the steps in the recipe correctly.
Maybe an even better read are the comments of Matt Giuca (a google engineer who ported matplotlib to work in GAE production env.) and his pull request in github.
https://github.com/matplotlib/matplotlib/issues/1823/
His comment of April 17, 2013 gives a recipe for matching the dev_appserver (version 1.77) to work with the matplotlib locally.
Maybe this helps someone dealing with this issue.

Why can't I import this Zope component in a Python 2.4 virtualenv?

I'm trying to install Plone 3.3rc4 with plone.app.blob and repoze but nothing I've tried has worked so far. For one attempt I've pip-installed repoze.zope2, Plone, and plone.app.blob into a virtualenv. I have this version of DocumentTemplate in the virtualenv's site-packages directory and I'm trying to get it running in RHEL5.
For some reason when I try to run paster serve etc/zope2.ini in this environment way Python gives the message ImportError: No module named DT_Util? DT_Util.py exists in the directory, __init__.py is there too, and the C module it depends on is there. I suspect there's some circular dependency or failure when importing the C extension. Of course this module would work in a normal Zope install...
>>> import DocumentTemplate
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "DocumentTemplate/__init__.py", line 21, in ?
File ".../lib/python2.4/site-packages/DocumentTemplate/DocumentTemplate.py", line 112, in ?
from DT_String import String, File
File ".../lib/python2.4/site-packages/DocumentTemplate/DT_String.py", line 19, in ?
from DocumentTemplate.DT_Util import ParseError, InstanceDict
ImportError: No module named DT_Util
I must say I doubt DocumentTemplate from Zope will work standalone. You are welcome to try though. :-)
Note that DT_Util imports C extensions:
from DocumentTemplate.cDocumentTemplate import InstanceDict, TemplateDict
from DocumentTemplate.cDocumentTemplate import render_blocks, safe_callable
from DocumentTemplate.cDocumentTemplate import join_unicode
You'll need to make sure those are compiled. My guess is that importing the cDocumentTemplate module fails and thus the import of DT_Util fails.

Categories