Deactivate pyenv in current shell
Asked Answered
K

12

45

My .bashrc has this:

enable-pyenv () {
    # Load pyenv automatically by adding
    # the following to your profile:

    export PATH="$HOME/.pyenv/bin:$PATH"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
}

enable-pyenv

Which enables pyenv. In some situations, I want to (temporarily) disable pyenv. How can I do this?

Kalila answered 6/9, 2016 at 12:14 Comment(9)
Define "temporarily". You want to disable pyenv for a bash session ?Blandina
@Marcs: Yes, indeed, because it conflicts with a good old virtualenvKalila
I would simply comment out like this #enable-pyenv and open a new bash session. If it is acceptable to open a new bash session for you, just remember that .bashrc is read only when a bash is initialized.Blandina
@Marcs: not even sure that would apply in my case since I open shells inside a tmux session which is run in a bash session where pyenv was already enabled, so very probably any shell within tmux has pyenv enabled, even if I edit the .bashrc. I can open a brand new terminal, but since this is gonna bite me more than once, I would like to have a way of cleanly disabling (and re-enabling) pyenv in my current shell.Kalila
I checked out a little and to do what you want directly inside a shell you should reverse the pyenv init - shell function, which changes a bunch of stuff and looks like is piping your commands through pydev. Try to launch pyenv init - in a shell and you'll see the shell code relative to your bash. So i would create a script with the reverse function to call it when you want to turnoff pydev. I'm speaking theoretically, I don't know how pydev works.Blandina
I’ve taken something of an opposite approach: I wrote a shell function pyinit() which initialises pyenv, prepends its shim directory to the $PATH and does some further initialisation. In other words, pyenv is not active until I want it to be. If I have already activated pyenv in my current shell and I need to get rid of it, I just open a new shell — cheap and easy on modern hardware.Deflower
@Deflower can you share your shell function? I'm assuming you don't mean a bash script?Kokoruda
@Kokoruda My function, when boiled down to the bare basics, wasn't that different from the one posted in the question — I just didn't then also call it after defining it in my shell startup. The point I was making in my comment was that instead of always activating pyenv and having a way to disable it, one could rather enable it when needed and discard the shell afterwards. Personally I don't use pyenv much anymore, but I use a streamlined version of the same idea for other tools.Deflower
You may want: pyenv shell --unset - From pyevn shell --help: > When - is passed instead of the version string, the previously set version will be restored. With --unset, the PYENV_VERSION environment variable gets unset, restoring the environment to the state before the first pyenv shell call.Libby
G
78

If you want to use the python version from your system temporarily (until you close your current shell):

pyenv shell system

https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-shell

If you want by default your system's python

pyenv global system

https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-global

Suppose your shell is currently in the 'src' folder and you want src and all its subfolders to use by default a specific version of python installed with pyenv, named for instance dev311:

pyenv local dev311

https://github.com/pyenv/pyenv/blob/master/COMMANDS.md#pyenv-local

Edited to take into account the comments

Giorgia answered 9/1, 2019 at 16:22 Comment(2)
You probably want pyenv shell system otherwise you'll affect only shells in that current directory (or below).Colored
continuation on above comment from @davidA, be careful moving to other projects after using the pyenv shell system. It will take precedence over the .python-version file created by pyenv local command. Don't forget to unset shell using pyenv shell --unset or close the shell when working on other projects.Pargeting
A
34

To deactivate from current shell environment, try:

pyenv shell --unset
Annmarie answered 7/4, 2021 at 11:22 Comment(3)
This has no effect at all.Florineflorio
@Florineflorio shell versions > local versions > global versions. Check pyenv versions to see which Python version is currently active.Concinnate
This is to back out from a shell usage (temp use) pyenv shell 3.11.5 change when your pyenv local is 3.10.4Hesperian
G
9

I'm not sure that this will get rid of all traces of pyenv, but editing your $PATH environment variable to get rid of the pyenv- or shim-containing paths seems to deactivate pyenv. Eg,

export PATH=`echo $PATH | python -c "import sys, re; print(':'.join(x for x in sys.stdin.read().strip().split(':') if not 'pyenv' in x))"`

If you want to be able to re-enable it, just store your previous $PATH so you can restore it later.

Guanabana answered 2/6, 2017 at 23:25 Comment(0)
D
7

Try playing around with some variants of:

env -i bash

env -i bash -l

env -i bash --norc

env -i bash --norc --noprofile

This does not come without side effects as env -i nukes your whole session and thus afterwards a lot of convenience like $HOME is gone with the bathwater, but so is pyenv.

Decalcomania answered 11/2, 2017 at 12:24 Comment(0)
I
5

For me, what worked ultimately was the brute force method of removing all pyenv paths from the $PATH variable:

PATH=`echo $PATH | tr ':' '\n' | sed '/pyenv/d' | tr '\n' ':' | sed -r 's/:$/\n/'`

I wish pyenv offered a better way by itself.

Insulin answered 17/7, 2020 at 8:52 Comment(0)
B
3

I use this but not sure if it is a good way

bash
Barnabe answered 21/11, 2021 at 21:19 Comment(0)
P
2

None of the posted answers worked for me but the following did:

$ echo "" > /home/myusername/.pyenv/version
Polygon answered 8/5, 2017 at 17:1 Comment(1)
This does not work, it only removes the prompt, it still points to the same shim, you also need to remove it from path and unset all the PYENV_* env-variablesGman
L
1

I have macOS Monterey, v12.0.1. Prophet was successfully installed using python 3.8. It did NOT work with 3.9 versions. I use pyenv to create virtual env. That is what I did:

pip3 install virtualenv

pip3 install virtualenvwrapper

brew install pyenv-virtualenv

You need these commands to have virtual env running under pyenv. Next, install python

pyenv install 3.8.10

Create env called 'prophet':

pyenv virtualenv 3.8.10 prophet

Activate it in your working directory:

pyenv local prophet

Install 2 packages:

pip install pystan==2.19.1.1

pip install prophet

It worked fine for me!

Linctus answered 3/3, 2022 at 22:15 Comment(1)
Hi Dmitry, did you accidentally paste this answer to the wrong site? Someone (probably you) also pasted it here where it is much more on-topic. It'll probably be deleted here. Cheers! --SietseLitharge
F
0

that worked for me, to deactivate pyenv:

#show shell variables (set) and environment/exported variables (env)
{ set; env; } | egrep -i pyenv
#unset variables and functions:
unset PYENV_ROOT PYENV_SHELL _pyenv pyenv
#remove from PATH:
PATH=$(echo $PATH | tr ':' '\n' | egrep -v pyenv | paste -sd:)
Felten answered 21/1, 2023 at 13:57 Comment(0)
C
0

In my case getting back to using my conda environment instead of pyenv temporarily, the simplest method was to re-set the PATH veriable in the current shell:

  1. Check current PATH
echo $PATH
/Users/fsk/.pyenv/shims:/Users/fsk/miniconda3/envs/polar/bin:/usr/local/mysql/bin:/opt/local/bin:...
  1. Remove pyenv part
export PATH="/Users/fsk/miniconda3/envs/polar/bin:/usr/local/mysql/bin:/opt/local/bin"
Christner answered 25/10, 2023 at 8:30 Comment(0)
I
-3

Try pyenv deactivate, to manually deactivate the virtual env.

Doc here: https://github.com/yyuu/pyenv-virtualenv

Illimani answered 6/9, 2016 at 12:20 Comment(6)
Sorry, I was not clear enough: I want to disable pyenv completely (for a bash session), because it conflicts with a normal virtualenv (outside the control of pyenv). I do not want to disable a specific virtualenv environment controlled by pyenv, but completely disable pyenv (for a bash session)Kalila
Using another custom .bashrc could be a solution then.Illimani
sure, and moving to another computer :) But there must be a way to disable pyenv in the current shell session?Kalila
Just cp your .bachrc to i.e. .bashrc_clean then source .bashrc_clean, on the session you want is enough :)Illimani
Nonono, the current shell has already pyenv enabled, commenting it out and resourcing will not disable it.Kalila
This is how to deactivate pyenv in the current shell which is the title of the question and what I was googling for o.0Autarky

© 2022 - 2024 — McMap. All rights reserved.