![]() |
Python Poetry is a comprehensive dependency management and packaging tool that specifically enhances the handling of Python projects. If compared to such tools as pip and virtualenv, Poetry gives a more precise and optimized workflow for managing different aspects of the project, both its dependencies and the environments. Another prominent strength of Poetry is flexibility with multiple environments, which may be desirable if you need to deploy your project to different environments, reproduce a specific state, or test all possible variations. Why Multiple Environments in Python Poetry?Before diving into the specifics of Poetry, it’s important to understand why multiple environments are necessary: 1. Isolation: This means that each project might have different dependencies to the packages it requires to be loaded hence reducing conflicts between packages. 2. Reproducibility: Maintains a consistent project development environment where any dependencies your project relies on operate as expected. 3. Testing: This enables you to verify your project in a number of environments so as to check on the compatibility differences. To begin with, there are various types of environment profiles; main, developmental, testing, and staging. Setting Up PoetryIf you haven’t already installed Poetry, you can do so using the following command: curl -sSL https://install.python-poetry.org | python3 -
Once installed, you can verify the installation with: poetry --version
Creating a New ProjectTo create a new project with Poetry, use the new command: poetry new my_project
This command creates a new directory with the project structure, including a pyproject.toml file, which is the heart of a Poetry-managed project. ![]() my_project directory Managing DependenciesTo add dependencies to your project, navigate to the project directory and use the add command: cd my_project This command updates the pyproject.toml file with the requests package and its version. Setting Up Different Environment ProfilesDevelopment vs. Production EnvironmentsPoetry provides the ability to set up the conditions for dependencies for one environment or another, for example, development and production. Adding Development DependenciesDevelopment dependencies are packages needed during development, such as testing or linting tools: poetry add --dev black flake8
This command both installs poetry and adds black and flake8 as development dependencies. Installing Dependencies for ProductionWhen setting up a production environment, you might want to exclude development dependencies: poetry install --no-dev
Using Environment MarkersEnvironment markers help specify conditions under which dependencies are installed: [tool.poetry.dependencies] Custom Virtual EnvironmentsPoetry allows you to create custom virtual environments by specifying the path to the environment or Python version: poetry env use /path/to/custom/python Switching Between EnvironmentsManaging Multiple ProjectsIf there are several projects it may be necessary to move from one environment to another for work or testing. Each project’s environment is managed independently: cd project_one Listing and Using EnvironmentsPoetry keeps track of environments and allows you to switch between them: poetry env list
To activate a specific environment from the list: poetry env use path/to/specific/environment
ConclusionUsing Python Poetry to manage multiple environments is rather simple and offers a lot of capabilities. Using such features as an isolated environment, markers indicating the environment the project is tested in, and development/production dependencies, you can be sure that your projects are properly arranged, fully replicable, and easily manageable. The technicalamentation of Poetry helps to minimize the number of matters in Python project management and allows to concentrate on creation of code and not the setup and administering of the project. FAQsQ1: How do I specify a different Python version for my project?
Q2: How can I check which dependencies are installed in my environment?
Q3: How do I remove a dependency from my project?
Q4: Can I use Poetry with existing projects?
Q5: How do I handle environment variables in Poetry?
|
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 20 |