Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can I source a Python file from another Python file?
In Python, sourcing one Python file from another enables code reuse and modular programming. This article explores different methods to import and use Python files in your scripts.
Understanding Python File Importing
Python importing allows you to use functions, classes, and variables defined in one file within another. This promotes code reusability and helps organize code into logical modules based on functionality.
Using the import Statement
The import statement is the most common way to include external Python files or modules in your script.
Following is the syntax ?
import module_name
Example
Here's a basic example importing the built-in math module ?
import math print(math.sqrt(16)) print(math.pi)
4.0 3.141592653589793
For custom modules, first create a file called math_operations.py ?
# math_operations.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
Now in main.py, import and use the functions ?
# main.py import math_operations result = math_operations.add(5, 4) print(result) result = math_operations.subtract(10, 6) print(result)
9 4
Using from...import Statement
The from...import statement imports specific functions, classes, or variables directly, allowing you to use them without the module prefix.
Here is the syntax ?
from module_name import attribute_name
Example
Create math_operations.py with additional functions ?
# math_operations.py
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
def power(a, b):
return a ** b
Import only specific functions in main.py ?
# Simulating the import (functions defined inline for demo)
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
# Using the functions directly
result = multiply(2, 8)
print(result)
result = divide(10, 5)
print(result)
16 2.0
Using import...as Statement
The import...as statement imports a module with an alias, helping to shorten long module names and avoid naming conflicts.
Following is the syntax ?
import module_name as alias
Example
Import math_operations with an alias ?
# Simulating import with alias
def add(a, b):
return a + b
def subtract(a, b):
return a - b
# Using alias-like approach
math_ops_add = add
math_ops_subtract = subtract
result = math_ops_add(3, 8)
print(result)
result = math_ops_subtract(15, 9)
print(result)
11 6
Using exec() for Dynamic Imports
The exec() function dynamically executes Python code, including imports. This is useful for conditional imports at runtime.
Example
Dynamically import based on runtime conditions ?
# Simulate dynamic import behavior
import math
# Dynamic module selection
module_name = "math"
if module_name == "math":
result = math.sqrt(25)
print(f"Square root of 25: {result}")
# You can also use exec() for dynamic imports
exec("import random")
print(f"Random number: {random.randint(1, 10)}")
Square root of 25: 5.0 Random number: 7
Comparison of Import Methods
| Method | Syntax | Usage | Best For |
|---|---|---|---|
import |
import module |
module.function() |
Full module access |
from...import |
from module import func |
func() |
Specific functions |
import...as |
import module as alias |
alias.function() |
Long module names |
exec() |
exec("import module") |
module.function() |
Dynamic imports |
Conclusion
Use import for full module access, from...import for specific functions, and import...as for aliases. Reserve exec() for dynamic runtime imports when the module name isn't known beforehand.
