如何从其他.py文件中调用一个.py文件中的函数?

时间:2016-08-15 16:52:18

标签: python python-3.x import

我有两个文件,我想从另一个文件中导入一个类。

档案one.py

class One: 
    def printNumber( self, a ):
        print (a)

和档案two.py

#import One # gives error no module named One
#import one # gives error no module named one

class Two:
  # Here I want to call printNumber method from one.py

1 个答案:

答案 0 :(得分:0)

将文件保存在同一目录中,然后使用以下格式。

from one import One

one = One()

class Two:
    a = 5
    one.printNumber(a)

编辑:在Pycharm中工作时,您需要标记源根目录以导入您自己的文件。

enter image description here