Call Function from Another File in Python

Python is a high-level interpreted programming that's simple to understand. Despite its simplicity, Python offers powerful tools and utilities for creating highly scalable and sophisticated applications. Modular programming, in which logic is defined independently and then imported into other program sections, is made possible by Python.

This tutorial will teach you how to define and call functions from different files to get started with Python modular programming.

A Python Module: What is it?

A Python file containing source code that can be imported into other programs is called a module. Therefore, a Python module is any file containing Python code ending in.py.

Creating a Python Function:

Let's start by defining the function to comprehend how to import and call it in Python from another file.

Things to Consider When Calling Functions

  • It is also possible to import several functions from various files.
  • Use the precise path from the root directory if you import functions from a file not in the same folder.
  • Therefore, you must import your file.py using the following command if it is located inside two directories.

It is possible to call user-defined functions from other files. It is possible for a function to be called and executed in a file other than its definition.

Plustwo() and falldist() are function definitions a separate script can use if a new file named myfunctions.py is created. The separate script must import the file and function names before using the functions. The file containing the function definitions must have the.py extension at the end. The file containing the defined functions cannot be imported if it does not have a.py extension.

The following code defines two functions that are included in the file myfuctions.py.

You can import this file, myfunctions.py, into a Jupyter Notebook or another script.

  • Remember that the files calling the functions and those containing the functions' definitions need to be in the same directory.
  • Include the import line, from filename import function_name, to use functions written in one file inside another. Despite the requirement for a.py extension in the file name, it should be noted that.py is not included in the filename when imported.

Below is the standard syntax for importing and using a function from a different [python file:

The following example is the utilizing of this syntax with the plustwo() function and the myfunctions.py file:

With the help of commas to separate the imported functions, multiple functions can be imported from the same file. Below is the standard syntax for importing and calling multiple functions from the same file:

See the below example of making use of the functions plustwo() and falldist() from the myfunctions.py file:

Output:

44.145, 5

Explanation

In the given code snippet, we have defined two functions: plustwo(n) adds 2 as the input n and returns the result, while falldist(t,g=9.81) computes the distance fallen given time t and optional gravitational constant g. After that, we have imported a function named function_name from a file named function_file and called it with the specified arguments. Next, it imports the plustwo() function from a file named myfunctions() and invokes it with the argument 3. Then, we have imported two functions, function_name1 and function_name2, from function_file and calls them with specified arguments. Lastly, we imported falldist() and plustwo() from myfunctions, computes the output of each function with the argument 3, and prints the output.

Another way of importing the functions from myfunctions.py in a different Python file or Jupyter Notebook is to import the complete file using import myfunctions, then invoke the functions using the below-provided syntax.

Another Example

Create a file named my_functions.py, and add another function called my_function(). You can create the files in the VS code editor or Jupyter Notebook and run the code.

Create a file named main.py. To retrieve the content from the main file, call my_function() from the main.py file.

Run the main.py file to see the content of the my_function from my_functions.

Output:

Calling my_function:
This is my function

Explanation

In the above example we have created two functions along with a main function. The function named `my_functions()` gives the output "This is my function". Next, we imported a function in the given code named `my_function` from a module named `my_functions`. Here, don't confuse with the names as we have declared `my_functions()` is defined but `my_function` is imported. The conditional statement `if _name_ == "_main_":` seems to have a typo as well, it should be `if __name__ == "__main__":` to execute the `main()` function when the script is run directly.

Conclusion:

Python easily provides us enables invoking functions from another file, which is a fundamental approach to ensure proper organization and modularity of code. You can easily reuse code by adding the import statement and specifying the module's function name. It is necessary to keep the files in the identical directory or adjust the import paths accordingly. This method promotes improved readability, maintainability, and code reusability.