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


As you explore the world of Python programming, you'll discover that modular and reusable code is indispensable. In Python, it is possible that you can source one Python file from another; this opens up a whole new avenue of possibilities for organizing code and sharing functionality between scripts. In this article, we will navigate through four different ways to import Python files, and each one of them has its own strengths and use cases. It is immaterial if you are a beginner or an expert in Python coding, either way, you will be guided through each method with step-by-step explanations and real-world code examples. By the end of this article, you'll notice that you have mastered the art of sourcing Python files; this has by now empowered you to write more organized and efficient Python code. Let us begin this journey where we learn to enhance the modularity and reusability of Python code!

Understanding Python File Importing

Before we get started right away with the code examples, let us try to understand the concept of importing of Python files. In Python, the process of importing makes it possible for you to use functions, classes, and variables defined in one Python file within another. This powerful feature goes a long way in helping code reusability and modularity; this enables you to split your code into multiple files based on functionality.

Using import Statement

One of the most common ways to import a Python file is by using the import statement.

Let us assume we have a file named math_operations.py containing the following code with several functions defined:

In this code example, we make use of the import statement to extract the functionalities of the math_operations.py file in another file, the main.py file. We then see to it that the functions add() and subtract() are called from the math_operations module within the main.py file.

# math_operations.py

def add(a, b):
   return a + b

def subtract(a, b):
   return a - b

Now, in another file, let's assume main.py, we go on to use the functions from math_operations.py.

Example

# main.py
import math_operations

result = math_operations.add(5, 4)
print(result)  

result = math_operations.subtract(10, 6)
print(result) 

Output

9
4

Using from...import Statement

You must know that the 'from...import' statement makes it possible for you to import specific functions or variables from a Python file.

In this example, we proceed to deploy the 'from...import' statement to selectively import only the multiply() and divide() functions from the math_operations.py file. This technique again permits us to use the functions directly without referencing the module name.

Continuing with the math_operations.py file:

# math_operations.py

def multiply(a, b):
   return a * b

def divide(a, b):
   return a / b

Now, in main.py, we can import only the functions we need:

Example

# main.py
from math_operations import multiply, divide


result = multiply(2, 8)
print(result)  

result = divide(10, 5)
print(result)  

Output

16
2.0

Using import...as Statement

The 'import...as' statement, it will be seen, permits you to provide an alias for the imported module or function; this makes the code more concise and compact.

Here, in this code snippet, we make use of the 'import...as' statement to import the math_operations module with the alias math_ops. We can now use a shorter and more convenient name when calling the functions.

Example

# main.py
import math_operations as math_ops

result = math_ops.add(3, 8)
print(result) 

result = math_ops.subtract(15, 9)
print(result)  

Output

11
6

Using exec()

The exec() function is a very useful function and it is indeed used to dynamically import Python files.

In this code, we make use of the exec() function, and a Python file is dynamically imported as indicated by the file_to_import variable. This approach or technique can be very handy when you need to import files based on runtime conditions.

Example

# main.py
file_to_import = "math_operations.py"
exec(f"import {file_to_import} as math_ops")

result = math_ops.add(3, 6)
print(result) 

Output

9

Importing Python files is a basic and fundamental technique that empowers you to write modular and reusable code. It does not matter whether you use the import statement, from...import, import...as, or exec(), each method offers unique advantages based on your code organization and project requirements.

As you continue your Python journey, realize and utilize the power of importing files to create more organized and efficient codebases. The skill of sourcing Python files hopefully elevates your coding experience and empowers you to build impressive Python projects.

Updated on: 28-Jul-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements