Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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 使用参考模块';s变量_Python - Fatal编程技术网

Python 使用参考模块';s变量

Python 使用参考模块';s变量,python,Python,在我的项目中,我定义了3个python文件: variables.py(其中包含一些变量,其值将由用户填写): helper.py(它有一些有用的函数,需要variables.py中的变量,所以我导入了它 import variables def execute_job(): print variables.VMD_name creation.py:它需要访问helper.py的函数和variables.py的变量。由于variable.py已经导入helper.py中,我认为我

在我的项目中,我定义了3个python文件:

  • variables.py
    (其中包含一些变量,其值将由用户填写):

  • helper.py
    (它有一些有用的函数,需要variables.py中的变量,所以我导入了它

    import variables
    def execute_job():
        print variables.VMD_name 
    
  • creation.py
    :它需要访问helper.py的函数和variables.py的变量。由于variable.py已经导入helper.py中,我认为我应该只导入helper.py,而helper.py反过来也会包含变量

    import helper
    
  • 但是下面两条语句都不起作用。请让我知道我是否需要在creation.py中再次导入variables.py?这不是双重性吗

    print helper.VMD_name
    print helper.variables.VMD_name
    
    您可以(在creation.py中)执行以下操作:

    这就行了

    或者,将在helper.py中导入的方式更改为:

    from variables import VMD_name
    
    现在,
    print helper.VMD_name
    将在creation.py中工作

    为什么它会这样工作?当您编写
    导入变量
    时,模块
    变量
    中的常量在
    helper.py
    中变为可用,但您仍然需要在模块名称前加前缀才能访问它们(即在
    helper.py
    中您应该编写
    variables.VMD_name
    )。类似地,在
    creation.py中
    import helper
    之后,
    helper
    中的常量在
    creation
    中可用,但同样,您应该在模块名称前面加上前缀。对于此常量,意味着您应该在已经存在的
    变量前面加上
    helper


    另一方面,如果您使用
    从变量导入VMD_name
    导入,则该常量在helper中无需模块限定符即可使用。

    您已在
    helper.py
    中导入模块
    变量
    但尚未导入
    变量
    模块的变量。因此您可以编写:

    import helper
    
    print helper.variables.VMD_name
    
    如果要使用
    helper.VMD_name
    ,则应在
    helper.py
    中导入变量:

    from variables import *
    

    helper.variables.VMD_name
    应该可以工作。因此,我们要么需要一个更完整的示例来实际再现问题,要么需要一条详细的错误消息。
    import helper
    
    print helper.variables.VMD_name
    
    from variables import *