DEV Community

Cover image for Simplify Python Dependency Management: Creating and Using Virtual Environments with Poetry
Rain Leander
Rain Leander

Posted on

2 2 1

Simplify Python Dependency Management: Creating and Using Virtual Environments with Poetry

As a Python developer, managing dependencies and libraries can become a bit of a hassle. It's important to keep track of different versions of packages and ensure that they work together seamlessly. Virtual environments and package managers can help to solve these issues.

Virtual environments are isolated Python environments where you can install packages and libraries without affecting the system-wide installation. You can have multiple virtual environments with different package versions and dependencies to work on different projects simultaneously. One of the most popular package managers for Python is Poetry, which simplifies package management and streamlines dependency resolution.

In this post, we will walk you through how to create and use virtual environments in Python with Poetry.

Step 1: Install Poetry

The first step is to install Poetry on your system. Poetry can be installed on any operating system that supports Python. To install Poetry, you can use the following command in your terminal:

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a new project

Once you have installed Poetry, create a new directory for your project and navigate into it. Then, run the following command to create a new project with Poetry:

poetry init
Enter fullscreen mode Exit fullscreen mode

This command will create a pyproject.toml file that contains information about your project and its dependencies.

Step 3: Create a virtual environment

To create a virtual environment with Poetry, run the following command:

poetry env use python
Enter fullscreen mode Exit fullscreen mode

This command will create a new virtual environment and activate it. You can also specify a specific version of Python to use in your virtual environment by running:

poetry env use /path/to/python
Enter fullscreen mode Exit fullscreen mode

Step 4: Add dependencies

To add dependencies to your project, you can use the following command:

poetry add package-name
Enter fullscreen mode Exit fullscreen mode

This command will install the package and its dependencies in your virtual environment and update your pyproject.toml file.

You can also specify the version of the package that you want to install:

poetry add package-name==1.0.0
Enter fullscreen mode Exit fullscreen mode

Step 5: Install dependencies

To install the dependencies of your project, you can run the following command:

poetry install
Enter fullscreen mode Exit fullscreen mode

This command will install all the dependencies listed in your pyproject.toml file.

Step 6: Use the virtual environment

To use the virtual environment, you need to activate it first:

source ~/.poetry/env
Enter fullscreen mode Exit fullscreen mode

This command will activate the virtual environment and you can start working on your project. To deactivate the virtual environment, simply run:

deactivate
Enter fullscreen mode Exit fullscreen mode

That's it!

You can now create and use virtual environments in Python with Poetry. With this approach, you can keep your projects isolated and ensure that they work seamlessly without any dependency issues.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (2)

Collapse
 
aydrian profile image
Aydrian

Oh! I just started using Poetry. I'm a fan. For step 6 you can just do poetry shell to activate the environment. I think it will also handle step 3 for you. But much more enjoyable than pip and all that.

Collapse
 
rainleander profile image
Rain Leander

Brilliant!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay