How do you activate virtual environment in poetry?

  • I am on the latest Poetry version.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).
  • OS version and name: Arch Linux x86_64
  • Poetry version: 1.1.2

Issue

After activating virtual env using poetry shell, running deactivate command deactivates the env, but poetry thinks it's still active.

Steps to reproduce:

Before,

$ poetry --version Poetry version 1.1.2 $ which python /usr/bin/python

Activate the env,

$ poetry shell -vvv Using virtualenv: /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8 Spawning shell within /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8 . /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8/bin/activate $ . /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8/bin/activate $ which python /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8/bin/python

Deactivate using deactivate, It says it's already active.

$ deactivate $ which python /usr/bin/python $ poetry shell -vvv Using virtualenv: /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8 Virtual environment already activated: /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8

It works properly if exit were used instead of deactivate.

The Python package management tool you needed yesterday

Photo by Jared Rice on Unsplash

What is the first word that comes to mind with Python? Numpy, Pandas or SciKit-Learn? What about Poetry? No, not that kind of poetry.

Poetry : Python packaging and dependency management made easy

Like a beautiful poem recited over a moonlit night, Poetry is here to soothe the nerves you get at the thought of reproducing your Python environment. Well at least for me.

Introduction

Poetry is a Python dependency management tool that manages virtual environment configurations as well as all the packages in your project. Think of it as a tool that combines all the best features of Anaconda and venv. Poetry enables anyone the ability to produce deterministic builds of Python execution environment.

Poetry automatically creates and manages virtual environments in addition to adding or removing packages as you install/uninstall packages from your project.

Installation

We will be working from the terminal in the command line. This will mean you need to open a Terminal window on mac or a Powershell window on a Windows machine.

  1. There are different commands for mac and windows so choose the appropriate command from below.

Mac:

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

Windows:

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -

How do you activate virtual environment in poetry?

curl in terminal (Image by author)

2. Confirm that poetry has been installed correctly by typing:

poetry --version

How do you activate virtual environment in poetry?

poetry version (Image by author)

If you see a poetry version number, great!! This means you are ready to use poetry 🎉🎉🎉 .

Creating your first virtual environment

I am going to assume you have installed the version of Python you require. If you have not, pause here and install the version of Python you need from the main Python website.

  1. Once you have Python and Poetry installed, change the working directory to your main project directory.
cd <PROJECT_FOLDER_LOCATION>

2. Initialise Poetry with the command:

poetry init

3. Work through the guide to create a pyproject.toml config file which will define the configurations of your Python environment. I have defined my Python version to be 3.9.7, change this value to the Python version you require. In my example below, I have chosen to not define the main dependencies interactively as I prefer to add them after.

How do you activate virtual environment in poetry?

4. Install the Python virtual environment.

poetry install

5. Add Python packages to your project by replacing <PACKAGE_NAME> with the name of the package you wish to add. You can add multiple packages by separating each name with a space.

poetry add <PACKAGE_NAME>

6. Activate your Python virtual environment and you should see the virtual environment’s unique identifier preceding your current folder location as shown below.

poetry shell

How do you activate virtual environment in poetry?

Successful activation of Python virtual environment. (Image by author)

7. To link your newly created virtual environment to Jupyter use the command below; replace poetry_python_env with the name, you wish to call it in Jupyter.

python -m ipykernel install --user --name=poetry_python_env

Exporting your Python build

Poetry makes it easy to share deterministic builds of Python environments. To do so, all you need to do is share or push the poetry.lock and pyproject.toml file to a repository.

As shown in the example below, the project working directory now contains a file called poetry.lock and pyproject.toml.

project
├── poetry.lock
└── pyproject.toml

In a terminal window, navigate to the project directory and install the virtual environment:

poetry install

It is as easy as that.

If you have reached the end of this blog, I hope you have gained a new skill in Python package management. Please do leave a like and follow me on Medium and Twitter (@iLloydHamilton) for more data science-related content.

Watch this space.

More content at plainenglish.io. Sign up for our free weekly newsletter here.

Does poetry create a virtual environment?

Poetry is also bound to the pyproject. toml file and its path to generate a new environment. poetry will create a new virtual environment but this is not exactly the same as changing just some project deps. This will generate a new environment with just the asked dependencies changed.

Where are poetry virtual environments stored?

If not set explicitly, poetry by default will create virtual environment under {cache-dir}/virtualenvs or use the {project-dir}/. venv directory when one is available. If set to true , the virtualenv will be created and expected in a folder named . venv within the root directory of the project.

How do you activate your environment?

Use the terminal or an Anaconda Prompt for the following steps:.
Create the environment from the environment.yml file: conda env create -f environment. yml. ... .
Activate the new environment: conda activate myenv..
Verify that the new environment was installed correctly: conda env list..

What is an example of a virtual environment?

Email, chat, and web-based document sharing applications are all examples of virtual environments. Simply put, it is a networked common operating space.