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 -
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
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
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
Step 4: Add dependencies
To add dependencies to your project, you can use the following command:
poetry add package-name
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
Step 5: Install dependencies
To install the dependencies of your project, you can run the following command:
poetry install
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
This command will activate the virtual environment and you can start working on your project. To deactivate the virtual environment, simply run:
deactivate
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.
Top comments (2)
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.Brilliant!