简体   繁体   English

导入子模块的Python错误

[英]Python error importing a child module

parent/__init__.py : parent/__init__.py

    favorite_numbers = [1]

    def my_favorite_numbers():
        for num in favorite_numbers:
            num

    my_favorite_numbers()

    from .child import *

    my_favorite_numbers()

parent/child.py : parent/child.py

    print favorite_numbers

    favorite_numbers.append(7)

I then created a file one directory up from parent directory named tst.py : 然后,我从名为tst.py父目录创建了一个目录:

    import parent

So the directory structure looks like this: 所以目录结构如下所示:

    parent (directory)
        __init__.py (file)
        child.py (file)
    tst.py (file)

And I get this error upon execution: 我在执行时遇到此错误:

    NameError: name 'favorite_numbers' is not defined

How can I add a value to favorite_numbers within child.py so that when I execute the my_favorite_numbers() function, I get 1 and 7. 如何在child.py中的favorite_numbers中添加一个值,这样当我执行my_favorite_numbers()函数时,我得到1和7。

In Python, each module has its own separate globals. 在Python中,每个模块都有自己独立的全局变量。 That's actually the whole point of modules (as opposed to, say, C preprocessor-style text inserts). 这实际上是模块的重点(与C预处理器风格的文本插入相反)。

When you do from .child import * , that imports .child , then copies all of its globals into the current module's globals. 当你from .child import *那里导入.child ,然后将它的所有全局变量复制到当前模块的全局变量中。 They're still separate modules, with their own globals. 它们仍然是独立的模块,具有自己的全局变量。


If you want to pass values between code in different modules, you probably want to wrap that code up in functions, then pass the values as function arguments and return values. 如果要在不同模块中的代码之间传递值,可能需要将该代码包装在函数中,然后将值作为函数参数传递并返回值。 For example: 例如:

parent/__init__.py : parent/__init__.py

from .child import *

favorite_numbers = [1]

def my_favorite_numbers():
    for num in favorite_numbers:
        num

my_favorite_numbers()

child_stuff(favorite_numbers)

my_favorite_numbers()

parent/child.py : parent/child.py

def child_stuff(favorite_numbers):
    print favorite_numbers

    favorite_numbers.append(7)

In fact, you almost always want to wrap up any code besides initialization (defining functions and classes, creating constants and other singletons, etc.) in a function anyway. 实际上,除了初始化(定义函数和类,创建常量和其他单例等)之外,您几乎总是想要包含任何代码。 When you import a module (including from … import ), that only runs its top-level code the first time. import模块(包括from … import )时,它仅在第一次运行其顶级代码时。 If you import again, the module object already exists in memory (inside sys.modules ), so Python will just use that, instead of running the code to build it again. 如果再次import ,模块对象已经存在于内存中(在sys.modules ),因此Python将使用它,而不是运行代码再次构建它。


If you really want to push a value into another module's namespace, you can, but you have to do it explicitly. 如果你真的想将值推送到另一个模块的命名空间,你可以,但你必须明确地这样做。 And this means you have to have the module object available by importing it, not just importing from it: 这意味着你必须通过导入模块对象来获得它,而不仅仅是它导入:

from . import child
child.favorite_numbers = favorite_numbers

But this is rarely a good idea. 但这不是一个好主意。

Did you ever run setup.py or a way of "building" your library? 你有没有运行setup.py或“建立”你的图书馆的方式?

I would create a setup.py file and likely run it in develop mode. 我会创建一个setup.py文件,并可能在开发模式下运行它。 Python setup.py develop vs install Python setup.py develop vs install

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

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