Skip to content

Instantly share code, notes, and snippets.

@oschannel
Last active April 17, 2022 09:16
Show Gist options
  • Save oschannel/7f61b590fb866c5989bbbdedb5bf9369 to your computer and use it in GitHub Desktop.
Save oschannel/7f61b590fb866c5989bbbdedb5bf9369 to your computer and use it in GitHub Desktop.
PYENV BASICS CHEATSHEET | by OsChannel.com

Watch a video tutorial about below on OsChannel.com: https://youtu.be/WVISFQpstJE

PyEnv Basics Cheatsheet Contains:

  • Installation of PyEnv.
  • Installation of Multiple Python versions including a macOS Fix.
  • Setting Different Python versions for your Projects using multiple ways.
  • Swiching between Python Versions using PyEnv.

To Install pyenv:

curl https://pyenv.run | bash

Add following lines to the end of your .bashrc/.zshrc depending on your ${SHELL}:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PYENV_ROOT/shims:$PATH"
eval "$(pyenv init -)"
# Once added restart your shell

To Check all available Python versions for installation:

pyenv install --list

To Install a specific version of Python using PyEnv (eg: 3.9.1):

pyenv install 3.9.1

(This is for macOS users only.) I faced an issue while installing older versions of python on my macOS Big Sur using PyEnv and had to use this patch. Simply try replacing the python version after the --patch flag with your desired python version if you see below error while installing python. ERROR TEXT

pyenv install --patch 3.7.2 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch)

To Uninstall a specific version of Python using PyEnv(eg: 3.9.1):

pyenv uninstall 3.9.1

To List all installed Python versions using PyEnv:

pyenv versions

To Get currently set Python version using PyEnv:

pyenv version

To Set a Python version globally for all projects for current user using PyEnv:

pyenv global 3.7.1

To Set a Python version for specific project using PyEnv:

python version set using pyenv local command overrides python version set using pyenv global command

pyenv local 3.8.1

To Set a Python version at shell level using PyEnv:

python version set using pyenv shell command overrides python version set using pyenv global or pyenv local command

pyenv shell 3.9.1

To unset a Python version set using PyEnv:

pyenv shell  --unset       
pyenv local  --unset
pyenv global --unset

Precedence for Python version set using global, local and shell methods:

Precedence chart

To Uninstall PyEnv along with all python versions installed by PyEnv:

rm -rvf ~/.pyenv
# and don't forget to remove the pyenv paths and init lines from your .bashrc/.zshrc.

PyEnv has a lot more than what's stated here. We are just getting started. For More PyEnv commands try pyenv commands and for help related to a specific command (eg: pyenv versions) try pyenv versions --help.

I thank all the contributors of PyEnv!! For more help on PyEnv refer specific projects on below link: https://github.com/pyenv

@oschannel
Copy link
Author

oschannel commented Jul 11, 2021

I received a similar error while installing any python version lower than 3.8(including 3.8)

Screenshot 2021-07-10 at 4 17 13 AM

Error text:

./Modules/posixmodule.c:8431:15: error: implicit declaration of function 'sendfile' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
ret = sendfile(in, out, offset, &sbytes, &sf, flags);

1 error generated.
make: *** [Modules/posixmodule.o] Error 1
make: *** Waiting for unfinished jobs....
1 warning generated.

FIX:

Simply try replacing the python version after the --patch flag with your desired python version if you see above error while installing python.

pyenv install --patch 3.7.2 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch)

@oschannel
Copy link
Author

And this is precedence chart for python versions set using PyEnv:
pyenv version precedence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment