我有三个文件.py
。 Main.py
、one_file.py
、second_file.py
。
在 one_file.py
中,我有一个名为 Create
的类。
在main.py
中我有这样的东西:
import one_file
var = input("yes or no?")
if var == "yes":
Create()
但我收到此错误NameError:名称“Create”未定义
。
我还尝试使用 from one_file import Create
和 from 。导入one_file
但无论如何它都不起作用。
one_file.py 中的代码:
import random
class Create:
def __init__(self):
self.word()
def word(self):
word_list = ["hello", "word", "sun", "moon"]
print("This is your word")
print(random.choice(word_list))
最佳答案
如果您想从导入的库运行代码,您应该首先调用导入的库。
import one_file
one_file.Create()
否则,你可以尝试
from one_file import Create
Create()
关于python - 从另一个 python 文件导入类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52364721/