2012-09-11 228 views
2

我使用的版本是python2.7,當我使用腳本時發生錯誤,但它在命令行中起作用。導入模塊失敗

from twisted.internet import protocol, reactor 

和錯誤:

ravjqpejcnngpig.eqo/re/fgh/ocr 
Traceback (most recent call last): 
    File "./tsTclntTW.py", line 5, in <module> 
    from twisted.internet import protocol, reactor 
    File "/usr/local/lib/python2.7/dist-packages/twisted/__init__.py", line 18, in <module> 
    from zope.interface import Interface 
    File "/usr/local/lib/python2.7/dist-packages/zope.interface-4.0.1-py2.7-linux-x86_64.egg/zope/__init__.py", line 3, in <module> 
    import pkg_resources 
    File "/usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 698, in <module> 
    self.callbacks.append(callback) 
    File "/usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 701, in Environment 

    File "/usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 96, in get_supported_platform 
    distutils.util.get_platform() normally reports the minimum version 
    File "/usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 222, in get_build_platform 
    # fallback for MacPorts 
    File "/usr/lib/python2.7/distutils/util.py", line 65, in get_platform 
    osname = string.lower(osname) 
AttributeError: 'module' object has no attribute 'lower' 
Error in sys.excepthook: 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 64, in apport_excepthook 
    from apport.fileutils import likely_packaged, get_recent_crashes 
    File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module> 
    from apport.report import Report 
    File "/usr/lib/python2.7/dist-packages/apport/report.py", line 18, in <module> 
    import problem_report 
    File "/usr/lib/python2.7/dist-packages/problem_report.py", line 16, in <module> 
    from email.mime.multipart import MIMEMultipart 
    File "/usr/lib/python2.7/email/mime/multipart.py", line 9, in <module> 
    from email.mime.base import MIMEBase 
    File "/usr/lib/python2.7/email/mime/base.py", line 9, in <module> 
    from email import message 
    File "/usr/lib/python2.7/email/message.py", line 16, in <module> 
    import email.charset 
    File "/usr/lib/python2.7/email/charset.py", line 14, in <module> 
    import email.quoprimime 
    File "/usr/lib/python2.7/email/quoprimime.py", line 48, in <module> 
    from string import hexdigits 
ImportError: cannot import name hexdigits 

Original exception was: 
Traceback (most recent call last): 
    File "./tsTclntTW.py", line 5, in <module> 
    from twisted.internet import protocol, reactor 
    File "/usr/local/lib/python2.7/dist-packages/twisted/__init__.py", line 18, in <module> 
    from zope.interface import Interface 
    File "/usr/local/lib/python2.7/dist-packages/zope.interface-4.0.1-py2.7-linux-x86_64.egg/zope/__init__.py", line 3, in <module> 
    import pkg_resources 
    File "/usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 698, in <module> 
    self.callbacks.append(callback) 
    File "/usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 701, in Environment 

    File "/usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 96, in get_supported_platform 
    distutils.util.get_platform() normally reports the minimum version 
    File "/usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 222, in get_build_platform 
    # fallback for MacPorts 
    File "/usr/lib/python2.7/distutils/util.py", line 65, in get_platform 
    osname = string.lower(osname) 
AttributeError: 'module' object has no attribute 'lower' 

我已經重新安裝了Python我使用,它不工作,我在Ubuntu 12.04。我也有

grep ravjqpejcnngpig.eqo/re/fgh/ocr . -R -n 

根據/usr/lib/python2.7但沒有返回。

+0

'ravjqpejcnngpig.eqo/RE/FGH/ocr'看起來像'tsTclntTW.py輸出',這與你的錯誤沒有任何關係。它是Python挑戰的一部分,你似乎在做這件事。 –

+0

@MatthewTrevor是的,把我自己的腳本命名爲'string.py'是很愚蠢的。事實上我正在做Python挑戰。 – kyon

回答

4

也許你在你的項目中有一個名爲string的模塊,它會遮蓋相同名稱的庫模塊。


編輯:爲了找到答案,只是失敗的import語句之前添加到您的腳本:

import string 
print string.__file__ 
+0

不,我沒有輸入字符串 – kyon

+3

@kyon確定嗎?追蹤幾乎*大聲說'string'模塊不是stdlib'string'模塊。仔細檢查你的目錄中是否運行了這個,沒有'string.py' –

+0

@JonClements是的,我在我的目錄中找到了一個string.py。非常感謝! – kyon