UnsupportedOperation: fileno - How to fix this Python dependency mess? - python

I'm building quite an extensive Python backend and things were working quite good on server A. I then installed the system on a new (development) server B on which I simply installed all pip packages again from scratch. Things seemed to work fine, so I did a pip freeze. I then took that list and upgraded the packages on server A.
But, as you might expect I should have known better. I didn't test things on machine B wel enough, so I ran into a problem with Pillow version 3.0.0. So I downgraded to version 1.7.8. That solves that single problem, bug gives me another one:
File "/home/kramer65/theproject/app/models/FilterResult.py", line 26, in to_json
self.image.save(b, 'JPEG')
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1437, in save
save_handler(self, fp, filename)
File "/usr/local/lib/python2.7/dist-packages/PIL/JpegImagePlugin.py", line 471, in _save
ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 476, in _save
fh = fp.fileno()
UnsupportedOperation: fileno
And here I'm kinda lost. As far as I know this is a problem in Pillow itself, so I wouldn't know why it used to work and why it doesn't work anymore.
I searched around on the internet, but I couldn't find any solution.
Does anybody know what I could do to solve this?
ps. PIL is not installed, so it's not a collision between PIL and Pillow
[EDIT]
I just tested an import Image in Python (which would suggest that PIL is still installed). To my surprise that succeeds, even though pip tells me that it is not installed:
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
>>> exit()
$ sudo pip uninstall PIL
The directory '/home/hielke/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Cannot uninstall requirement PIL, not installed

BytesIO objects raise UnsupportedOperation (rather than AttributeError which StringIO does) when their fileno method is called that exception wasn't handled as it should be.
This was fixed in Pillow 3.0.0 by this commit https://github.com/python-pillow/Pillow/commit/197885164b22f82653af514e66c76f4b778c0b1b by catching the exception. The following is the fix. The rest of that commit are changes to the test suite.
In PIL/ImageFile.py:
## -29,6 +29,7 ##
import Image
import traceback, os
+import io
MAXBLOCK = 65536
## -475,7 +476,7 ## def _save(im, fp, tile):
try:
fh = fp.fileno()
fp.flush()
- except AttributeError:
+ except (AttributeError, io.UnsupportedOperation):
# compress to Python file-compatible object
for e, b, o, a in tile:
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
You could simply patch 1.7.8 to handle the exception.

I finally managed to fix things. The reason I downgraded pillow from 3.0.0 to 1.7.8, is because those where the only two versions I saw listed on the Pillow Pypi package index. I finally remembered that I had one more server on which I once tested this code and there it was still working. A quick pip freeze told me that it had Pillow version 2.3.0 installed. So after installing that on my dev server things worked beautifully again.
So what have I learned from this? Use pip freeze!

Related

Could not find module \atari_py\ale_interface\ale_c.dll (or one of its dependencies)

I'm trying to work with the openai gym module but I get this error:
>>> import atari_py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\ssit5\AppData\Local\Programs\Python\Python38\lib\site-packages\atari_py\__init__.py", line 1, in <module>
from .ale_python_interface import *
File "C:\Users\ssit5\AppData\Local\Programs\Python\Python38\lib\site-packages\atari_py\ale_python_interface.py", line 17, in <module>
ale_lib = cdll.LoadLibrary(os.path.join(os.path.dirname(__file__),
File "C:\Users\ssit5\AppData\Local\Programs\Python\Python38\lib\ctypes\__init__.py", line 451, in LoadLibrary
return self._dlltype(name)
File "C:\Users\ssit5\AppData\Local\Programs\Python\Python38\lib\ctypes\__init__.py", line 373, in __init__
self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'C:\Users\ssit5\AppData\Local\Programs\Python\Python38\lib\site-packages\atari_py\ale_interface\ale_c.dll' (or one of its dependencies). Try using the full path with constructor syntax.
I don't have an ale_c.dll and tried finding solutions but nothing worked.
I followed the solution here https://github.com/openai/gym/issues/1726 but when trying to import atari_py it comes up with the same error. I don't see why the __init__ would search for something that didn't come with the module either. There were other StackOverflow questions that I looked at but they also yielded no results. The only solution I can think of is to get a copy of ale_c.dll but I don't know how I would get it.
I was facing the same error. Fortunately, I was able to find one workaround. Follow this steps and you should be good to go.
Download ale_c.dll from here.
Copy it in C:\Users\Deep Raval\AppData\Local\Programs\Python\Python38\Lib\site-packages\atari_py\ale_interface (Your path can be different).
Try to download arari-py package from conda
conda install -c conda-forge atari_py
this will fix your problem even if you are using Python > 3.7
For anybody else in the future, my problem was that I got the same error but I already had the ale_c.dll file in the right folder.
I followed a solution on this link --> https://github.com/openai/gym/issues/1726
I have tried to make it work with python 3.8 and 3.9 on Windows 10. Installing Python 3.7 and using it as the Python Interpreter on PyCharm resolved the issue. I have searched for the missing file while having 3.8 + version of Python and it did not exist at all. Atari-py is supported only for Python 3.7 (https://github.com/openai/atari-py) so having a higher version of that would not work. It is a bad idea to download files from unlicensed places so I advise you to install the supported version of Python (3.5, 3.6, and 3.7). Even though gym can work on 3.9 the atari version will not.
Just an update because gym 0.21.0 came out which supports ale-py and no longer uses atari-py (so any game marked v0 - v4 will no longer work if you use gym 0.21.0 and ale-py (which is supposed to come with gym). Make sure when you install your libraries that they are actually compatible.
Link to new version of gym https://brosa.ca/blog/ale-release-v0.7. When running v5 I do manage to render the game but I cannot find the other supported games. If you use gym 0.21.0 you will not need atari-py (atari_py).
From the example:
import gym
env = gym.make('ALE/Breakout-v5',
obs_type='rgb', # ram | rgb | grayscale
frameskip=5, # frame skip
mode=0, # game mode, see Machado et al. 2018
difficulty=0, # game difficulty, see Machado et al. 2018
repeat_action_probability=0.25, # Sticky action probability
full_action_space=True, # Use all actions
render_mode=None # None | human | rgb_array
)
it does actually run but the game is broken even in older versions of gym.
What I managed to get working after trying various python, gym and atari-py versions is:
I would suggest to the downloade the wheel file for you Windows and Python version from here: https://pypi.org/simple/atari-py/
and then execute pip install on this file like:
pip install atari_py-0.2.9-cp39-cp39-win_amd64.whl
This one helped for me (solution from github seems much much better, but it was not working for me - maybe because of venv? ) - for me it's a more safe solution than downloading a .dll from google drive.
This is a problem that you may face when using Windows.
Uninstall and reinstall atari using:
pip install -f https://github.com/Kojoley/atari-py/releases atari_py

How to reuse modules across Python installs?

I am trying to import a python (2.7.5) module but I'm not sure if I am going at it in the right way. I usually work in Jupyter Notebook (in a seperate Conda env) to keeps things organized per project. Now I am trying to import a module called otbApplication which are Python bindings for a GIS program called Orfeo Toolbox. The thing is, Orfeo Toolbox (together with QGIS) comes with its own Python install (and subsequent paths) and even its own CMD prompt (assuming you use OSGEO4W). If I use this CMD prompt to start Python and import otbApplication, it works fine. But I want to install more packages and just work within my own 'usual' environment (Jupyter Notebook) in this case.
How should you normally reuse modules between Python installations?
I already tried placing a .pth txt file containing the path to the module in one of the sys.path locations of a different Python installation but it wouldn't even find it. I tried to force it by hosting a notebook (with the same Python install) in the same folder as the module and then importing it. I got the following error which makes me question if I am going at this the wrong way:
Python 2.7.5 |Continuum Analytics, Inc.| (default, Jul 1 2013, 12:37:52)
[MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import otbApplication
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "otbApplication.py", line 28, in <module>
_otbApplication = swig_import_helper()
File "otbApplication.py", line 24, in swig_import_helper
_mod = imp.load_module('_otbApplication', fp, pathname, description)
ImportError: DLL load failed: The specified module could not be found.
This means that forcing the same paths to my new installation is not enough, what am I missing? Apologies for the long story (or the probable butchering of some of the terms).
Kind regards,
Jasper
You've got the right idea with the .pth file, but in order to get it to work you need to check some prerequisites. Obviously, the syntax needs to be good (just give the directory locations separated by line breaks). A common issue is that folks don't put the .pth files in the correct directory (usually though not necessarily \Lib\site-packages). I bet that if you check these you'll be okay.
**Also: as noted in the comments be aware that 32-bit python isn't going to like a 64-bit DLL and verse-visa, so ensure that you're running the right version of python when you try to access those libs.

Python-magic has OSError: [WinError 193] error while running in 32-bit version of IDLE

I have been trying to install the module python-magic for a few hours, and I've been encountering some problems. I am using the 32-bit version of Python 3.5.2 with 64-bit Windows 7.
First, I used the command "pip install python-magic". I downloaded magic1.dll, regex2.dll, and zlib1.dll, and magic from the Files for Windows project, and I copied those four files to
C:\Program Files (x86)\Python35-32\Lib\site-packages\python_magic-0.4.12-py3.5.egg-info.
I added the aforementioned directory to the beginning of PATH in my Windows environment variables. Then I opened IDLE's Shell and typed "import magic" and got the response OSError: [WinError 126] The specified module could not be found.
I read that the "magic" file should have the extension .dll, so I renamed it. This led to the pop-up warning by Windows "C:\Program Files (x86)\Python35-32\Lib\site-packages\magic.dll is either not designed to run on Windows or contains an error" and by Python, "OSError: [WinError 193] %1 is not a valid Win32 application". I read that the latter error is commonly encountered while running it in a 64-bit environment, but I made sure to run it in 32-bit IDLE and only have the 32-bit version of Python installed.
Following the advice of previous StackOverflow posts, I tried copying cygmagic-1.dll, cygwin1.dll, and cygz.dll to C:\Windows\System32, to the same folder as magic.dll, and I also tried renaming cygmagic-1.dll as magic1.dll, but that didn't have any effect. I know other places say you're not supposed to mix Cygwin Python and Windows Python, but I tried it without the involvement of these files, and it didn't work then, either.
I tried renaming magic.dll to magic.exe, and that allowed "import magic" and magic.Magic(magic_file=r'C:\Program Files (x86)\Python35-32\Lib\site-packages\python_magic-0.4.12-py3.5.egg-info\magic.exe') with the response "<magic.Magic object at 0x02EA0A70>". When I tried testing with magic.from_file(r'C:\Program Files (x86)\Python35-32\Lib\site-packages\README.txt'), though, I got the error magic.MagicException: b'could not find any magic files! I figured that renaming it to magic.exe had to be wrong, but that it was worth a try.
After I gave up on python-magic, someone recommended an older project. I downloaded it and put the pymagic folder in my site-packages directory. When I tried to import pymagic.pymagic, it told me that the module StringIO doesn't exist, and the recommender told me it was because StringIO is from Python2. I changed all mentions of StringIO to io and tried the command pymagic.pymagic.identify_file(r'E:\Pictures\picture.jpg')
This generated the error TypeError: startswith first arg must be bytes or a tuple of bytes, not str. I'm not involved enough with Python's os, io, etc. modules to know how to make modifications to get this to work. Can anyone make any recommendations on how to get python-magic or pymagic working, or any other module for identifying a file based on its header? I know this question has been asked a lot, but the previous answers didn't work out for me.
Did you call the 'magic' data file magic, and leave it in the same folder as magic1.dll?
Following your instructions I was able to reproduce the same error as you. Using Sysinternals Process Monitor, I could see that the reason for your first error appeared to be that Python was trying to load the the magic data file as if it were the library.
I then renamed the magic data file to magic_data, restarted IDLE, and it worked. I could then use magic to identify a file:
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import magic
>>> fn = r'C:\Python34\Lib\site-packages\python_magic-0.4.12-py3.4.egg-info\magic_data'
>>> m = magic.Magic(magic_file=fn)
>>> m.from_file(r'C:\Python34\Lib\site-packages\python_magic-0.4.12-py3.4.egg-info\zlib1.dll')
'PE32 executable for MS Windows (DLL) (console) Intel 80386 32-bit'
(I'm using a different version of Python (3.4), and a different version of Windows (10) to you, but I don't think these matter too much.)

unable to start many Python programs in Ubuntu 11.4

I have recently begun using Ubuntu. I am a reasonably experienced programmer, and have been using the "old" Python for years. But, in Python 3.2, when I try to run one of several different programs which I installed from .eg, I get an error message similar to the one pasted below.
Here, I am trying to start IPython. But the error is happening for others as well.
Traceback (most recent call last):
File "./ipython3", line 9, in <module>
load_entry_point('ipython==0.12', 'console_scripts', 'ipython3')()
File "/usr/local/lib/python3.2/dist-packages/distribute-0.6.24-py3.2.egg/pkg_resources.py", line 337, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/local/lib/python3.2/dist-packages/distribute-0.6.24-y3.2.egg/pkg_resources.py", line 2279, in load_entry_point
raise ImportError("Entry point %r not found" % ((group,name),))
ImportError: Entry point ('console_scripts', 'ipython3') not found
I have googled this error, and found that other users have been having similar problems, but as far as I can see, nobody has reported a real solution.
My guess is that you have mixed distro installs with easy_installs.
Probably the best way to experiment and work with Python is to use a separated "sandbox". With virtualenv (it is one of the several ways) it can be done as follows:
$ virtualenv -p /usr/bin/python3.2 --distribute MYPYTHON32
Running virtualenv with interpreter /usr/bin/python3.2
New python executable in MYPYTHON32/bin/python3.2
Also creating executable in MYPYTHON32/bin/python
Installing distribute...
Then you can install whatever you want to play with:
$ cd MYPYTHON32
$ bin/easy_install ipython
I have no problem:
$ bin/ipython3
Python 3.2 (r32:88445, Dec 8 2011, 15:26:58)
Type "copyright", "credits" or "license" for more information.
IPython 0.12 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
Documentation on virtualenv will give you more hints. This way possible install will not interfere with system pythons and you always has control on what you install in the sandbox.

import gtk/glib produces ImportError: DLL load failed

I installed the latest versions of python (2.6.5), gtk+, pygtk (and friends) from their respective websites on Windows XP SP3. When you try to import gtk (or just glib for that matter), an ImportError is raised:
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\site-packages\gtk-2.0\gtk\__init__.py", line 38, in <module>
import gobject as _gobject
File "C:\Python26\lib\site-packages\gtk-2.0\gobject\__init__.py", line 26, in <module>
from glib import spawn_async, idle_add, timeout_add, timeout_add_seconds, \
File "C:\Python26\lib\site-packages\gtk-2.0\glib\__init__.py", line 22, in <module>
from glib._glib import *
ImportError: DLL load failed: The specified procedure could not be found.
This was working on the same machine around 6 months ago, although this time around I have re-installed. I tried different versions of the libraries but still couldn't get it to work.
There are a couple of pages out there relating to this, but not suggest a solution, and I'm unable to find one.
I got a work around this by installing the windows GTK+ runtime as indicated in this link
http://www.gramps-project.org/wiki/index.php?title=Windows_installer#Installation
Also see their page dedicated to troubleshooting import errors on windows: http://www.gramps-project.org/wiki/index.php?title=ImportError:_DLL_load_failed
I managed to solve this myself. Firstly, there can be more than one cause so this may not help everyone.
I went through the PATH system environment variable and removed all paths that pointed to old versions of gtk+ (for example, those that come packaged with applications).
Same problem, answer was to reinstall pygtk from grampy page.
It was problem of pygtk, not system or gtk.
(Before this, import pygtk works well)
I had this same exact problem on Windows 7 64. I tried installing multiple versions of python 2.6, and the all-in-one pygtk that is supposed to give you GTK+ also. This installer didn't seem to do anything. Then I installed the latest versions of GTK+ (from binary), pygtk, pycairo, and gobject (all explained on the pygtk downloads website). When I did this, I got the ImportError: DLL load failed: The specified procedure could not be found. that is given above.
The SOLUTION was to uninstall pygtk, pycairo, gobject and remove the GTK+ bin location from my path variable. Then I went here and did the first 5 steps (in that order) under "Dependencies - The Hard Way". And now it works. Note: I didn't install gramps, just the pygtk part (first 5 steps).
Some reports indicate that the trick is to import pygtk first. Have you tried that?
I have copied gtk module (site-packages: gtk-2.0, pygtk.py, cairo) to other python installation and encountered the same problem.
It was happening because I forgot to copy site-packages/pygtk.pth.

Categories