【问题标题】:Python ImportError: No module named 'xxxx'Python ImportError:没有名为“xxxx”的模块
【发布时间】:2013-07-28 17:17:24
【问题描述】:

得到错误:

Traceback (most recent call last):
File "C:/Python33/Lib/123.py", line 5, in <module>
from wordpress_xmlrpc import Client, WordPressPost
File "C:/Python33/lib/site-packages/wordpress_xmlrpc/__init__.py", line 6, in <module>
import base
ImportError: No module named 'base'

base.py 位于:

C:\Python33\Lib\site-packages\wordpress_xmlrpc\

__init__.py 看起来像:

from base import *
from wordpress import *
import methods

我使用的所有其他导入都可以正常工作。

路径变量如下所示:

C:\Python33;C:\Python33\Scripts;C:\Python33\Lib\site-packages;C:\Python33\Lib\site-packages\wordpress_xmlrpc;C:\Python33\Lib;

有人知道我为什么会收到这个错误吗?

【问题讨论】:

  • 您自己的应用程序不会碰巧在运行时更改路径变量,对吗?
  • from foo import * 通常不受欢迎。

标签: python importerror import


【解决方案1】:

使用python3时需要使用显式的相对导入或绝对导入,所以

from wordpress_xmlrpc import base
# or
from . import base

在 python3 中,import base 只会导入绝对包 base,因为不再支持隐式相对导入。

【讨论】:

    【解决方案2】:

    相对导入与“.”一起使用使用 python 3 时。

    请看已经回答的问题

    link

    【讨论】:

    • 在 py3 中允许相对导入 ,而不是 implicit 相对导入。您需要显式形式 (from . import ...)
    猜你喜欢
    • 2016-03-31
    • 2013-01-06
    • 2015-12-09
    • 2012-01-26
    • 2020-12-15
    • 2015-11-17
    • 2016-07-24
    • 2017-07-15
    • 1970-01-01
    相关资源
    最近更新 更多