Wizzard
Wizzard

Reputation: 12702

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()

Upvotes: 26

Views: 132947

Answers (4)

devsaw
devsaw

Reputation: 1037

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

Upvotes: 0

kfirbreger
kfirbreger

Reputation: 109

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

Upvotes: 3

SilentGhost
SilentGhost

Reputation: 319551

You need to do:

import sqlitedbx

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

if __name__ == "__main__":
    main()

Upvotes: 30

joaquin
joaquin

Reputation: 85613

try

from sqlitedbx import SqliteDBzz

Upvotes: 5

Related Questions