
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Source a Python File from Another Python File
In Python, it is possible to source one Python file from another, which enables us to share data between scripts. In this article, we will go through the different ways to source a Python file from another Python file.
Understanding Python File Importing
In Python, the process of importing makes to use functions, classes, and variables defined in one Python file within another. This helps us in code reusability and enables us to split our code into multiple files based on functionality.
Using the import Statement
One of the most common ways to import a Python file is by using the import statement. The import statement is used to include external Python files or modules into the current script.
Following is the syntax of using the import Statement -
# math_operations.py import module_name
Example
Here is the basic example in which we will import the math module and use it later in the Python code -
import math print(math.sqrt(16)) # Output: 4.0
Below is the output of the above code ?
4.0
Now, in another file, let's assume main.py and later we go on to use the functions from math_operations.py script -
# main.py import math_operations result = math_operations.add(5, 4) print(result) result = math_operations.subtract(10, 6) print(result)
Following is the output of the above code ?
9 4
Using from...import Statement
The from ... import statement is used to import specific attributes such as functions, classes, or variables directly from a module. This allows us to access them without prefixing the module name.
Here is the syntax of using the from ... import Statement -
from module_name import attribute_name
Example
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.
# 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 as follows -
# main.py from math_operations import multiply, divide result = multiply(2, 8) print(result) result = divide(10, 5) print(result)
Following is the output of the above code ?
16 2.0
Using import...as Statement
The import...as statement allows us to import a module or object with an alias. This is helps to shorten long module names, avoid naming confusions, and make code cleaner and more readable.
Following is the syntax of using the import...as statement -
import module_name as alias
Example
In this example, we will import math_operations script with the alias name math_ops in the current Python script -
# main.py import math_operations as math_ops result = math_ops.add(3, 8) print(result) result = math_ops.subtract(15, 9) print(result)
Here is the output of using the import ... as Statement -
11 6
Using exec()
The Python exec() function is used to dynamically import Python files. This approach or technique can be very handy when we need to import files based on runtime conditions.
Example
In this example, we make use of the exec() function, and a Python file is dynamically imported as indicated by the file_to_import variable -
# 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)
Following is the output of the above code, which uses exec() function -
9