python-poetry / poetry-plugin-shell

MIT License
0 stars 1 forks source link

Allow `--force` activate `poetry shell` when poetry fails to activate it #14

Open aalok-sathe opened 2 years ago

aalok-sathe commented 2 years ago

Issue

Often times poetry keeps thinking I have a shell already activated, and I don't. This is widely reported by others too (#4622, python-poetry/poetry-plugin-shell#21, python-poetry/poetry#5050, python-poetry/poetry#3136). I also do not have any conda environment activated (not even base). In this case, I am just forced to use the full path to the venv to run the activate script manually. I propose that we should be able to do this in poetry, when it inevitable fails to activate the venv, to --force activate the environment, so that even if it is "already active" (not actually), it will still be "activated" (for the first time).

Current output

$ poetry shell -vvv
Using virtualenv: ...-py3.8
Virtual environment already activated: ...-py3.8

Proposed Usage example:

$ poetry shell
Virtual environment already activated: ...

$ poetry shell -f
Force-activating Virtual environment: ...
bmarroquin commented 2 years ago

poetry shell starts a new shell and activates the virtual environment. Running deactivate will deactivate the virtual environment but not close the shell. To close the shell run exit. This link has more info. Poetry tracks whether the virtual environment is activated by tracking that the shell is still open. Maybe the message needs to be better.

if shell:
    if activated: 
        print("Virtual environment already activated: ...")
    else:
        print("Poetry shell is already running and virtual environment is deactivated. Exit shell and run again.")
    return 0

# create shell and activate venv
aalok-sathe commented 2 years ago

It's difficult to know if such a shell is active, and whether it is actually the poetry shell that is active or some other shell. Running exit can be destructive, especially when there is a process such as screen nesting your session, or when you are in ssh. Then exit will instead cause those contexts to disappear. There should just be a good way to handle this cleanly within poetry

SonGokussj4 commented 2 years ago

poetry shell starts a new shell and activates the virtual environment. Running deactivate will deactivate the virtual environment but not close the shell. To close the shell run exit.

This should be more documented! Finally I know what I need to do if I want the venv to activate. That is fine.

If I do poetry shell that informs me it's already activated, I press CTRL+D and then poetry shell Hallelluyah

neersighted commented 2 years ago

This is a tricky business as we are very limited in what we can do to affect a parent process. Poetry starts a subshell as a compromise -- maybe we could print a message about it being a subshell when we spawn it.

Another feature worth considering is a poetry shell --export that the user can source to set the variables in their current shell.

mtwichan commented 1 year ago

I loved Poetry but this is a serious issue that needs to be solved. It should be easier to deactivate the environment running in the subprocess with a simple command outside of the shell. Instead I need to find the Python path and deactivate it manually: https://stackoverflow.com/questions/60580332/poetry-virtual-environment-already-activated

Also, huge bummer that I'm unable to run multiple of the same environments at once: https://stackoverflow.com/questions/70739858/how-to-create-a-brand-new-virtual-environment-or-duplicate-an-existing-one-in-po

Will be moving away from Poetry for now.