[python] NameError: global name is not defined

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 the terminal:

Traceback (most recent call last):
  File "update_urls.py", line 7, in <module>
    main()
  File "update_urls.py", line 4, in main
    db = SqliteDBzz()
NameError: global name 'SqliteDBzz' is not defined

I tried renaming the files and classes differently, which is why there's x and z on the ends. ;)

File sqlitedbx.py

class SqliteDBzz:
    connection = ''
    curser = ''

    def connect(self):
        print "foo"

    def find_or_create(self, table, column, value):
        print "baar"

File update_url.py

import sqlitedbx

def main():
    db = SqliteDBzz()
    db.connect

if __name__ == "__main__":
    main()

This question is related to python class namespaces

The answer is


try

from sqlitedbx import SqliteDBzz

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


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

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to class

String method cannot be found in a main class method Class constructor type in typescript? ReactJS - Call One Component Method From Another Component How do I declare a model class in my Angular 2 component using TypeScript? When to use Interface and Model in TypeScript / Angular Swift Error: Editor placeholder in source file Declaring static constants in ES6 classes? Creating a static class with no instances In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric Static vs class functions/variables in Swift classes?

Examples related to namespaces

Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model How do I get an object's unqualified (short) class name? socket.error:[errno 99] cannot assign requested address and namespace in python What is the use of "using namespace std"? Visibility of global variables in imported modules Using :: in C++ 'namespace' but is used like a 'type' type object 'datetime.datetime' has no attribute 'datetime' Why am I getting error CS0246: The type or namespace name could not be found? string in namespace std does not name a type