Python不允许我从同一目录导入.py文件

时间:2020-09-16 15:06:20

标签: python

所以我才刚刚开始学习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!')

2 个答案:

答案 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)