Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name namedtuple

I have a python script that produces the following error when run:

import urllib2
File "C:\Python27\lib\urllib2.py", line 94, in <module>
import httplib
File "C:\Python27\lib\httplib.py", line 73, in <module>
from urlparse import urlsplit
File "C:\Python27\lib\urlparse.py", line 119, in <module>
from collections import namedtuple
ImportError: cannot import name namedtuple

I also use the openpyxl package, located in C:\Python27\Lib\site-packages, which contains a collections folder with a __init__.py file. I figured this is what causes the problem, because it may be preventing the python 2.7 collections module from being imported.

What can I do to fix this problem?

like image 439
SergioP Avatar asked Jul 29 '14 21:07

SergioP


2 Answers

This can also happen if your local script contains a "collections" module due to the way Python handles relative and absolute imports.

like image 60
Chris Lamb Avatar answered Oct 21 '22 01:10

Chris Lamb


I had the same issue but this worked when I changed Collection Package to a simple collection directory.

I just deleted the init.py file from the collections

this can be due to the package name Collections. this can be also resolved by renaming Collection Package to othername.

like image 42
Sudip Neupane Avatar answered Oct 21 '22 02:10

Sudip Neupane