Python:无法从我的模块加载类

Python:无法从我的模块加载类,python,module,Python,Module,编辑: 我无法使用自己的模块。那是愚蠢的浪费时间。如果您曾经遇到过同样的问题,请尝试先阅读以下内容: 我刚刚用Python开始OOP,我被模块和类弄糊涂了 我使用Mac电脑,我可以编写自己的模块,并从SitePackages文件夹加载它们 现在我想用有用的类创建模块。 导入自定义模块工作。 但是如果custom\u模块有一个类custom\u类,事情就不起作用了 我试着做: (编辑:很抱歉,我正在删除旧代码,这是我刚刚使用的代码,不起作用) 在custommodule.py中: class C

编辑: 我无法使用自己的模块。那是愚蠢的浪费时间。如果您曾经遇到过同样的问题,请尝试先阅读以下内容:

我刚刚用Python开始OOP,我被模块和类弄糊涂了

我使用Mac电脑,我可以编写自己的模块,并从SitePackages文件夹加载它们

现在我想用有用的类创建模块。
导入自定义模块
工作。 但是如果
custom\u模块
有一个类
custom\u类
,事情就不起作用了

我试着做: (编辑:很抱歉,我正在删除旧代码,这是我刚刚使用的代码,不起作用)

在custommodule.py中:

class Customclass:
    def __init__(self, name):
        self.name = name
此模块加载时没有错误。 然后我得到:

new = custommodule.Customclass('foo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Customclass'
new=custommodule.Customclass('foo'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“模块”对象没有属性“Customclass”
顺便说一句,我开始尝试使用下面的代码


我无法克服这一点。请告知,我一定是做错了什么。

至少对我来说,这在mod_name import ClassName中起作用

当我运行这段代码时,我没有得到任何错误。希望这有帮助


编辑:还要确保要导入的模块位于项目目录中。如果在图像中查看左侧面板,则两个模块都在
堆栈中。我希望这是显而易见的,但是,类也需要在您导入的模块中。确保要导入的类不会导入导入的类,因为这样会得到循环依赖项。

至少对我来说,这在mod_name import ClassName中起作用。

当我运行这段代码时,我没有得到任何错误。希望这有帮助


编辑:还要确保要导入的模块位于项目目录中。如果在图像中查看左侧面板,则两个模块都在
堆栈中。我希望这是显而易见的,但是,类也需要在您导入的模块中。确保要导入的类不会导入导入的类,因为这样会得到循环依赖项。

至少对我来说,这在mod_name import ClassName中起作用。

当我运行这段代码时,我没有得到任何错误。希望这有帮助


编辑:还要确保要导入的模块位于项目目录中。如果在图像中查看左侧面板,则两个模块都在
堆栈中。我希望这是显而易见的,但是,类也需要在您导入的模块中。确保要导入的类不会导入导入的类,因为这样会得到循环依赖项。

至少对我来说,这在mod_name import ClassName中起作用。

当我运行这段代码时,我没有得到任何错误。希望这有帮助

编辑:还要确保要导入的模块位于项目目录中。如果在图像中查看左侧面板,则两个模块都在
堆栈中。我希望这是显而易见的,但是,类也需要在您导入的模块中。确保要导入的类不会导入导入的类,因为这样会得到循环依赖项。

请尝试这种方法

custommodule目录中的custommodule.py文件

class Customclass:
    def __init__(self, name):
        self.name = name
custommodule目录中的文件
\uuuu init\uuuuuuuuuuuuuuuuuuu.py

from .custommodule import CustomClass
注意自定义模块前的点。这将强制init从同一目录加载模块

如果没有圆点,它可以在python2下工作,但不能在python3下工作

custommodule目录中的custommodule.py文件

class Customclass:
    def __init__(self, name):
        self.name = name
custommodule目录中的文件
\uuuu init\uuuuuuuuuuuuuuuuuuu.py

from .custommodule import CustomClass
注意自定义模块前的点。这将强制init从同一目录加载模块

如果没有圆点,它可以在python2下工作,但不能在python3下工作

custommodule目录中的custommodule.py文件

class Customclass:
    def __init__(self, name):
        self.name = name
custommodule目录中的文件
\uuuu init\uuuuuuuuuuuuuuuuuuu.py

from .custommodule import CustomClass
注意自定义模块前的点。这将强制init从同一目录加载模块

如果没有圆点,它可以在python2下工作,但不能在python3下工作

custommodule目录中的custommodule.py文件

class Customclass:
    def __init__(self, name):
        self.name = name
custommodule目录中的文件
\uuuu init\uuuuuuuuuuuuuuuuuuu.py

from .custommodule import CustomClass
注意自定义模块前的点。这将强制init从同一目录加载模块

如果没有点,它可以在python2下工作,但不能在python3下使用此文件布局

site-packages/custommodule/__init__.py
site-packages/custommodule/custommodule.py
您正在创建一个名为
custommodule
的包,其中包含一个名为
custommodule
的模块。您的代码需要看起来像

import custommodule
# or more specifically,
# import custommodule.custommodule
new = custommodule.custommodule.Customclass('foo')

您还可以将
custommodule.py
直接放在
站点包中,以避免创建包,在这种情况下,您的原始代码应该可以使用此文件布局。

site-packages/custommodule/__init__.py
site-packages/custommodule/custommodule.py
您正在创建一个名为
custommodule
的包,其中包含一个名为
custommodule
的模块。您的代码需要看起来像

import custommodule
# or more specifically,
# import custommodule.custommodule
new = custommodule.custommodule.Customclass('foo')

您还可以将
custommodule.py
直接放在
站点包中,以避免创建包,在这种情况下,您的原始代码应该可以使用此文件布局。

site-packages/custommodule/__init__.py
site-packages/custommodule/custommodule.py
您正在创建一个名为
custommodule
的包,其中包含一个名为
custommodule
的模块。您的代码需要看起来像

import custommodule
# or more specifically,
# import custommodule.custommodule
new = custommodule.custommodule.Customclass('foo')

您还可以将
custommodule.py
直接放在
站点包中,以避免创建包,在这种情况下,您的原始代码应该可以使用此文件布局。

site-packages/custommodule/__init__.py
site-packages/custommodule/custommodule.py
您正在创建一个名为
custommodule
的包,其中包含一个名为
custommodule
的模块。您的代码需要看起来像

import custommodule
# or more specifically,
# import custommodule.custommodule
new = custommodule.custommodule.Customclass('foo')


您还可以将
custommodule.py
直接放在
站点包中,以避免创建包,在这种情况下,您的原始代码应该可以工作。

自定义模块的代码是什么?您可以吗