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

Method to Exit Poetry Virtual Environment #2792

Closed
2 tasks done
adam-grant-hendry opened this issue Aug 10, 2020 · 14 comments
Closed
2 tasks done

Method to Exit Poetry Virtual Environment #2792

adam-grant-hendry opened this issue Aug 10, 2020 · 14 comments
Labels
area/docs Documentation issues/improvements

Comments

@adam-grant-hendry
Copy link

  • I have searched the issues of this repo and believe that this is not a duplicate.
  • I have searched the documentation and believe that my question is not covered.

Feature Request

Related to #1057. Please add poetry exit or equivalent to exit virtual environment.

@adam-grant-hendry adam-grant-hendry added kind/feature Feature requests/implementations status/triage This issue needs to be triaged labels Aug 10, 2020
@beeb
Copy link

beeb commented Aug 11, 2020

Just use exit to close the shell

@adam-grant-hendry
Copy link
Author

adam-grant-hendry commented Aug 11, 2020

@beeb Thank you for your suggestion, but I don't want to close the shell. I often have to switch back and forth between multiple activities and projects and I want to use one window. In fact, I don't need a new shell to be spawned every time I want to use the environment.

In short, I would like the feel of virtualenv and pipenv (I believe this would help increase adoption by others). When activating the virtual environment with poetry shell, I'd like like the environment name to show up to the left of my path in parentheses (see #2793). Then, I'd like to be able to type poetry exit, poetry deactivate, or something to that effect, to exit the environment and have the name to the left of the path disappear.

I realize there is a lot of development activity ongoing with poetry and of the difficulty of trying to accommodate multiple shells (bash, fish, Windows Command Prompt, Windows Power Shell, etc.). Is this something that can be done?

@beeb
Copy link

beeb commented Aug 11, 2020

I pretty much achieve exactly the behavior you are describing by using zsh. When I activate the environment with poetry shell I get the name of the environment in parentheses in the prompt, and when I do exit I go back the shell that was previously active (same window).

@adam-grant-hendry
Copy link
Author

@beeb I see, okay. Is there a reason why poetry insists on spawning a new shell? This may cause issues for developers who want to use environments like vscode, atom, pycharm, etc., that all have the console/shell built-in to the editor. I'm sure it will be an inconvenience to have to handle an additional pop-up window when the point of these environments is to have everything all in one place.

@beeb
Copy link

beeb commented Aug 12, 2020

Are you actually using poetry with your IDE? In VSCode it doesn't open a pop-up or anything.

(base) ➜  my_folder git:(develop) ✗ poetry shell
Spawning shell within /Users/beeb/my_folder/.venv
(base) ➜  my_folder git:(develop) ✗ . /Users/beeb/my_folder/.venv/bin/activate
(.venv) (base) ➜  my_folder git:(develop) ✗ which python
/Users/beeb/my_folder/.venv/bin/python

I'm afraid I cannot answer your question on the reason behind spawning a new shell since I'm not involved in the development. Just another user trying to help

@abn
Copy link
Member

abn commented Aug 12, 2020

@adam-grant-hendry If you are not already familiar with it, I would recommend reading up on how virtual environments work. You will find that they depend on some trickery using shell environment variables (PATH, PYTHONHOME etc.) to manipulate Python's site.

It is important to note that poetry is a python application and from within python code it is not possible to affect parent shell environment variables.

$ python -c "import os; os.environ['TEST']='1'; print(os.environ['TEST'])"
1
$ echo -n  $TEST
$ 

There are a few workaround that can approximate this (windows registry hacks etc.), but these typically creates more problems than they solve.

This means that we cannot activate a virtual environment in the parent shell using a python cli application. Hence why, we spawn a sub shell with the virtual environment activated. You will note that we use virtualenv under the hood to manage virtual environment creation etc.

As for your comment regarding issues with IDEs, I am not sure I follow. Typically with IDEs, you will configure the virtual environment path and not use poetry shell. Although there is nothing wrong with using it in their terminal emulators either. In my case, PyCharm auto-detects the virtual environment (created in-project under .venv directory) when I open a project. If it does not detect it, you can always configure the Project Interpreter.

Regarding "exiting" the shell, I am afraid I have to agree with @beeb that the typical case is to use exit (exit the sub-shell) or deactivate (available when a virtual environment is active). Bear in mind that the commands might differ based on your platform and your shell.

@adam-grant-hendry
Copy link
Author

adam-grant-hendry commented Aug 12, 2020

@beeb Yes, you are right. VSCode doesn't open a new CLI window when a new shell is spawned. I have been using it in VSCode, but I thought poetry wasn't working correctly in VSCode. I think my confusion was with what "spawn" meant. I was thinking it meant "create a new CLI window". Apologies and thank you for your clarification.

@abn Thank you very much for this insight into virtual environments (I will read up on this) and the inner workings of poetry. Perhaps I'm doing this wrong, but from #2793:

"Using VSCode, I can set

"python.venvPath": "C:\\Users\\example\\AppData\\Local\\pypoetry\\Cache\\virtualenvs"

in user settings. Now, upon opening VSCode, the activate.batch script is run and the virtual environment appears in parentheses next to the path and I can use the commands activate and deactivate to display and remove, resp., the virtual environment next to the path.

However, after running deactivate, entering poetry env list in my project directory shows:

(project-####-py#.#) (Activated)

"

How do I deactivate the virtual environment within poetry? Do I need to enter poetry exit and then deactivate?

@abn
Copy link
Member

abn commented Aug 13, 2020

@adam-grant-hendry I think you are conflating a few different mechanisms here.

With respect to poetry commands, most are run within the project's virtual environment. So even if you do not have poetry shell executed, it will still detect the default environment and activate it. For example when you run commands like poetry run etc.

The specific message you are referring to could be improved, I suppose, as it is merely indicating the current/default environment for the project not necessarily detecting if it is active currently in the parent shell.

def handle(self):
from poetry.utils.env import EnvManager
manager = EnvManager(self.poetry)
current_env = manager.get()
for venv in manager.list():
name = venv.path.name
if self.option("full-path"):
name = str(venv.path)
if venv == current_env:
self.line("<info>{} (Activated)</info>".format(name))
continue
self.line(name)

@finswimmer finswimmer added the area/docs/faq Frequently duplicated/potential addition to FAQ label Aug 14, 2020
@adam-grant-hendry
Copy link
Author

@abn Ah! Yes I see, this message was absolutely confusing me. So if I deactivate or delete the existing virtual environment and create a new one with virtualenv, do I then need to manually point poetry to the new virtual environment with poetry config virtualenvs.path path/to/new/virtualenv? I need to manually maintain both virtualenv and poetry, yes?

@abn
Copy link
Member

abn commented Aug 16, 2020

You can do that or activate the environment prior to running poetry commands. If a virtual environment is already active when you execute poetry, then Poetry will use that.

What works of my personal use is the in-project configuration. I have set poetry config virtualenvs.in-project true. This means Poetry will use /path/to/project/.venv as the virtual environment and I configure my IDE to use that as the virtual environment for the project. It does not matter how you create that virtual environment (python -m venv/virtualenv/poetry), if that exists poetry willl use it.

@abn abn removed kind/feature Feature requests/implementations status/triage This issue needs to be triaged labels Aug 16, 2020
@adam-grant-hendry
Copy link
Author

@abn and @finswimmer +1 for as adding this to candidate/faq. Thank you for the help!

@adam-grant-hendry
Copy link
Author

adam-grant-hendry commented Aug 17, 2020

I get error when creating my own virtual environment with venv. See #2823.

@abn
Copy link
Member

abn commented Aug 17, 2020

#2417 resolves gap in documentation.

@abn abn closed this as completed Aug 17, 2020
@abn abn added area/docs Documentation issues/improvements and removed area/docs/faq Frequently duplicated/potential addition to FAQ labels Aug 17, 2020
Copy link

github-actions bot commented Mar 3, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/docs Documentation issues/improvements
Projects
None yet
Development

No branches or pull requests

4 participants