EDIT: I couldn't use my own modules. It was a stupid waste of time. If you ever have the same problem, try reading this first: http://docs.python-guide.org/en/latest/writing/structure/
编辑:我无法使用自己的模块。这是一种愚蠢的浪费时间。如果您遇到同样的问题,请先尝试阅读:http://docs.python-guide.org/en/latest/writing/structure/
I just started OOP with Python and I am confused by modules and classes.
我刚开始使用Python进行OOP,我对模块和类感到困惑。
I work with a Mac and I can write my own modules and load them from the site-packages folder.
我使用Mac,我可以编写自己的模块并从site-packages文件夹加载它们。
Now I would like to create modules with useful classes. import custom_module
works. But if custom_module
has a class Custom_class
, things don't work.
现在我想用有用的类创建模块。 import custom_module有效。但是如果custom_module有一个类Custom_class,那么事情就不起作用了。
I tried doing: (EDIT: I am sorry, I am removing old code which was made up, this is what I just used and doesn't work)
我尝试过:(编辑:对不起,我正在删除已编写的旧代码,这是我刚才使用的并且不起作用)
in custommodule.py:
class Customclass:
def __init__(self, name):
self.name = name
This module loads without errors. Then I get:
该模块加载没有错误。然后我得到:
new = custommodule.Customclass('foo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Customclass'
BTW, I started trying to do this using code from this tutorial
顺便说一句,我开始尝试使用本教程中的代码执行此操作
I was not able to get over this. Please advise, I must be doing something wrong.
我无法克服这一点。请指教,我一定是做错了。
3 个解决方案
#1
With this file layout
有了这个文件布局
site-packages/custommodule/__init__.py
site-packages/custommodule/custommodule.py
you are creating a package named custommodule
that contains a module also named custommodule
. Your code needs to look like
您正在创建一个名为custommodule的包,其中包含一个名为custommodule的模块。您的代码需要看起来像
import custommodule
# or more specifically,
# import custommodule.custommodule
new = custommodule.custommodule.Customclass('foo')
or
from custommmodule import custommodule
new = custommodule.Customclass('foo')
You can also put custommodule.py
directly in site-packages
to avoid creating the package, in which case your original code should work.
您也可以将custommodule.py直接放在site-packages中以避免创建包,在这种情况下您的原始代码应该可以正常工作。
#2
For me, at least, this works from mod_name import ClassName
对我来说,至少,这适用于mod_name import ClassName
When I run this code I don't get any errors. Hope this helped
当我运行此代码时,我没有收到任何错误。希望这有帮助
EDIT: also make sure that the module you want to import is in the project directory. If you view the left panel in the images both modules are in Stack
. I hope this is obvious but, the class needs to be in the module you import as well. Make sure that the class you're importing does not import the class your importing in because then you get circular dependencies.
编辑:还要确保要导入的模块位于项目目录中。如果您在图像中查看左侧面板,则两个模块都在堆栈中。我希望这是显而易见的,但是,类也需要在您导入的模块中。确保您导入的类不导入您导入的类,因为这样您就会获得循环依赖。
#3
Try this way
试试这种方式
File custommodule.py in the directory custommodule
在custommodule目录中输入custommodule.py
class Customclass:
def __init__(self, name):
self.name = name
File __init__.py
int he custommodule directory
在custommodule目录中输入文件__init__.py
from .custommodule import CustomClass
Note the dot before custommodule. This force the init to load the module from the same directory.
请注意custommodule之前的点。这会强制init从同一目录加载模块。
Without the dot, it work under python2 but not under python3
如果没有点,它可以在python2下工作,但不能在python3下工作
#1
With this file layout
有了这个文件布局
site-packages/custommodule/__init__.py
site-packages/custommodule/custommodule.py
you are creating a package named custommodule
that contains a module also named custommodule
. Your code needs to look like
您正在创建一个名为custommodule的包,其中包含一个名为custommodule的模块。您的代码需要看起来像
import custommodule
# or more specifically,
# import custommodule.custommodule
new = custommodule.custommodule.Customclass('foo')
or
from custommmodule import custommodule
new = custommodule.Customclass('foo')
You can also put custommodule.py
directly in site-packages
to avoid creating the package, in which case your original code should work.
您也可以将custommodule.py直接放在site-packages中以避免创建包,在这种情况下您的原始代码应该可以正常工作。
#2
For me, at least, this works from mod_name import ClassName
对我来说,至少,这适用于mod_name import ClassName
When I run this code I don't get any errors. Hope this helped
当我运行此代码时,我没有收到任何错误。希望这有帮助
EDIT: also make sure that the module you want to import is in the project directory. If you view the left panel in the images both modules are in Stack
. I hope this is obvious but, the class needs to be in the module you import as well. Make sure that the class you're importing does not import the class your importing in because then you get circular dependencies.
编辑:还要确保要导入的模块位于项目目录中。如果您在图像中查看左侧面板,则两个模块都在堆栈中。我希望这是显而易见的,但是,类也需要在您导入的模块中。确保您导入的类不导入您导入的类,因为这样您就会获得循环依赖。
#3
Try this way
试试这种方式
File custommodule.py in the directory custommodule
在custommodule目录中输入custommodule.py
class Customclass:
def __init__(self, name):
self.name = name
File __init__.py
int he custommodule directory
在custommodule目录中输入文件__init__.py
from .custommodule import CustomClass
Note the dot before custommodule. This force the init to load the module from the same directory.
请注意custommodule之前的点。这会强制init从同一目录加载模块。
Without the dot, it work under python2 but not under python3
如果没有点,它可以在python2下工作,但不能在python3下工作