python: module has no attribute mechanize - python

#!/usr/bin/env python
import mechanize
mech = mechanize.Browser()
page = br.open(SchoolRank('KY'))
Gives:
Traceback (most recent call last):
File "mechanize.py", line 2, in <module>
import mechanize
File "/home/jcress/Documents/programming/schooldig/trunk/mechanize.py", line 12, in <module>
mech = mechanize.Browser()
AttributeError: 'module' object has no attribute 'Browser'
And I'm confused. I have the module installed for 2.6 and 2.7, same result...

Change your filename away from mechanize.py. Python is importing your file as the module.

Related

Python AttributeError: 'module' object has no attribute 'get'

I installed the requests package, but when I start to use it, I got this error :
AttributeError: 'module' object has no attribute 'get'
This my code :
from bs4 import BeautifulSoup
import requests
r = requests.get("http://someSite.com/path")
I checked some solution to this problem, and most of them saying that either there is a mistake with importing the package, or that a file with the name requests.py exist in the current directory, but it's not the case for me.
it's been a while since I got this error, and I stuck with it.
any idea? thanks.
UPDATE
FULL error message
Traceback (most recent call last):
File "parser.py", line 2, in <module>
import requests
File "/usr/local/lib/python2.7/dist-packages/requests/__init__.py", line 52, in <module>
from .packages.urllib3.contrib import pyopenssl
File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 47, in <module>
from cryptography import x509
File "/usr/local/lib/python2.7/dist-packages/cryptography/x509/__init__.py", line 7, in <module>
from cryptography.x509.base import (
File "/usr/local/lib/python2.7/dist-packages/cryptography/x509/base.py", line 14, in <module>
from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa
File "/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/asymmetric/rsa.py", line 14, in <module>
from cryptography.hazmat.backends.interfaces import RSABackend
File "/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/backends/__init__.py", line 7, in <module>
import pkg_resources
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 76, in <module>
import parser
File "/home/lichiheb/Desktop/parser.py", line 4, in <module>
r = requests.get("http://t...content-available-to-author-only...s.com/search-results-jobs/?searchId=1483197031.949&action=search&page=1&view=list")
AttributeError: 'module' object has no attribute 'get'
Your file is called parser.py which conflicts with a built-in module name parser.
The error message about requests was a weird and unfortunate coincidence. Just rename your module to something else.

AttributeError: 'module' object has no attribute 'webdriver'

AttributeError: 'module' object has no attribute 'webdriver'
why this error happen when write
import selenium
and when write code like this no error happen
from selenium import webdriver
You get an error because webdriver is a module inside the selenium module, and you can't access modules without an explicit import statement.
If you take a look at help(selenium), you'll see there are two modules and one non-module contained inside.
PACKAGE CONTENTS
common (package)
selenium
webdriver (package)
And it behaves according to what I described above:
>>> selenium.common # doesn't work
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'common'
>>> from selenium import common # works
>>> selenium.selenium # works
<class 'selenium.selenium.selenium'>
>>> selenium.webdriver # doesn't work
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'webdriver'
>>> from selenium import webdriver # works
>>>

Plist file parsing error "'str' object has no attribute 'read'"

I'm trying to read a .plist file on Mac OSX with the plistlib.
Sadly I always get an error when running the script
Traceback (most recent call last):
File "/Users/johannes/pycharmprojects/adobe-cache-cleaner/test.py", line 6, in <module>
pl = plistlib.load(fp2)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plistlib.py", line 983, in load
header = fp.read(32)
AttributeError: 'str' object has no attribute 'read'
that's my script:
import plistlib
fp2 = "/Users/Johannes/Pythonproject/test.plist"
pl = plistlib.load(fp2)
print pl
It looks like ptlistlib ist expecting a file not a string:
import plistlib
with open("/Users/Johannes/Pythonproject/test.plist", "rb") as file:
pl = plistlib.load(file)
print pl
see https://docs.python.org/3/library/plistlib.html

AttributeError: 'module' object has no attribute 'TextCalendar'

When I copy the below code in Ideone then it runs fine but it is not running in the text editor and showing the error mentioned in the subject of this post:
import calendar
c = calendar.TextCalendar(calendar.SUNDAY)
c.prmonth(2007, 7)
The complete error is:
Traceback (most recent call last):
File "calendar.py", line 1, in <module>
import calendar
File "/home/shl/Desktop/calendar.py", line 2, in <module>
c = calendar.TextCalendar(calendar.SUNDAY)
AttributeError: 'module' object has no attribute 'TextCalendar'
change the program name from calendar.py to something like calendar_code.py
This wold work properly.
Importing from builtin library when module with same name exists

Python 3.3.2: Error on html module import

I have a clean install of Python 3.3.2 on Windows 7. I'm trying to import html.parser.HTMLParser, using the example from the documentation: Simple HTML and XHTML parser
But I'm getting the error:
>>> from html.parser import HTMLParser
aee4
gg2
Traceback (most recent call last):
File "htmlang.py", line 1, in <module>
from html.parser import HTMLParser
File "c:\Python33\lib\html\parser.py", line 13, in <module>
import warnings
File "c:\Python33\lib\warnings.py", line 6, in <module>
import linecache
File "c:\Python33\lib\linecache.py", line 10, in <module>
import tokenize
File "c:\Python33\lib\tokenize.py", line 37, in <module>
__all__ = token.__all__ + ["COMMENT", "tokenize", "detect_encoding",
AttributeError: 'module' object has no attribute '__all__'
I have just opened the interpreter and typed the import line. Why it isn't working as expected? Why it is printing the weird "aee4" and "gg2" strings?
Just write "import HTMLParser" and it will work

Categories

Resources