2016-08-14 100 views
1

我用Python 2.7 + PyCharm 2016.2 + Windows 10(64)在我的電腦上遇到了這個奇怪的問題。AttributeError:'模塊'對象沒有屬性'maketrans' - PyCharm

雖然行書:

import urllib 
from BeautifulSoup import * 

url = raw_input('Enter -') 
if (len(url) < 1): 
    url = "http://python-data.dr-chuck.net/comments_292106.html" 
html = urllib.urlopen(url).read() 
soup = BeautifulSoup(html) 
lst = list() 
tags = soup('span') 

for tag in tags: 
# print 'TAG:', tag 
# print 'URL:', tag.get('href', None) 
# print 'Contents:', tag.contents 
# print 'Attrs:', tag.attrs 
    num = int(tag.contents[0]) 
    lst.append(num) 
print sum(lst) 

我已經得到了這樣的消息:

C:\Python27\python.exe E:/python/coursera/following_links.py 
23 
0.8475 
Traceback (most recent call 
    File "E:/python/coursera/following_links.py", line 1, in <module> 
    import urllib 
    File "C:\Python27\lib\urllib.py", line 30, in <module> 
    import base64 
    File "C:\Python27\lib\base64.py", line 98, in <module> 
    _urlsafe_encode_translation = string.maketrans(b'+/', b'-_') 
AttributeError: 'module' object has no attribute 'maketrans' 

Process finished with exit code 1 

同樣的情況發生在WingIDE。

有趣的是,當使用python的空閒時,這個腳本可以工作。

而且它的工作原理與我的Windows 8(64)(Python 2.7版和PyCharm 2016.2)第二PC

+0

你有PyCharm項目目錄中的'string.py'嗎? –

+0

Thx尋求幫助。 我確實在項目目錄中有我自己的「string.py」。 我是莫朗:( –

回答

-1

上,我認爲這是發生在幕後。爲了執行你的命令,Python必須做一些工作,而不是找到它需要的文件。這也許可以解釋爲什麼它可能在空閒和PC上工作,但不能在Wing IDE等其他工具中工作。

相關問題