Poetry Set Environment Variables

Posted on
Poetry offers an easy and straight forward way of setting environment variables. It allows developers to configure environment variables according to their needs, making it possible to customize the environment in which their applications are running. Poetry also provides a reliable and consistent way to access these variables, allowing them to take full advantage of the features and capabilities that they provide. In this article, we will explore how to set up environment variables using Poetry and discuss some common uses for them.

java – Environment variables in Eclipse – Stack Overflow
Poetry is a Python package manager that has gained immense popularity in recent years. It simplifies the packaging and distribution of Python applications. Poetry allows developers to create, manage, and publish Python packages with ease. Additionally, Poetry provides a set of environment variables that make it extremely flexible to work with.

In this article, we will explore how Poetry set environment variables can help us create a stable environment for our Python projects.

What are Environment Variables?

Environment variables are dynamic values that can affect the behavior of running processes. In simple terms, they are variables that define the operating system’s behavior and settings. Environment variables can be set by the user or by the system itself.

Poetry provides several environment variables to enhance its functionality and allow greater customization of your development environment.

Setting Poetry Environment Variables

To set an environment variable in Poetry, we use the export command or add them to our shell configuration file (.bashrc or .zshrc). Here’s an example:

export POETRY_CACHE_DIR=$HOME/.cache/pypoetry
export POETRY_PYPI_INDEX_URL=https://pypi.org/simple

The first variable sets virtual environments inside the project directory instead of globally. This helps keep dependencies isolated from each project and avoids conflicts between versions.

The second variable sets the cache directory for Poetry’s package downloads. By default, it is set to ~/.cache/pypoetry/, but we can customize it based on our preferences.

Lastly, the third variable sets PyPI as our default package index when installing packages using Poetry.

Using Environment Variables in Your Projects

Now that we’ve seen how to set up these environment variables let’s look at how they can be used in our project code using Poetry.

To activate these settings in a particular project, simply run:

poetry config virtualenvs.in-project true
poetry config cache-dir ~/.cache/pypoetry/
poetry config repositories.pypi url https://pypi.org/simple/

These settings will be stored in the pyproject.toml file at the root of your project directory. This means that anyone who clones your project can run it with the same environment configuration.


In conclusion, Poetry set environment variables are an excellent way to customize and optimize our Python development environment. With these variables, we can configure our virtual environments, package index, and cache directory according to our preferences, making it easier to manage multiple projects simultaneously. By using these variables, we can make sure that our development environment is consistent across different machines and platforms.

Python Poetry tutorial: How to use Python Poetry
This short Python Poetry tutorial is about installation Poetry on Ubuntu-based Linux distribution, how to create a Python project with Poetry, how to activate virtual environment with Python Poetry, install and remove Python packages. Poetry is a Python package and virtual environments manager. Follow me @: Telegram: …

Leave a Reply

Your email address will not be published. Required fields are marked *