How can I source a Python file from another Python file?

PythonServer Side ProgrammingProgramming

In order to source a Python file from another python file, you have to use it like a module. import the file you want to run and run its functions. For example, say you want to import into, assuming the files are in the same directory, inside fileA you'd write

import fileB

Now in fileA, you can call any function inside fileB like:


In file B you have to define a function called my_func before you can use it in any other file.


def my_func():
    print("Hello from B")


Now when you run, you will get the output:

Hello from B
Updated on 13-Dec-2019 11:02:32