To use a module, you can import it using the import
statement. For example, to import the built-in math
module, you would write:
import math
# Now you can use functions and constants from the math module
print(math.sqrt(16)) # Output: 4.0
print(math.pi) # Output: 3.141592653589793
If you only need specific functions or variables from a module, you can import them directly using the from ... import ...
statement:
from math import sqrt, pi
# Now you can use sqrt and pi directly without the math prefix
print(sqrt(16)) # Output: 4.0
print(pi) # Output: 3.141592653589793
You can import everything from a module using the asterisk *
:
from math import *
# Now you can use all functions and constants from the math module
print(sqrt(16)) # Output: 4.0
print(pi) # Output: 3.141592653589793
However, this practice is generally discouraged because it can lead to conflicts and make the code less readable.
You can give a module or a function an alias using the as
keyword. This is useful when you have long module names or potential naming conflicts:
import math as m
print(m.sqrt(16)) # Output: 4.0
print(m.pi) # Output: 3.141592653589793
from math import sqrt as square_root
print(square_root(16)) # Output: 4.0
If you have a Python file named my_module.py
in the same directory as your main script, you can import it directly:
# Content of my_module.py
def greet(name):
return f"Hello, {name}!"
# Content of main.py
import my_module
print(my_module.greet("Alice")) # Output: Hello, Alice!
If your module is in a subdirectory, you need to add an __init__.py
file in that directory to make it a package. Suppose you have the following structure:
my_project/
main.py
my_package/
__init__.py
my_module.py
You can import my_module
like this:
# Content of my_module.py
def greet(name):
return f"Hello, {name}!"
# Content of main.py
from my_package import my_module
print(my_module.greet("Alice")) # Output: Hello, Alice!
To use external libraries, you need to install them using a package manager like pip
. For example, to install and use the requests
library:
pip install requests
Then, you can import and use it in your script:
import requests
response = requests.get('https://api.github.com')
print(response.status_code) # Output: 200
Python uses the sys.path
list to determine where to look for modules. By default, sys.path
includes:
1. The directory containing the input script (or the current directory).
2. PYTHONPATH
(a list of directory names, with the same syntax as the shell variable PATH
).
3. The installation-dependent default.
You can modify sys.path
within your script to include additional directories:
import sys
sys.path.append('/path/to/your/module')
import your_module
# Standard library imports
import os
import sys
# Related third party imports
import requests
# Local application/library-specific imports
import my_module
Using the import
statement effectively is crucial for writing clean, modular, and maintainable Python code. By understanding how to import built-in modules, third-party libraries, and your own modules, you can leverage existing code and resources to build powerful applications. Remember to follow best practices for importing to keep your code organized and readable.
Jorge García
Fullstack developer