简体   繁体   English

python模块导入错误

[英]python module importing error

I have the following structure: 我有以下结构:

AXBot:
    __init__.py
    bot.py
    util.py
    settings.py
    creator
        __init__.py
        xbot.py

The problem is that I cannot import the 'util' module in 'xbot.py' because python ends with 'ImportError: No module named util'... how can I solve? 问题是我无法在“ xbot.py”中导入“ util”模块,因为python结尾于“ ImportError:没有名为util的模块” ...我该如何解决?

PS: I am using the following code to import: PS:我正在使用以下代码导入:

import util
import settings

Thank you. 谢谢。

Use relative importing 使用相对导入

from . import util
from . import settings

I would recomment changing your folder hierarchy though, that looks cleaner to me. 我建议改变文件夹的层次结构,对我来说看起来更干净。 Also check your PYTHONPATH, it should normally work the way you did it. 还要检查您的PYTHONPATH,它通常应该按照您的方式工作。

References: 参考文献:

Try to stay away from sys.path hacks. 尝试远离 sys.path hacks。

I think the namespacing is incorrect. 我认为命名空间不正确。 From xbot.py, try using this import command 从xbot.py,尝试使用此导入命令

from AXBot import util
from AXBot import settings

It seems you're trying to run xbot.py from within the creator folder. 看来您正在尝试从creator文件夹中运行xbot.py

This is the output I get with xbot.py containing import util : 这是我通过xbot.py获得的包含import util的输出:

C:\Users\Luke\Python stuff\AXBot\creator>xbot.py
Traceback (most recent call last):
  File "C:\Users\Luke\Python stuff\AXBot\creator\xbot.py", line 4, in <module>
    import util
ImportError: No module named util

This is the output I get with xbot.py containing from . import util 这是我从xbot.py获得的包含的输出from . import util from . import util

C:\Users\Luke\Python stuff\AXBot\creator>xbot.py
Traceback (most recent call last):
  File "C:\Users\Luke\Python stuff\AXBot\creator\xbot.py", line 3, in <module>
    from . import util
ValueError: Attempted relative import in non-package

I also get this latter error with from .. import util instead of from . import util 我也from .. import util而不是from . import util收到后一个错误from . import util from . import util . from . import util

If you're running xbot.py from the directory containing it, Python can't tell that it's being run inside a package hierarchy. 如果从包含xbot.py的目录中运行xbot.py ,则Python无法确定它是在程序包层次结构中运行的。 It thinks xbot.py isn't inside a package. 它认为xbot.py不在软件包中。

I replaced the line that attempted to import util with from AXBot import util , moved up a couple of directories and ran xbot.py using Python's -m command-line switch, which tells Python to run a module specified by module name instead of filename. 我将尝试from AXBot import util导入util的行替换为上一行,并使用Python的-m命令行开关运行了xbot.py ,该命令行告诉Python运行由模块名而不是文件名指定的模块。 Note that when you use -m , you pass in the fully-qualified name of the module, including the package hierarchy, but you don't include the file extension .py , because that's not part of the name of the module: 请注意,当您使用-m ,您传入模块的全限定名称,包括包层次结构,但不包括文件扩展名.py ,因为这不是模块名称的一部分:

C:\Users\Luke\Python stuff\AXBot\creator>cd ..\..

C:\Users\Luke\Python stuff>python -m AXBot.creator.xbot
1232

I got the same output if I used import AXBot.util as util instead of from AXBot import util . 如果将import AXBot.util as util而不是from AXBot import util则得到相同的输出。

(I don't have your code to run, so instead I put a variable in util.py and attempted to print its value from within xbot.py . The value of this variable was 1232 .) (我没有你的代码运行,所以不是我把一个变量util.py并试图从内打印其价值xbot.py 。这个变量的值是1232 )。

where you are is the most important question! 您在哪里是最重要的问题! according to that, you use the import . 据此,您可以使用import please update your question 请更新您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM