SoFunction
Updated on 2025-03-02

Python calls methods of other functions in .py in a .py file

Suppose the file named needs to call the C(x,y) function in the file

If in the same directory, just

import B
if __name__ == "__main__":
  (x,y)

If you only need to call a single function, you can

from B import C
if __name__ == "__main__":
  C(x,y)

If the same is located in a different directory, the following method can be used

(Suppose it is located in the root directory of disk D)

1. Reference the path

import sys
('D:/')
import B
if __name__=="__main__":
  print (x,y)

2. Use imp

import imp
B=imp.load_source('B','D:/')
import B
if __name__=="__main__":
  print (x,y)

Summarize

The above is the method of calling other functions in .py in a .py file introduced by the editor to you. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply to you in time!