Skip to content

Instantly share code, notes, and snippets.

@harmancode
Last active June 1, 2022 22:21
Show Gist options
  • Save harmancode/d0de94cfede08b2d92c4a6ee107dd5c9 to your computer and use it in GitHub Desktop.
Save harmancode/d0de94cfede08b2d92c4a6ee107dd5c9 to your computer and use it in GitHub Desktop.
How to install and use pyenv and pyenv virtualenv
How to install and use pyenv and pyenv virtualenv
by @harmancode | https://github.com/harmancode | https://harman.page
This work is licensed under a Creative Commons Attribution 4.0 International License.
pyenv
https://github.com/pyenv/pyenv
1. Install:
$ brew update
$ brew install pyenv
2. Shell configuration for Zsh:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
If you wish to get Pyenv in noninteractive login shells as well, also add the commands to ~/.zprofile or ~/.zlogin.
3. Restart shell
pyenv-virtualenv
https://github.com/pyenv/pyenv-virtualenv
Install
$ brew install pyenv-virtualenv
Shell configuration for Zsh
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc
Restart shell
Bash:
$ exec bash -l
For zsh,
$ exec zsh -l
Install python version to pyenv
Check installed versions
$ pyenv versions
If you wanted to see all the available Python 3.6 through 3.8, you can do this:
$ pyenv install --list | grep " 3\.[678]"
All versions:
$ pyenv install --list
Install specific version:
$ pyenv install -v 3.7.2
Create and activate virtual environment
https://realpython.com/intro-to-pyenv/#virtual-environments-and-pyenv
General command to create virtual environment
$ pyenv virtualenv <python_version> <environment_name>
With specific version
$ pyenv virtualenv 3.6.8 myproject
Create virtualenv from current version
$ pyenv virtualenv myproject
Generates .python-version file in the directory
$ pyenv local myproject
To be sure:
$ pyenv which python
$ pyenv which pip
Activate/deactivate manually
$ pyenv activate <environment_name>
$ pyenv deactivate
Activate/deactivate automatically
1. Create .python-version file in the project dir with: pyenv local myproject
2. Make sure that you made Shell configuration for Zsh as explained above.
3. Deactivate the environment if activated
4. Leave the directory if entered
5. Enter the directory
Remove a virtualenv
$ pyenv uninstall <environment_name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment