Python - Cannot import name - 3.4 - python

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.

Related

Why is "Image" not defined after PIL import

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.

trouble importing autograd in python script

I'm trying to import autograd with the following line of code:
import autograd.numpy as np
However, I'm getting the following error when trying to run the script:
Traceback (most recent call last):
File "autograd.py", line 1, in <module>
import autograd.numpy as np
File "/home/hakon/Documents/FYS_STK4155/project2/code and plots/test/autograd.py", line 1, in <module>
import autograd.numpy as np
ModuleNotFoundError: No module named 'autograd.numpy'; 'autograd' is not a package
I've tried installing autograd through pip, pip3 and conda, but the error remains the same.
The problem is that your module (the one that you're running) has the same name that you're trying to import: autograd (.py). Try renaming your file and running it again.
aaossa's answer worked for me. If changing the module name that you are running doesn't work, please check if there is any other autograd(.py) that you created existing in your current directory. If so, you also need to change that file's name or delete it so that you can import "autograd".

importing installed python module x.features2d from Opencv

I have installed Opencv3 module on my Ubuntu 14.04 system and the installation of sub-modules done without problem. But when I called x.features2d in my code after importing cv2, as shown in the code below
import sys
import cv2
import cv2.xfeatures2d
import numpy as np
sys.path.append('/usr/local/lib/python2.7/site-packages')
img_bgr=cv2.imread('sc2.png')
img_gray=cv2.cvtColor(img_bgr,cv2.COLOR_BGR2GRAY)
template=cv2.imread('template.png',0)
w, h=template.shape[::-1]
res=cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold=0.7
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_bgr, pt, (pt[0]+w, pt[1]+h), (0,255,0), 2)
freakExtractor=cv2.xfeatures2d.FREAK_create()
keypoints,descriptors=freakExtractor.compute(img_bgr,keypoints)
cv2.imshow('detected', img_bgr)
cv2.waitKey(0)
it gives me the error of not being able to find x.features2d module as below
Traceback (most recent call last):
File "test.py", line 3, in <module>
import cv2.xfeatures2d
ImportError: No module named xfeatures2d
I tried to re-install it again and also installed opencv-contrib, but same problem happened.
Any idea to solve it?
You have not given many details on your installation process.
All the contrib features such as xfeatures2d are no longer part of the prebuilt libraries. This means xfeatures2d will only work if you compile openCV yourself.
Example of how to do that (e.g. for Windows) is given here:
https://putuyuwono.wordpress.com/2015/04/23/building-and-installing-opencv-3-0-on-windows-7-64-bit/

ImportError: No module named vis (Python )?

Hello l tried to run my python code and the errors is:
Traceback (most recent call last):
File "/home/anelmad/Desktop/simulation/Theano-Tutorials-master/3_net.py", line 5, in <module>
from foxhound.utils.vis import grayscale_grid_vis, unit_scale
ImportError: No module named vis**
knowing that l have correctly installed foxhound.
import theano
from theano import tensor as T
import numpy as np
from load import mnist
from foxhound.utils.vis import grayscale_grid_vis, unit_scale
from scipy.misc import imsave
is Foxhound a scikit? if yes, it doe's not contains utils module. You can clone it on github https://github.com/IndicoDataSolutions/Foxhound
I have met with the same problem. from foxhound vis import grayscale_grid_vis is enough. And I just comment out the other part. And the program works well. It seems that the module is not used.

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.

Categories