Python script completed, but is not setting poetry environment variables

111 views Asked by At

Hello I am trying to run the following python script against an already initialized virtual environment:

import subprocess

# List of Poetry commands to be executed
poetry_commands = [
    "poetry install",
    "export PARAM_1=<value>",
    "export PARAM_2=<value>",
    .
    .
    "export PARAM_N=<value>",
    "poetry run start"
]
# Iterate over each Poetry command and execute it
for command in poetry_commands:
    try:
        # Run the command in the shell, wait for it to finish
        subprocess.run(command, shell=True, check=True)
    except subprocess.CalledProcessError as e:
        print(f"Command '{command}' failed with error code {e.returncode}")

For some reason the script completes, however the poetry run start command fails due to export params not being applied. How can I assure that the export env variables are correctly set after poetry install?

2

There are 2 answers

2
Mikko Ohtamaa On

You are running each command in its own separate shell.

Each shell is created, running an export statement and then destroyed,

To correctly pass environment variables, pass env to subprocess.

0
Антон Георгиев On

Managed to workaround the problem by using os.environ to set the env variables:

import subprocess
import os

poetry_commands = [
    "poetry install",
    "poetry run start"
]

# Set environment variables using os.environ
os.environ["PARAM_1"] = "<value_1>"
os.environ["PARAM_2"] = "<value_2>"

for command in poetry_commands:
    try:
        subprocess.run(command, shell=True, check=True)
    except subprocess.CalledProcessError as e:
        print(f"Command '{command}' failed with error code {e.returncode}")