Remi.b
Remi.b

Reputation: 18219

Source a Python's file from another Python file

My question is maybe a duplicate but I couldn't find the answer.

How can I source a python file from another python file. Something like:

def fun_1 (arg):
   ...
   ...
   fun_2 (arg):
     ...
     ...

#### MAIN ####

Source the file that contains the function `fun_2`

fun_1('hello')

Upvotes: 2

Views: 946

Answers (1)

NPE
NPE

Reputation: 500337

The canonical way is to turn the first file into a module and import it into the second file:

$ cat file1.py
def fun():
   print('in fun1')

$ cat file2.py
import file1

file1.fun()

Upvotes: 5

Related Questions