In this tutorial, you'll learn about organizing your Python code into reusable modules and packages. We'll explore how to import these modules using various techniques, create your own packages, and manage dependencies using the pip package manager and requirements.txt files. Understanding these concepts is crucial for developing well-structured and maintainable Python applications.
Python modules are files containing Python definitions and statements. They allow you to organize your code into separate files, making it easier to manage and reuse. Packages are collections of modules that can be distributed and shared. The pip package manager is a tool used to install and manage third-party packages in Python. Together, these tools help you build complex applications by leveraging existing libraries and organizing your own code.
The simplest way to import a module is using the import statement. This imports the entire module into your namespace.
1# basic_import.py2import math34result = math.sqrt(16)5print(result) # Output: 4.0
4.0
You can import specific attributes from a module using the from ... import ... syntax. This is useful when you only need certain functions or classes.
1# from_import.py2from math import sqrt, pi34result = sqrt(16)5print(result) # Output: 4.067print(pi) # Output: 3.141592653589793
4.0 3.141592653589793
You can use the as keyword to give a module or attribute an alias, which can make your code cleaner and more readable.
1# import_alias.py2import math as m34result = m.sqrt(16)5print(result) # Output: 4.067from datetime import datetime as dt89current_time = dt.now()10print(current_time) # Output: 2023-09-25 12:34:56.789012
4.0 2023-09-25 12:34:56.789012
A package is a way of structuring Pythonβs module namespace by using βdotted module names.β For example, the module name mypackage.mymodule would indicate that a submodule named mymodule is in a package named mypackage.
To create a package, you need to create an __init__.py file inside the directory. This file can be empty or contain initialization code for the package.
mypackage/ init.py module1.py module2.py
To use modules from a package, you can import them using dot notation.
1# use_package.py2from mypackage import module1, module234module1.function1()5module2.function2()
The pip package manager is used to install and manage third-party Python packages. You can install a package using the pip install command.
$ pip install requestsCollecting requestsDownloading requests-2.28.1-py3-none-any.whl (60 kB)|ββββββββββββββββββββββββββββββββ| 60 kB 2.4 MB/sInstalling collected packages: requestsSuccessfully installed requests-2.28.1
You can list all installed packages using pip list.
$ pip listPackage Version---------- -------requests 2.28.1...
A requirements.txt file is used to specify the dependencies of a Python project. This makes it easy for others (or yourself) to install all required packages.
You can generate a requirements.txt file using pip freeze.
$ pip freeze > requirements.txt
To install all dependencies from a requirements.txt file, use the following command:
$ pip install -r requirements.txtCollecting requests==2.28.1Downloading requests-2.28.1-py3-none-any.whl (60 kB)|ββββββββββββββββββββββββββββββββ| 60 kB 2.4 MB/sInstalling collected packages: requestsSuccessfully installed requests-2.28.1
Let's create a simple package with two modules and demonstrate how to use them.
myapp/ init.py utils.py main.py
1# myapp/utils.py2def add(a, b):3return a + b45def subtract(a, b):6return a - b
1# myapp/main.py2from .utils import add, subtract34result1 = add(5, 3)5print(f"Addition: {result1}") # Output: Addition: 867result2 = subtract(5, 3)8print(f"Subtraction: {result2}") # Output: Subtraction: 2
To run the application, navigate to the parent directory of myapp and execute:
$ python -m myapp.mainAddition: 8Subtraction: 2
import, from ... import ..., and as for different importing techniques.__init__.py file to your directory.pip install to manage third-party packages.requirements.txt file and use pip freeze to generate it.In the next tutorial, we'll explore how to structure your Python code using the if __name__ == '__main__': construct. This will help you write scripts that can be both imported as modules and executed as standalone programs. Stay tuned!