How do I run poetry install in a subprocess without reference to the current virtualenv?

I am writing some auto setup code in a command line tool that we have.
This command line tool is installed with poetry, so when someone is invoking the command line tool, it is from within a virtual environment setup by poetry.

The command I am trying to invoke is poetry install for another directory.
I want this command to create a new virtualenv for the other directory.

What I am currently doing is

import subprocess

other_directory = "/some/path"

subprocess.run([f"cd {other_directory} && poetry install && poetry env info"], cwd=other_directory, shell=True, check=True) 

But that always installs the dependencies from the other repo into the virtualenv that I am using to run this script. I have also tried to use the path to the system poetry instead of the one in the virtualenv, but that doesn’t work either.
The poetry env info command always outputs the info for the current virtualenv:

Virtualenv
Python:         3.10.9
Implementation: CPython
Path:           /home/veith/.cache/pypoetry/virtualenvs/my-cli-tool-Qe2jDmlM-py3.10
Executable:     /home/veith/.cache/pypoetry/virtualenvs/my-cli-tool-Qe2jDmlM-py3.10/bin/python
Valid:          True

And if I manually cd to the other directory and run poetry env info, it always shows that no virtualenv is set up:

Virtualenv
Python:         3.10.9
Implementation: CPython
Path:           NA
Executable:     NA

The output I am looking for here would be:

Virtualenv
Python:         3.10.9
Implementation: CPython
Path:           /home/veith/.cache/pypoetry/virtualenvs/my-other-directory-HASH-py3.10
Executable:     /home/veith/.cache/pypoetry/virtualenvs/my-other-directory-HASH-py3.10/bin/python
Valid:          True

Is there any way to achieve this? I don’t even understand where poetry knows the current virtualenv from if I set the cwd keyword in the subprocess call.

>Solution :

Poetry does not create a new venv if it detects, that the current environment is a venv. This is done by looking for the VIRTUAL_ENV environment variable.

If you unset the variable for the subprocess, poetry will happily create a new venv for you.

E.g.:

import os
import subprocess

other_directory = "/some/path"
current_env = os.environ.copy()

if "VIRTUAL_ENV" in current_env:
    del current_env["VIRTUAL_ENV"]

subprocess.run([f"cd {other_directory} && poetry install && poetry env info"], cwd=other_directory, shell=True, check=True, env=current_env) 

Leave a Reply