/projects/mymath$ ls
__init__.py __init__.pyc mymath.py mymath.pyc tests
在tests目录下
/projects/mymath/tests/features$ ls
steps.py steps.pyc zero.feature
我尝试导入我的阶乘函数
sys.path.insert(0,"../../")
#import mymath
from mymath.MyMath import factorial
但它说没有名为 MyMath 的模块。
这是我的虚拟 MyMath 类。
class MyMath(object):
def factorial(self, number):
if n <= 1:
return 1
else:
return n * factorial(n-1)
那怎么了?谢谢。这甚至是一个好习惯(编辑 sys 路径?)
这将有效 import mymath
请您参考如下方法:
您不能从类中导入函数。您想要导入类本身(import mymath.mymath.MyMath)或将函数放在模块级别并执行 import mymath.mymath.factorial。
