I'm using Python 2.6.1 on Mac OS X.
我在Mac OS X上使用Python 2.6.1。
I have two simple Python files (below), but when I run
我有两个简单的Python文件(下面),但是当我运行时
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. ;)
我尝试以不同方式重命名文件和类,这就是为什么两端都有x和z的原因。 ;)
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()
4 个解决方案
#1
29
You need to do:
你需要这样做:
import sqlitedbx
def main():
db = sqlitedbx.SqliteDBzz()
db.connect()
if __name__ == "__main__":
main()
#2
5
try
尝试
from sqlitedbx import SqliteDBzz
#3
3
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
#4
0
That's How Python works. Try this :
这就是Python的工作方式。尝试这个 :
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
这样你就可以直接使用没有封闭模块的名称。或者只是导入模块并添加'sqlitedbx'。你的功能,课程等
#1
29
You need to do:
你需要这样做:
import sqlitedbx
def main():
db = sqlitedbx.SqliteDBzz()
db.connect()
if __name__ == "__main__":
main()
#2
5
try
尝试
from sqlitedbx import SqliteDBzz
#3
3
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
#4
0
That's How Python works. Try this :
这就是Python的工作方式。尝试这个 :
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
这样你就可以直接使用没有封闭模块的名称。或者只是导入模块并添加'sqlitedbx'。你的功能,课程等