Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utelize the poetry shell command #24

Open
MaKaNu opened this issue Feb 15, 2024 · 2 comments
Open

Utelize the poetry shell command #24

MaKaNu opened this issue Feb 15, 2024 · 2 comments

Comments

@MaKaNu
Copy link

MaKaNu commented Feb 15, 2024

I recognized, that my Variables are available with poetry run. But if I use poetry shell the variables are still using the variables I specified global.

poetry shell should also override the variables.

@waketzheng
Copy link

@MaKaNu After try a lot, I found that poetry shell was able to override variables in Windows.

Bash/Zsh/Fish in ubuntu/centos/MacOS are able to reproduce, this is because poetry-dotenv-plugin set variables into os.environ before virtual environment activated, and poetry activate virtual environment by pexpect.spawn which will run the shell initial file (such as: .bashrc/.zshrc/.profile) that specified global variables again.

One solution is to send export command into interactive environment:

# Add the following lines to `~/.local/share/pipx/venvs/poetry/lib/python3.10/site-packages/poetry/utils/shell.py`
        import dotenv
        if variables := dotenv.dotenv_values(Path.cwd()/'.env').items():
            export_cmd = ' && '.join(f'export {k}={v!r}' for k, v in variables)
            c.sendline(export_cmd)
image

@waketzheng
Copy link

Or use source .env:

        import dotenv
        path = os.environ.get("POETRY_DOTENV_LOCATION") or dotenv.find_dotenv(usecwd=True)
        if dotenv.dotenv_values(path):
            c.sendline(f"source {path}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants