Reputation: 25
Traceback (most recent call last):
File "bs4.py", line 1, in <module>
import bs4
File "/home/mhadi/Desktop/bs4test/bs4.py", line 5, in <module>
soup = bs4.BeautifulSoup(site,'lxml')
AttributeError: module 'bs4' has no attribute 'BeautifulSoup'
The code:
import bs4
import urllib.request
site = urllib.request.urlopen('http://127.0.0.1:8000').read()
soup = bs4.BeautifulSoup(site,'lxml')
#for i in site:
# print(site[i])
print(soup)
Upvotes: 3
Views: 5192
Reputation: 477607
The problem is that your filename is bs4.py
. Now if you write an import
statement, Python will first look for local files with that name. So it assumes that your import bs4
refers to your own file. Your file will thus aim to import itself, but it obviously does not contain the desired module.
A quick fix is renaming the file. For instance into bs4tests.py
. Then you can use import bs4
.
Alternatively, you can for instance try to remove the local path, like:
import sys # import sys package
old_path = sys.path[:] # make a copy of the old paths
sys.path.pop(0) # remove the first one (usually the local)
import bs4 # import the package
sys.path = old_path # restore the import path
Upvotes: 6