Python子模块导入疯狂 [英] Python Submodule Importing Madness

查看:127
本文介绍了Python子模块导入疯狂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用一些基本的Python导入攻击我的头。我已经尽可能地简化了问题,希望我能够将其扩展到更大的范围,如果我理解它是如何工作的那样

I'm banging my head against the wall with some basic Python importing. I have simplified the problem as much as possible hoping I'd be able to expand this to a larger scale if I understand how this works

这就是困境 -

run.py 来自子模块 p1 的工作原理,但不是当它处于顶层时。为什么?

Here is the dilemma -
run.py from inside the submodule p1 works, but NOT when it's at the top level. Why?

(版本Python 3.6.3)

(version Python 3.6.3)

/sandbox
    __init__.py
    /p1
        __init__.py
        file1.py
        run.py
    run.py

/ sandbox / p1 / __ init __。py

__all__ = ["file1", "file2"]

/sandbox/p1/file1.py

from file2 import B
class A(object):
    pass

/sandbox/p1/file2.py

class B(object):
    pass

/ sandbox / p1 /run.py

from file1 import A
a = A()

/sandbox/run.py

from p1 import file1
a = file1.A()

做:

python p1 /run.py (工作正常)

python run.py


Traceback(最近一次调用最后一次):
文件run.py,第2行,在
中来自p1 import file1
文件.. ./sandbox/p1/file1.py,第1行,来自file2导入的
B
ModuleNotFoundError:没有名为'file2'的模块

Traceback (most recent call last): File "run.py", line 2, in from p1 import file1 File ".../sandbox/p1/file1.py", line 1, in from file2 import B ModuleNotFoundError: No module named 'file2'


推荐答案

(I)没有名为'file2'的模块



您遇到了没有名为'file2'的模块,因为您在 file1 run.py > import file2 ,python找不到 file2 作为模块目录而不在模块搜索路径中。

(I) no module named 'file2'

You encountered No module named 'file2' because you run run.py outside a package, when file1 import file2, python cannot find file2 as the module directory not in module search path.

对于您的方案, file1 & file2 是2个模块,在同一个包中,对于这种情况,建议你使用相对导入,这是最好的练习。

For your scenario, file1 & file2 are 2 modules which in the same package, for this situation, suggest you use relative import, this is the best practice.

from .file2 import B
class A(object):
    pass

然后, python run.py 工作。

正如你所提到的,如果执行 python p1 / run.py ,你会看到下一个:

As you mentioned you will see next if execute python p1/run.py:


尝试相对导入而没有已知的父包

attempted relative import with no known parent package

这是什么意思?

这是因为相对导入可以在包中的模块中得到支持。

This because relative import can just be supported in the module which in package.

您可能会说 file1.py 在包 p1 中,为什么还会看到错误?

You may say file1.py is in package p1, why still see the error?

这是因为python使用 __ name __ 来判断这个模块是否在包中或不,不是它的位置。

This is because python use __name__ to determine if this module in package or not, not by the position of it.

更改 file1.py ,如下所示:

print(__name__)
from .file2 import B
class A(object):
    pass

接下来是输出:


p1.file1

p1.file1



python p1 / run.py



python p1/run.py


file1

file1

对于 python p1 / run.py ,因为它在包中运行,python将无法知道它在一个包,所以输出是 file1

For python p1/run.py, because it runs in the package, python will fail to know it is in a package, so output is file1.

如果 __ name __ 没有,python会认为它不在包中,所以相对导入失败。

If __name__ does not has ., python will think it's not in package, so relative import failure.

使用 python -m p1.run ,这应该可以执行顶部脚本包。

Use python -m p1.run, this should works to execute top script which inside a package.

这篇关于Python子模块导入疯狂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆