Skip to content

Python projects - Key concepts

Virtual Environments?

Virtual environments in Python are isolated spaces where you can install packages and dependencies for your projects without affecting other projects or the global Python installation. This ensures that different projects can have different versions of the same package without conflicts.

Key points:

  • Isolation: Keeps project dependencies separate without interfering with other projects.
  • Dependency Management: Allows different versions of packages for different projects.
  • Portability: Makes it easier to share projects with others without dependency issues.
  • Clean environments: Avoids package conflicts and clutter in the global Python environment.
  • Reproducibility: Facilitates consistent setups across different development and production environments.

Historically speaking, they have been managed by the python venv module. But as we are moving forward, we now use poetry.

Poetry

Poetry simplifies dependency management, package creation, and environment handling, making it easier to maintain Python projects.

Key Features:

  • Dependency Management
    Poetry ensures that your project’s dependencies are consistent and up-to-date. It uses a pyproject.toml file to specify dependencies and their versions, which helps avoid conflicts and ensures compatibility across different environments.

  • Virtual Environments
    Poetry creates and manages virtual environments automatically. This keeps your project dependencies isolated from your system Python environment, avoiding potential conflicts and making your projects more portable and reproducible.

  • Project Initialization
    Poetry simplifies the setup of new Python projects by guiding you through the creation of a pyproject.toml file, which includes metadata about your project (name, version, description) and its dependencies.

  • Build and Packaging
    Poetry streamlines the process of building and packaging Python projects. It can build your project into distributable formats like wheels or source distributions, ensuring that your package is ready for distribution on platforms like PyPI.

  • Version Management
    Poetry handles versioning of your project automatically. It can update the version in your project files based on semantic versioning principles, ensuring that each release of your project is well-documented and correctly versioned.

  • Dependency Resolution
    Poetry uses an advanced dependency resolver to ensure that your project's dependencies are compatible with each other. This minimizes the risk of dependency conflicts and ensures a smooth development experience.

  • Publishing
    Poetry makes it easy to publish your project to PyPI or other package repositories. It handles authentication and uploading, simplifying the process of sharing your code with the Python community.

Basic Commands

Action Command
Initialize a project poetry init
Install dependencies poetry install
Add a dependency poetry add <package>
Remove a dependency poetry remove <package>
Update dependencies poetry update
Run a script within the virtual environment poetry run <script>
Publish a package poetry publish
Check dependency status poetry show