Programmation Orientée Objet en Python
  • Back to Main Website
  • Home
  • Introduction: Histoire et Concepts
    • Introduction: Histoire et Concepts
    • Histoire de la programmation
    • Première Structuration des données
    • Naissance de la POO
    • Python: tout n’est qu’objet
    • Python : Simplicité des objets et performance sous-jacente
    • Classes en Python : Concepts fondamentaux

    • Travaux Pratiques
    • Construire sa propre Liste
    • Construire son propre DataFrame
  • Encapsulation, Héritage, Composition et Dunder
    • Encapsulation, Heritage, Composition et Dunder
    • Encapsulation en Python
    • Héritage en Python : Concept et intérêt
    • Héritage vs Composition
    • Méthodes Dunder en Python
    • Python call Method: A Fun Exploration

    • Travaux Pratiques
    • TP: Heritage avec le pricing d’option
    • TP : Ajouter des méthodes dunder à DataFrameSimple
    • TP : Étendre la classe Liste avec des méthodes dunder
    • TP: Dunder Method with Tensor for Automatic Differentiation
  • Polymorphisme et Surcharge
    • Polymorphisme et Surcharge
    • Polymorphism in Object-Oriented Programming
    • Polymorphism in Python: Function Overloading and Type Checking
    • Class Creation: Standard vs type()
    • Type Hinting, Typing Module, and Linters in Python
    • Abstract Classes
    • Protocol Classes

    • Travaux Pratiques
    • TP
  • Decorators
    • Design Patterns
    • The decorator pattern
    • Decorator Practically
    • Built-in Decorators and Standard Library Decorators in Python
    • Practical Decorators in Python Libraries

    • Travaux Pratiques
    • TP: Monte Carlo Option Pricing with Decorators
    • TP: Optimizing Heston Model Monte Carlo Simulation
  • Project Management and Packaging
    • Project and Package
    • Organizing Python Projects
    • Understanding imports
    • Python Package Management and Virtual Environments
    • Unit Testing in Python

    • Travaux Pratiques
    • TP: Creating a Linear Regression Package
  • Design Patterns
    • OOP Design Patterns
    • Python-Specific Design Patterns
    • Creation Design Patterns
    • Structural Design Patterns
    • Behavioral Design Pattern

    • Travaux Pratiques
    • TP
  • Sujets de Projets possibles
    • Projets
    • Projets POO - 2024-2025
  • Code source
  1. Python Package Management and Virtual Environments
  • Project and Package
  • Organizing Python Projects
  • Understanding imports
  • Python Package Management and Virtual Environments
  • Unit Testing in Python
  • Travaux Pratiques
    • TP: Creating a Linear Regression Package

On this page

  • Python Package Management and Virtual Environments
    • Package Management Tools:
    • Virtual Environment Tools:
    • Using pip and venv
    • Introduction to Poetry
    • The pyproject.toml File
    • Building and Publishing

Python Package Management and Virtual Environments

Cours
Fondamentaux
Learn to create a package and manage virtual environment
Author

Remi Genet

Published

2024-10-21

Python Package Management and Virtual Environments


When developing Python projects, managing dependencies and isolating environments are crucial. There are several tools available for these tasks:

Package Management Tools:

  1. pip: The standard package installer for Python.
  2. Poetry: A modern dependency management and packaging tool.
  3. Conda: A package, dependency, and environment management system (popular in data science).

Virtual Environment Tools:

  1. venv: Built-in Python module for creating virtual environments.
  2. pyenv: Allows you to easily switch between multiple versions of Python.
  3. virtualenv: A tool to create isolated Python environments (precursor to venv).

Let’s focus on pip, venv, and an introduction to Poetry.

Using pip and venv

  1. Create a virtual environment:

    python -m venv myenv
  2. Activate the environment:

    • Windows: myenv\Scripts\activate
    • macOS/Linux: source myenv/bin/activate
  3. Install packages:

    pip install package_name
  4. Create a requirements.txt file:

    pip freeze > requirements.txt
  5. Install from requirements.txt:

    pip install -r requirements.txt

Introduction to Poetry

Poetry is a more modern tool that combines dependency management, package building, and publishing.

  1. Install Poetry:

    pip install poetry
  2. Create a new project or initialize an existing one:

    poetry new myproject
    # or
    poetry init
  3. Add dependencies:

    poetry add package_name
  4. Install dependencies:

    poetry install

The pyproject.toml File

Poetry uses pyproject.toml for project configuration. Here’s a simple example:

[tool.poetry]
name = "myproject"
version = "0.1.0"
description = "A sample Python project"
authors = ["Your Name <you@example.com>"]

[tool.poetry.dependencies]
python = "^3.9"
requests = "^2.26.0"

[tool.poetry.dev-dependencies]
pytest = "^6.2.5"

This file defines your project’s metadata and dependencies.

Building and Publishing

Building a package means creating a distribution that can be installed by pip. Publishing means uploading this distribution to a package index like PyPI.

  1. Build your package:

    poetry build

    This creates distribution files in the dist/ directory.

  2. Publish to PyPI:

    poetry publish

    This uploads your built package to PyPI, making it available for others to install.

Poetry simplifies these processes, handling the complexities of building and publishing for you.

Back to top
Understanding imports
Unit Testing in Python

Programmation Orienté Object en Python, Rémi Genet.
Licence
Code source disponible sur Github

 

Site construit avec et Quarto
Inspiration pour la mise en forme du site ici
Code source disponible sur GitHub