简体   繁体   English

得到 AttributeError: 'module' 对象在从另一个文件导入全局变量时没有属性

[英]got AttributeError: 'module' object has no attribute while importing global variable from another file

I have two python files like this:我有两个这样的python文件:

# first.py
global x
if __name__ == "__main__":
    x = 'test_var'

and:和:

# second.py
import first
class XX(object):
    @staticmethod
    def print_x():
        print first.x

I run this script:我运行这个脚本:

import second
second.XX.print_x()

And I get this error:我得到这个错误:

AttributeError: 'module' object has no attribute 'x'

Any idea what's going wrong?知道出了什么问题吗?

The code in first.py never runs because it isn't your entry point and the code isn't directly called, meaning x is never defined. first.py的代码永远不会运行,因为它不是您的入口点,并且不会直接调用该代码,这意味着从未定义 x。 Use first.py as your entry point or put the declaration of x into a method that you call before trying to access it.使用 first.py 作为您的入口点或将 x 的声明放入您尝试访问它之前调用的方法中。

暂无
暂无

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

相关问题 我收到此错误 AttributeError: 'function' object has no attribute 'hlauncher' while试图从另一个文件中获取属性 - I got this error AttributeError: 'function' object has no attribute 'hlauncher' while trying to get a attribute from another file 出现错误 - AttributeError: 'module' object has no attribute 'connect' - 从 sockets 程序导入时 - Getting the error - AttributeError: 'module' object has no attribute 'connect' - While importing from a sockets program AttributeError:“模块”对象没有属性“文件” - AttributeError: 'module' object has no attribute 'file' 我得到了这个AttributeError:'module'对象没有属性'QuerySet' - i got this AttributeError: 'module' object has no attribute 'QuerySet' AttributeError:从文件读取时,“ str”对象没有属性“ readline” - AttributeError: 'str' object has no attribute 'readline' While Reading from File 导入theano:AttributeError:'模块'对象没有属性'find_graphviz' - Importing theano: AttributeError: 'module' object has no attribute 'find_graphviz' 导入cython函数:AttributeError:'module'对象没有属性'fun' - Importing cython function: AttributeError: 'module' object has no attribute 'fun' 导入大熊猫会给出AttributeError'模块'对象没有属性'partial' - Importing pandas gives an AttributeError 'module' object has no attribute 'partial' 从另一个文件调用方法AttributeError: object has no attribute - Calling a method from another file AttributeError: object has no attribute “AttributeError: 'list' object 没有属性 'items'” 导入 Json 时 - "AttributeError: 'list' object has no attribute 'items'" While importing Json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM