NameError: global name is not defined

后端 未结 4 2075
借酒劲吻你
借酒劲吻你 2020-12-03 16:54

I\'m using Python 2.6.1 on Mac OS X.

I have two simple Python files (below), but when I run

python update_url.py

I get on

相关标签:
4条回答
  • 2020-12-03 17:29

    Importing the namespace is somewhat cleaner. Imagine you have two different modules you import, both of them with the same method/class. Some bad stuff might happen. I'd dare say it is usually good practice to use:

    import module
    

    over

    from module import function/class
    
    0 讨论(0)
  • 2020-12-03 17:33

    try

    from sqlitedbx import SqliteDBzz
    
    0 讨论(0)
  • 2020-12-03 17:34

    You need to do:

    import sqlitedbx
    
    def main():
        db = sqlitedbx.SqliteDBzz()
        db.connect()
    
    if __name__ == "__main__":
        main()
    
    0 讨论(0)
  • 2020-12-03 17:39

    That's How Python works. Try this :

    from sqlitedbx import SqliteDBzz
    

    Such that you can directly use the name without the enclosing module.Or just import the module and prepend 'sqlitedbx.' to your function,class etc

    0 讨论(0)
提交回复
热议问题