[英]How do I import my module?
/projects/mymath$ ls
__init__.py __init__.pyc mymath.py mymath.pyc tests
and under the directory tests
I have 在目录tests
下
/projects/mymath/tests/features$ ls
steps.py steps.pyc zero.feature
I tried to import my factorial function 我试图导入我的阶乘函数
sys.path.insert(0,"../../")
#import mymath
from mymath.MyMath import factorial
But it said No module named MyMath. 但是它说没有名为MyMath的模块。
Here is my dummy MyMath
class. 这是我的虚拟MyMath
课程。
class MyMath(object):
def factorial(self, number):
if n <= 1:
return 1
else:
return n * factorial(n-1)
So what's wrong? 那怎么了 Thanks. 谢谢。 Is this even a good practice (editing the sys path?) 这甚至是一个好习惯吗(编辑sys路径?)
This will work import mymath
这将工作import mymath
You can't import a function out of a class. 您不能从类中导入函数。 You want to either import the class itself ( import mymath.mymath.MyMath
) or put the function at a module level and do import mymath.mymath.factorial
. 您要导入类本身( import mymath.mymath.MyMath
)或将函数置于模块级别并import mymath.mymath.factorial
。
From what I can tell, it's right. 据我所知,这是正确的。 There is no module named mymath.MyMath
. 没有名为mymath.MyMath
模块。 There's a module named mymath.mymath
though... 虽然有一个名为mymath.mymath
的模块...
To be explicit, when you create a folder and put an __init__.py
file in it, you've crated a package. 明确地说,在创建文件夹并将__init__.py
文件放入其中时,您已经创建了一个包。 If your __init__.py
file is empty, then you still have to explicitly import the modules in the package. 如果__init__.py
文件为空,则仍然必须显式导入软件包中的模块。 So you have to do import mymath.mymath
to import the mymath
module into your namespace. 因此,您必须执行import mymath.mymath
才能将mymath
模块导入您的命名空间。 Then you can access the things you want via mymath.mymath.MyMath
, and so on. 然后,您可以通过mymath.mymath.MyMath
访问所需的内容。 If you want to import the class directly in, you have to do this: 如果要直接导入该类,则必须执行以下操作:
from mymath.mymath import MyMath
And as someone else has already explained, you can't import a method from a class. 正如其他人已经解释的那样,您不能从类中导入方法。 You have to import the whole class. 您必须导入整个类。
One problem is your import is wrong. 一个问题是您的导入错误。 You have a package called mymath, and a module in it called mymath. 您有一个名为mymath的程序包,其中有一个名为mymath的模块。 In that module is a class. 该模块中有一个类。 That's the most you can import: 这是您最多可以导入的内容:
>>> from mymath.mymath import MyMath
>>> myMathObject = MyMath()
>>> myMathObject.factorial(5)
120
The other problem is your second call to factorial should call factorial on self
, otherwise it will try to look it up as a free function in the module, which won't work. 另一个问题是您对阶乘的第二次调用应在self
上调用阶乘,否则它将尝试在模块中将其查找为自由函数,这将无法正常工作。 Try it! 试试吧!
Here is my dummy MyMath class. 这是我的虚拟MyMath课程。
Python is not Java; Python不是Java;它不是Java。 the class is not any kind of organizational unit here, but simply a blueprint for data types. 在这里,该类不是任何类型的组织单位,而仅仅是数据类型的蓝图。 There is no reason to define a class here (which would then have to be instantiated anyway); 这里没有理由定义一个类(无论如何都必须实例化它)。 just define the factorial
function as a function. 只需将factorial
函数定义为函数即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.