Packaging - Outline¶
The importance of Python Packaging¶
Here are some of the reasons Python packaging is important:
- Dependency Management: Python projects often rely on external libraries and packages to extend their functionality.
- Reproducibility: Packaging allows developers to create a clear and consistent snapshot of their project's dependencies and environment, which can reproduce the exact environment needed for their project on different systems.
- Distribution and Sharing: Proper packaging enables developers to easily distribute their projects as Python packages to empower greater adoption.
- Versioning and Updates: This ensures that new versions of dependencies do not introduce breaking changes or unexpected behavior.
Python Pip:¶
Pros:¶
- Availability: Most distributions of Python come with pip preinstalled, making it readily available.
- Simple and Familiar: Pip's command-line interface is straightforward and easy to use, making it accessible to Python developers of all levels of experience.
Cons:¶
- Dependency Hell: Pip does not handle dependencies and version constraints as efficiently, leading to potential dependency conflicts and version mismatches.
- Lack of Lock File: Pip does not generate a lock file that ensures deterministic installations, which can lead to different versions of packages being installed on different systems.
- Virtual Environment Management: While virtual environments can be created with venv, managing them can be more cumbersome compared to modern package managers like Poetry.
- No Direct Build Support: Pip does not directly support building and packaging projects, requiring additional tools for that purpose.
Python Poetry:¶
Pros:¶
- Dependency Management: Poetry offers a robust and efficient dependency resolver, avoiding dependency conflicts and ensuring that your project's dependencies are compatible with each other.
- Lock File Support: Poetry generates a poetry.lock file, ensuring deterministic installations and consistent dependencies across different environments.
- Virtual Environment Integration: Poetry seamlessly integrates with virtual environments, simplifying the creation and management of isolated project environments.
- Modern Features: Poetry supports direct building and packaging of projects, making it easier to distribute Python packages.
- Toml-Based Configuration: Poetry uses a pyproject.toml file, which is more readable and user-friendly compared to requirements.txt and widely used across many popular libraries such as black, pylint, and mypy.
Cons:¶
- Additional Installation: Poetry is not pre-installed with Python, requiring an extra step to install it separately.
- Tends to be slower as Poetry verifies the compatibility of each package, often by downloading and inspecting it.
- Migration from Pip: Transitioning existing projects from pip to poetry may require some effort and adjustments, especially if there are complex dependencies.
Pip Overview¶
Pip is the default package manager for Python. The system provided is rather simple, but not nearly as feature rich as Poetry.
- Basic usage of pip for installing packages.
- Working with pip and requirements.txt:
- Creating a requirements.txt file to list project dependencies.
- Using pip to install dependencies from a requirements.txt file.
- Using pip: pip install -r requirements.txt.
- Updating dependencies
- update with --upgrade, e.g. pip install
--upgrade.
- update with --upgrade, e.g. pip install
- Viewing Dependency Mapping:
- Using pip: pip freeze and pip show.
Poetry Overview¶
Lab¶
Go through these high level steps.
- Create a new poetry project via
poetry init- Create a new folder called
netcrawler
- Create a new folder called
- Add the packaged
netmikoandnetutilsto it - Create the lock file and review the it
- Add the package
requeststo the environment- Bonus, can you do it without evaluating latest versions?
- Create a virtual environment
- Go into the Python interpreter shell of the virtual environment, and
import netutils