第一次问。 无论如何,Python似乎表现得非常奇怪。不久前,我制作了一个程序,从另一个文件(较大项目的一部分)导入变量。为了证明:
item.py
class item():
def __init__(self,name,desc,val,dmg,dur):
s = ''
self.name = name
self.desc = desc
self.val = val
self.dmg = dmg
self.dur = dur
rshckey = item('Key to Nowhere','An ancient key, rusted and corroded, that is said to unlock any door.',0,0,1)
data.py
from item import *
print (rshckey)
这给了我这个错误信息:
Traceback (most recent call last):
File "/Users/Chrispang/Google Drive/Python Code/My Projects/Wyvern Caves/Wy4 Riv/data.py", line 2, in <module>
print (rshckey)
NameError: name 'rshckey' is not defined
任何人都能解释一下吗?我试过了
from item import rshckey
给了我
Traceback (most recent call last):
File "/Users/Chrispang/Google Drive/Python Code/My Projects/Wyvern Caves/Wy4 Riv/data.py", line 1, in <module>
from item import rshckey
ImportError: cannot import name 'rshckey'
有趣的是,几天前它仍然运作良好。