How do I call a function from another .py file?

You can call a function from another .py file by importing the file and then calling the function. For example, if you have a file called that contains a function called example_function(), you can call that function from another file, such as, by doing the following:

import example_module


In this example, the import example_module statement causes the code in to be executed, which makes the example_function() function available to the current script. Then, you can call that function using the syntax example_module.example_function().

Watch a course Python - The Practical Guide

You can also use from example_module import example_function instead of import example_module

from example_module import example_function

Then you can call the function directly with its name, in this case example_function().

It is worth to mention that this approach has the advantage that you only import the necessary function instead of the whole module, but on the other hand, if you are importing the same function and variable names in different modules, there may be conflicts that you should take care of.