所以我才刚刚开始学习Python,我正在学习类和导入,但是由于某种原因,即使我按照书中所说的那样执行相同的代码,也会出现回溯错误。
Traceback (most recent call last):
File "C:/Users/Programming/Desktop/Programs/EX40/main.py", line 1, in <module>
import objects.py
ModuleNotFoundError: No module named 'objects.py'; 'objects' is not a package
这是我要链接的两个Python文件:
main.py
import objects.py
print(MyStuff.tangerine)
objects.py
class MyStuff(object):
def __init__(self, arm):
self.tangerine = 'And now a thousand years between'
self.arm = arm
def apple(self):
print('I AM CLASSY APPLES!')
答案 0 :(得分:0)
尝试使用
import objects
因为它将py
作为objects
文件中不存在的模块
答案 1 :(得分:0)
import objects.py
不是有效的导入
https://docs.python.org/3/reference/import.html
尝试
from objects import MyStuff
实例化课程:
mystuff = MyStuff("arm value")
print(mystuff.tangerine)