Skip to content

pyenv + virtualenv not working - use pyenv activate for all  #4013

Closed

Description

@fenchu

Environment data

  • VS Code version: 1.30.2
  • Extension version (available under the Extensions sidebar): vscode-python 2018.12.1
  • OS and version: ubuntu 16.04LTS
  • Python version (& distribution if applicable, e.g. Anaconda): any pypy,cpython
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): pyenv
  • Relevant/affected Python packages and their versions: None

Expected behaviour

When choosing python interpreter it lists all versions under ~/.pyenv correctly (see png below), but
the ones created with pyenv+virtualenv like pyenv virtualenv 3.7.2 mycoolapp which creates a venv environment under pyenv using python 3.7.2 with the name mycoolapp.
This should be activated with:
pyenv activate mycoolapp

The ones listed with venv VSC does:
$ source /home/mortenb/.pyenv/versions/mycoolapp/bin/activate

The ones listed with pyenv (the ones created with pyenv install 3.7.2, that downloads, compile and install the specified python version):
$pyenv shell 3.7.2
this works for base versions not for pyenv+virtualenv, but I recommend using pyenv activate for all

$ pyenv help activate
Usage: pyenv activate <virtualenv>
       pyenv activate --unset

Activate a Python virtualenv environment in current shell.
This acts almost as same as `pyenv shell`, but this invokes the `activate`
script in your shell.

<virtualenv> should be a string matching a Python version known to pyenv.

Example loading flask on python 3.6.2:

venv-vs-pyenv

VSC does when opening a terminal:

$ source /home/mortenb/.pyenv/versions/flask/bin/activate
$ which python
/home/mortenb/.pyenv/shims/python
$ python --version
Python 3.7.0

It is wrong version, if it had done this it would have been correct:

$ pyenv activate flask
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(flask) mortenb@mortenb-home1:/dist/sensio/unity-selenium$ which python
/home/mortenb/.pyenv/shims/python
(flask) mortenb@mortenb-home1:/dist/sensio/unity-selenium$ python --version
Python 3.6.2

Pyenv is a terrific technology, we manage full python versioning control within docker containers by a single build parameter. This container tagged with version number we use as base container for all python projects within the company, it makes regression testing and upgrading a oneliner in jenkins:

https://gist.github.com/fenchu/14b8fe5a0c24942f845a35106c7b176a

So please continue supporting it :-)

Thanks

Activity

  1. ghost added
    triage-needed
    on Jan 16, 2019
  2. ghost removed
    triage-needed
    on Jan 16, 2019
  3. znd4 commented on Apr 29, 2019

    @znd4

    Would this fix be implemented in vscode-python, or would it happen in the python language server?

    I'd love to try to figure out how to get this to work, if no-one's already working on it. (pyenv is pretty awesome, and it would be great if I could use it with vscode)

  4. clbarnes commented on May 22, 2019

    @clbarnes

    It's surprising that the python extension explicitly looks for python versions installed by pyenv, but then can't activate them.

  5. michaelwclark commented on Oct 3, 2019

    @michaelwclark

    Adding "python.terminal.activateEnvironment": false to my settings resolved this issue for me.

  6. added
    area-environments
    on Oct 10, 2019
  7. 31 remaining items

  8. andreamah commented on Sep 27, 2023

    @andreamah

    Kartik Raj (@karrtikr) Can you provide minimal steps to verify this?

  9. added
    verification-steps-needed
    on Sep 27, 2023
  10. karrtikr commented on Sep 27, 2023

    @karrtikr

    Verification steps:

    {
        "python.experiments.optInto": ["pythonTerminalEnvVarActivation"]
    }
    
    • Create pyenv environments:
    pyenv virtualenv hello
    
    • Select respective pyenv environments using Python: Select Interpreter, give it a couple seconds to activate:

    image

    • Create new terminal and run:
    python -c"import sys;print(sys.executable)"
    

    Make sure it points to the path of the selected interpreter.

  11. removed
    verification-steps-needed
    on Sep 27, 2023
  12. andreamah commented on Sep 27, 2023

    @andreamah
    image

    I don't think it enables the env correctly? This is when I create a new terminal and run the command

  13. removed
    verified
    on Sep 27, 2023
  14. karrtikr commented on Sep 27, 2023

    @karrtikr

    I don't think you're opted into experiement, can you make sure? Note it requires a reload.

  15. andreamah commented on Sep 27, 2023

    @andreamah

    Seems that, when I added the setting to user settings, it didn't register. Moved it to the remote settings and it worked 👍

  16. jasonbrancazio commented on Oct 2, 2023

    @jasonbrancazio

    I like the new auto-activation, but now as you've noted the deactivate command is broken. It took me a while to figure out why:
    #22037

    Adding a huge chunk of bash as suggested in issue 22037 to my ~/.bashrc is an inconvenient way to work around this.

  17. karrtikr commented on Oct 2, 2023

    @karrtikr

    While we understand that the current workaround involving ~/.bashrc can be inconvenient, but unfortunately we're limited by VS Code.

    FWIWI we're actively working on a notification system to help users discover this solution more easily #22121, which can mitigate at least some part of the issue.

  18. karrtikr commented on Oct 3, 2023

    @karrtikr

    Jason Brancazio (@jasonbrancazio) Would it work if we were to provide a Python command to open a new terminal which is deactivated? We're trying to understand why "deactivate" command is used, and if there are any alternatives we could provide.

  19. jasonbrancazio commented on Oct 4, 2023

    @jasonbrancazio

    The preference setting to disable auto-activation was sufficient for me.

    I activate and deactivate virtual environments all the time. I often deactivate them if I want to delete them from disk and recreate them. I also might create two virtual environments, each with different versions of libraries installed, switch between them by activating and deactivating, and test code in each environment.

  20. locked as resolved and limited conversation to collaborators on Nov 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-environmentsarea-terminalfeature-requestverification-neededverified

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions