Why is "Image" not defined after PIL import - python

First time Python user, so apologies if I am misunderstanding something basic like how libraries are accessed (I am an R user).
Using a colleague's code (which works on his end) and trying to load the the following:
from reportlab.lib import colors
results in the following error:
Traceback (most recent call last):
File "Box\Py\Python3\Py3_StaticMain.py", line 32, in <module>
from reportlab.lib import colors
File "C:\Program Files\Python310\lib\site-packages\reportlab\lib\colors.py", line 44, in <module>
from reportlab.lib.utils import asNative, isStr, rl_safe_eval
File "C:\Program Files\Python310\lib\site-packages\reportlab\lib\utils.py", line 389, in <module>
haveImages = Image is not None
NameError: name 'Image' is not defined
Pillow and reportlab are installed. After searching online I found similar error reports and the solution was to add this line prior to the previous library call (again, my colleague does need this on his end):
from PIL import Image
However, this did not fix the problem, the error persists. Also of note both these lines get greyed out by PyCharm which apparently means that these libraries are already loaded so these lines are unnecessary? It is counter intuitive that an unnecessary command would cause an error. The other libraries imported are os.path, sys, datetime, and tkinter. Also, even if I just put these two lines in a new py file I get the same behavior: greyed out and error.

Not a very satisfying answer but after uninstalling and reinstalling both Python and the IDE everything worked.

Related

Cannot get pandas to import properly

I have spent the last hour trying to fix the pandas import error, but with no success.
Python version: 3.8.3
Pandas version: 1.0.4
I use Visual Studio code. Importing other modules works (tried with numpy). Pandas is installed using pip.
The error:
Traceback (most recent call last):
File "c:/Users/38599/Desktop/course1/moduli.py", line 34, in <module>
import pandas
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pandas\__init__.py", line 180, in <module>
import pandas.testing
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pandas\testing.py", line 5, in <module>
from pandas._testing import (
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pandas\_testing.py", line 404, in <module>
RANDS_CHARS = np.array(list(string.ascii_letters + string.digits), dtype=(np.str_, 1))
AttributeError: module 'string' has no attribute 'ascii_letters'
I've tried downgrading pandas and reinstalling it, but nothing fixed the error for me.
Does anyone have any ideas?
remove the line with string.ascii_letters from the code.. as the error says
I faced same issue. I did the following and it worked.
select the py file you want to execute, for vs code menu select Run=>Start Debugging, it works perfectly. I think it is a bug in the current vs code
Do you have a file named string.py somewhere in your workspace, by chance?
If yes, that would be the problem. By Python rules, such a file is also a module named string, so whenever anything is trying to import string, your file gets loaded instead of the standard library module with the same name.

import wx.xrc in wxFormBuilder code fails with file not found error

The form builder wxFormBuilder imports wx and wx.xrc. However the second of these causes an error as follows:
Traceback (most recent call last):
File "/home/peter/python/wxpython/wxFormBuilder/Demo/demo.py", line 4, in
import demogui
File "/home/peter/python/wxpython/wxFormBuilder/Demo/demogui.py", line 11, in
import wx.xrc
File "/usr/local/lib/python3.6/dist-packages/wx/xrc.py", line 10, in
from ._xrc import *
ImportError: libwx_gtk3u_html-3.0.so.0: cannot open shared object file: No such file or directory
The obvious answer would be that the file was missing, but it isn't:
peter#peter-HP-Pavilion-15-Notebook-PC:~$ locate libwx_gtk3u_html-3.0.so.0
/usr/local/lib/python3.6/dist-packages/wx/libwx_gtk3u_html-3.0.so.0
So, to my inexperienced eyes it seems to be present and in the right place. Can anyone make any suggestions on this?
I'm using wxPython for Python 3, by the way.
Many thanks
This issue was fixed in wxPython release 4.0.1, see the discussion at https://groups.google.com/forum/#!topic/wxpython-users/UBjdeTgOmD4.
The build scripts were setting LD_RUN_PATH only for the wxPython libraries, not for the embedded wxWidgets build. I can confirm that the fixes mentioned in https://github.com/wxWidgets/Phoenix/issues/723 solve the issue.

Errors importing pyodbc

My background is Java/C++ and I'm very new to python. I'm trying to import pyodbc to my project which works alright if I do it via command line.
import odbc
However, when I try to do the same in pydev/eclipse, i get the following error which I cannot find a solution to. I suspect this may have something to do with Eclipse setup
Traceback (most recent call last):
File "C:\Users\a\workspace\TestPyProject\src\helloworld.py", line 2, in <module>
import pyodbc
File "C:\Users\a\AppData\Local\Continuum\Anaconda3\Lib\site-packages\sqlalchemy\dialects\mssql\pyodbc.py", line 105, in <module>
from .base import MSExecutionContext, MSDialect, VARBINARY
ImportError: attempted relative import with no known parent package
I'm really stuck here and any hints will be appreciated!
An incorrect library was imported into the External Libraries list of the project. After fixing it, the import is working.

Import error in Python: Polygon

I installed this library called Polygon, in Python 2.7.3. But, each time I import it I get the next error message :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "Polygon/__init__.py", line 5, in <module>
from Polygon.cPolygon import *
ImportError: No module named cPolygon
I have no idea about what could be going wrong. And also, I have already tried to contact the original author of this on his personal webpage.
but he hasn't replied though :( I wonder if someone can help me with this issue please.
It sounds like you didn't in fact install it, but instead just copied it to your working directory. As always, run setup.py with the appropriate arguments to install.
This error can happen if you try to run python from inside the directory you used to install Polygon. Move to a different directory and try again.

have trouble with PIL to create PhotoImage object

I have Python 2.6 running on Fedora 13. I installed PIL 1.1.7, and I get the successful installed message in the Python prompt. I am able to import PIL.PhotoImage,but when I try to run the following, I get an error.
mgobj = PhotoImage(file=imgpath)
Stacktrace:
Traceback (most recent call last):
File "viewer-tk.py", line 25, in <module>
imgobj = PhotoImage(file=imgpath) # now JPEGs work!
File "/home/Toshiba/vinpython/venv/lib/python2.6/site-packages/PIL/ImageTk.py", line 116, in __init__
self.paste(image)
File "/home/Toshiba/vinpython/venv/lib/python2.6/site-packages/PIL/ImageTk.py", line 181, in paste
import _imagingtk
ImportError: No module named _imagingtk
In the module ImageTk.py , I see _imagingtk being imported,but i am not sure how do it get that module.Help is greatly appreciated!!!
You need to install Tkinter python module as stated at this trac. But since it's an integral part of python distributions. Since you are using fedora take a look at this page where you can install Tkinter when it doesn't come along, even being for Fedora 3 I think it might help.

Categories