简体   繁体   English

Python跨模块和全局变量导入

[英]Python imports across modules and global variables

I have a question which seems to be rather fundamental but I can't seem to find any help on this anywhere. 我有一个似乎相当基本的问题,但我似乎无法在任何地方找到任何帮助。

file_a.py >>

from xyz import XYZ
class A:
    .
    .
    .

file_b.py >>

import file_a
from file_a import A
class B(A):
    def __init__(self):
        A.__init__(self)

    def someMethod(self):
        XYZ.doSomething()

XYZ.doSomething() fails saying NameError: name 'XYZ' is not defined Even standard imports like 'import sys' done from file_a does not seem to render it usable in file_b. XYZ.doSomething()无法说出NameError:名称'XYZ'未定义即使从file_a完成的标准导入(如'import sys')似乎也无法使其在file_b中可用。 I assumed that should work. 我认为应该工作。 Is my understanding wrong? 我的理解错了吗? If yes, then is there a way to have common imports and global variables across files? 如果是,那么有没有办法在文件中包含常见的导入和全局变量? (If it is of nay help, I've been a C++ and java programmer and am now starting to use python. ) (如果它有帮助,我一直是C ++和java程序员,现在我开始使用python。)

Is my understanding wrong? 我的理解错了吗?

Yes, because the line from file_a import A import only class A into the namespace of file_b . 是的,因为from file_a import A的一行from file_a import A只将类A导入到file_b的命名空间中。 The namespace of file_a is left alone. file_a的命名空间file_a If it were not like this, there would be little sense in having both syntax: 如果它不是这样的话,那么两种语法都没有意义:

import modulename
from modulename import something

as if your thinking was right, then after the second form you would always be able to use modulename.someotherthing . 好像你的想法是正确的,然后在第二个表格后你总是可以使用modulename.someotherthing

If yes, then is there a way to have common imports and global variables across files? 如果是,那么有没有办法在文件中包含常见的导入和全局变量?

Yes, with the star * operator: 是的,使用星号*运算符:

from modulename import *

but this brings the issue of namespace pollution, for example from file_a import * will import in file_b also all the imports done in file_a . 但这带来了命名空间污染的问题,例如from file_a import *将在file_b导入所有在file_a完成的file_a You will eventually lose control of your imports and this will bite you at some time... trust me on this! 你最终失去对你的进口的控制权,这会在某个时候咬你...相信我!

When for some reason from module import * is needed, a workaround to namespace pollution is to define in module the variable __all__ , which whitelists what should be imported with the star operator. 当出于某种原因需要from module import * ,命名空间污染的一种解决方法是在module定义变量__all__ ,该变量将使用星号运算符导入的内容列入白名单。

HTH! HTH!

When you import a module, all the variables defined in that module are available in its namespace. 导入模块时,该模块中定义的所有变量都可在其命名空间中使用。 Hence, if XYZ is available in file_a module, when you import file_a you can access XYZ as file_a.XYZ . 因此,如果XYZfile_a模块中可用,则在import file_a ,可以将XYZ作为file_a.XYZ访问。

The general idea here is that your namespace shouldn't be cluttered with the contents of other namespaces. 这里的一般想法是,您的命名空间不应该与其他命名空间的内容混在一起。

Yes, your understanding is wrong. 是的,你的理解是错误的。 Each module is its own namespace, and only things you explicitly import within that file are available in that namespace. 每个模块都是自己的命名空间,只有您在该文件中显式导入的内容才可在该命名空间中使用。

Contrary to other answers, it is not particularly Pythonic to refer to file_a.XYZ , although this will work. 与其他答案相反,引用file_a.XYZ并不是特别Pythonic,尽管这会起作用。 Instead, you should import XYZ and sys at the top of file_b . 相反,您应该在file_b的顶部导入XYZ和sys。

import file_a
from file_a import A

I think the problem is: you do not import XYZ really! 我认为问题是:你真的不进口XYZ!

from fila_a import * 

can solve your problem,but it is not a good way~~~ 可以解决你的问题,但这不是一个好方法~~~

you can write this in file_b: 你可以在file_b中写这个:

from file_a import XYZ

It is done? 它完成了吗?

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

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