36

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?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
blueFast
  • 41,341
  • 63
  • 198
  • 344
  • Define "temporarily". You want to disable pyenv for a bash session ? – Marcs Sep 06 '16 at 12:18
  • 1
    @Marcs: Yes, indeed, because it conflicts with a good old virtualenv – blueFast Sep 06 '16 at 12:21
  • 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. – Marcs Sep 06 '16 at 12:25
  • @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. – blueFast Sep 06 '16 at 12:29
  • 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. – Marcs Sep 06 '16 at 13:01
  • 2
    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. – wjv Nov 28 '17 at 09:21
  • @wjv can you share your shell function? I'm assuming you don't mean a bash script? – mcExchange Mar 16 '21 at 09:41
  • 1
    @mcExchange 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. – wjv Mar 17 '21 at 10:14
  • 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. – schalkneethling Jul 25 '23 at 17:24

11 Answers11

69

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

pylanglois
  • 807
  • 6
  • 3
  • 14
    You probably want `pyenv shell system` otherwise you'll affect only shells in that current directory (or below). – davidA Jun 26 '19 at 00:11
  • 4
    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. – OsChannelDotCom Jul 06 '21 at 06:11
30

To deactivate from current shell environment, try:

pyenv shell --unset
vvvvv
  • 25,404
  • 19
  • 49
  • 81
aakinlalu
  • 1,051
  • 9
  • 4
8

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.

Noah
  • 21,451
  • 8
  • 63
  • 71
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.

Rotonen
  • 156
  • 2
  • 4
3

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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
shivams
  • 923
  • 12
  • 27
2

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

$ echo "" > /home/myusername/.pyenv/version
  • 2
    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-variables – MortenB Jul 15 '19 at 06:46
2

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

bash
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!

Dmitry
  • 31
  • 3
  • Hi Dmitry, did you accidentally paste this answer to the wrong site? Someone (probably you) also pasted it [here](https://github.com/facebook/prophet/issues/2002#issuecomment-1058543235) where it is much more on-topic. It'll probably be deleted here. Cheers! --Sietse – Esteis Mar 08 '22 at 13:32
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:)
-4

Try pyenv deactivate, to manually deactivate the virtual env.

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

Cal Eliacheff
  • 236
  • 1
  • 12
  • 2
    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) – blueFast Sep 06 '16 at 12:23
  • Using another custom .bashrc could be a solution then. – Cal Eliacheff Sep 06 '16 at 12:24
  • 6
    sure, and moving to another computer :) But there must be a way to disable pyenv in the current shell session? – blueFast Sep 06 '16 at 12:26
  • Just `cp` your `.bachrc` to i.e. `.bashrc_clean` then `source .bashrc_clean`, on the session you want is enough :) – Cal Eliacheff Sep 06 '16 at 12:30
  • 5
    Nonono, the current shell has already pyenv enabled, commenting it out and resourcing will not disable it. – blueFast Sep 06 '16 at 12:31