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

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

Open
3 tasks done
aalok-sathe opened this issue Mar 16, 2022 · 5 comments
Open
3 tasks done

Comments

@aalok-sathe
Copy link

aalok-sathe commented Mar 16, 2022

  • 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: Ubuntu 20.04

  • Poetry version: 1.1.13

Issue

Often times poetry keeps thinking I have a shell already activated, and I don't. This is widely reported by others too (#4622, #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
Copy link

bmarroquin commented Mar 17, 2022

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
Copy link
Author

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
Copy link

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
Copy link
Member

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
Copy link

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.

@Secrus Secrus transferred this issue from python-poetry/poetry Oct 14, 2024
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

5 participants