I try to run first example from python wiki and when I try to run it:
$ python BaseHttpServer.py
I get an error AttributeError: 'module' object has no attribute 'BaseHTTPRequestHandler'.
I tested it on Python 2.7.3 on Linux Mageia 2 64-bit:
Traceback (most recent call last):
File "BaseHTTPServer.py", line 9, in <module>
import BaseHTTPServer
File "/home/vanveber/BaseHttpServer/BaseHTTPServer.py", line 14, in <module>
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
AttributeError: 'module' object has no attribute 'BaseHTTPRequestHandler'
And it on Python 2.7.3 on Windows 7 64-bit
Traceback (most recent call last):
File "BaseHTTPServer.py", line 11, in <module>
from BaseHTTPServer import BaseHTTPRequestHandler
File "C:\BaseHttpServer\BaseHTTPServer.py", line 11, in <module>
from BaseHTTPServer import BaseHTTPRequestHandler
ImportError: cannot import name BaseHTTPRequestHandler
BUT!
BaseHttpServer is a class from standard Python library.
If I write and run this code from Python GUI on Windows it works correctly!
What is a problem and why?!
Solution: Rename the python file.
Explanation: BaseHTTPServer is a module in the standard library. When you have a python file called BaseHTTPServer.py in your local directory, you will hide the standard library module, and you can no longer import it, because the statement
import BaseHTTPServer
will not import the standard library module, but the local BaseHTTPServer.py module.
Related
I'm having an import module issue. I'm using python 3.7.2 on windows 10.
When I trie to import the websockets module (I just write import websockets in the python shell), I have this error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Simon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websockets\__init__.py", line 16, in <module>
+ uri.__all__
NameError: name 'client' is not defined
I tried to download the websockets module last version but the error remains.
PS : English is not my native language, sorry for the phrasing
I am getting this error when I try to import the boto3 library. I have installed boto3 1.4.1 and also tried downgrading to 1.3.1 and still I am getting this below error
Traceback (most recent call last):
File "storage.py", line 1, in <module>
import boto3
File "/Library/Python/2.7/site-packages/boto3/__init__.py", line 16, in <module>
from boto3.session import Session
File "/Library/Python/2.7/site-packages/boto3/session.py", line 17, in <module>
import botocore.session
File "/usr/local/lib/python2.7/site-packages/botocore/__init__.py", line 22, in <module>
class NullHandler(logging.Handler):
AttributeError: 'module' object has no attribute 'Handler'
I've found that this can happen when a file named logging.py exists in the same directory as your Python script (storage.py). This is because Boto3 tries to import the logging module, but it imports logging.py instead.
To fix, move or rename logging.py, or move your Python script.
This question already has answers here:
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed last month.
I am trying to import requests module, but I got this error
my python version is 3.4 running on ubuntu 14.04
>>> import requests
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 10, in <module>
from queue import LifoQueue, Empty, Full
ImportError: cannot import name 'LifoQueue'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/requests/__init__.py", line 58, in <module>
from . import utils
File "/usr/local/lib/python3.4/dist-packages/requests/utils.py", line 26, in <module>
from .compat import parse_http_list as _parse_list_header
File "/usr/local/lib/python3.4/dist-packages/requests/compat.py", line 7, in <module>
from .packages import chardet
File "/usr/local/lib/python3.4/dist-packages/requests/packages/__init__.py", line 3, in <module>
from . import urllib3
File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/__init__.py", line 10, in <module>
from .connectionpool import (
File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 12, in <module>
from Queue import LifoQueue, Empty, Full
ImportError: No module named 'Queue'
import queue is lowercase q in Python 3.
Change Q to q and it will be fine.
(See code in https://stackoverflow.com/a/29688081/632951 for smart switching.)
Queue is in the multiprocessing module so:
from multiprocessing import Queue
I solve the problem my issue was I had file named queue.py in the same directory
It's because of the Python version. In Python 2.x it's import Queue as queue; on the contrary in Python 3 it's import queue. If you want it for both environments you may use something below as mentioned here
try:
import queue
except ImportError:
import Queue as queue
In my case it should be:
from multiprocessing import JoinableQueue
Since in python2, Queue has methods like .task_done(), but in python3 multiprocessing.Queue doesn't have this method, and multiprocessing.JoinableQueue does.
I run into the same problem and learn that queue module defines classes and exceptions, that defines the public methods (Queue Objects).
Ex.
workQueue = queue.Queue(10)
I just copy the file name Queue.py in the */lib/python2.7/ to queue.py and that solved my problem.
An application can't import the "urllib" module from the package "six". But I can import it from a test script in the same environment. Can't figure out why.
❯ nova
Traceback (most recent call last):
File "/usr/bin/nova", line 6, in <module>
from novaclient.shell import main
File "/usr/lib/python2.7/site-packages/novaclient/shell.py", line 45, in <module>
from novaclient import client
File "/usr/lib/python2.7/site-packages/novaclient/client.py", line 34, in <module>
from six.moves.urllib import parse
ImportError: No module named urllib
But if I run a simple test script, it's just fine:
#!/usr/bin/python2
from six.moves.urllib import parse
foo = parse.urlsplit("http://foo.net/florp/blorp?blap=blork")
print foo
Here's the output:
❯ ./test.py
SplitResult(scheme='http', netloc='foo.net', path='/florp/blorp', query='blap=blork', fragment='')
Tried setting my PYTHONPATH explicitly, tried it under virtualenv... nothing..
Any ideas? (Sorry I'm not a Python programmer, so it may be something obvious).
Yesterday I installed feedparser (on OSX 10.5) and it worked fine, but now it stopped working.
This is the script (copied from feedparser documentation)
import feedparser
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
d['feed']['title']
u'Sample Feed'
It tells me this:
Traceback (most recent call last):
File "example.py", line 3, in <module>
import feedparser
File "example.py", line 2, in <module>
d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
AttributeError: 'module' object has no attribute 'parse'
But also an actual script using feedparser stopped working, same error.
The point is when there is a script named feedparser.py, python will considered it as a module to import with higher priority than the module installed.
Issue is with Name of file. Python confuses between name of file and module name.