python模块imp

2024-05-17 02:34:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用macports安装各种模块。一般来说,这很有效,但下面是一个我无法轻易解决的错误:

$ python
Python 2.6.6 (r266:84292, Feb 12 2011, 16:57:53) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dns
>>> import opcode
>>> from dns import resolver
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/dns/resolver.py", line 26, in <module>
    import dns.message
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/dns/message.py", line 28, in <module>
    import dns.opcode
ImportError: No module named opcode

这可能是路径问题吗?

>>> import sys
>>> sys.path
['', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages', '/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']

$cat/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site packages/dns/init.py [剪下的评论] #init.py用于DNS类。

__version__ = '2.3.3'

import Type,Opcode,Status,Class
from Base import DnsRequest, DNSError
from Lib import DnsResult
from Base import *
from Lib import *
Error=DNSError
from lazy import *
Request = DnsRequest
Result = DnsResult

提前谢谢。


Tags: frompyimportdnslibpackageslocallibrary
3条回答

我正在使用Python3.7并安装了pubdns。这解决了我的问题。 我在使用py3dns、pyDNS(不会安装)、dnspython和其他许多工具时遇到了极大的困难

我卸载了py26 dnspython并重新安装。问题解决了。芬克在freenode上提出了这个建议。谢谢。

因为你需要:

from dns import resolver

这不起作用:

import datetime.datetime

但这确实:

from datetime import datetime

如果导入的包是另一个包的一部分,则需要使用“from”语法

相关问题 更多 >