Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何导入在_uinit_uuu.py中定义的变量?_Python - Fatal编程技术网

Python 如何导入在_uinit_uuu.py中定义的变量?

Python 如何导入在_uinit_uuu.py中定义的变量?,python,Python,我对导入的工作原理有点困惑。假设: package/ __init__.py file1.py 在\uuuu init\uuuuu.py中: from file1 import AClass __version__ = '1.0' Class AClass(object): def bar(): # I want to use __version__here, but don't want to pass # it through th

我对导入的工作原理有点困惑。假设:

package/
    __init__.py
    file1.py
\uuuu init\uuuuu.py
中:

from file1 import AClass
__version__ = '1.0'
Class AClass(object):
    def bar():
        # I want to use __version__here, but don't want to pass
        # it through the constructor. Is there any way?
        pass
file1.py中

from file1 import AClass
__version__ = '1.0'
Class AClass(object):
    def bar():
        # I want to use __version__here, but don't want to pass
        # it through the constructor. Is there any way?
        pass

如果我使用来自的
。导入
file1.py中的
版本
它只是说
导入错误:无法导入名称

版本

您有一个循环依赖关系,因为两个文件都试图彼此导入。将
\uuuu version\uuu
移动到一个单独的模块,例如
package/version.py
,然后使用

from .version import __version__
尝试:

您需要在导入模块之前分配常量,以便在尝试导入模块时将其放置到位


编辑:larsmans建议避免循环依赖是个好主意。

可能是错误的,但_uu意味着私人ie不能在其他地方使用?