Skip to content

Instantly share code, notes, and snippets.

@nano-jag
Created September 9, 2022 19:19
Show Gist options
  • Select an option

  • Save nano-jag/b731ddbc3774ef6902ef85a3b193ad43 to your computer and use it in GitHub Desktop.

Select an option

Save nano-jag/b731ddbc3774ef6902ef85a3b193ad43 to your computer and use it in GitHub Desktop.

PYTHON VIRTUAL ENV

Here’s what you need to know:

  • pyenv manages multiple versions of Python itself.
  • virtualenv/venv manages virtual environments for a specific Python version.
  • pyenv-virtualenv manages virtual environments for across varying versions of Python.
brew upgrade pyenv
brew uninstall pyenv

# install pyenv + friends
curl https://pyenv.run | bash

restart your shell
exec $SHELL

# update pyenv
pyenv update

# uninstall
rm -fr ~/.pyenv

# remove these three lines from .bashrc
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

# restart shell
exec $SHELL


# exploring pyenv commands
pyenv commands

pyenv local <version>
pyenv global <version>

pyenv uninstall <version>

pyenv install --list | grep " 3\.[678]"
pyenv install --list | grep " 3\.7.*"

# install verbose -v
pyenv install -v 3.7.2

# install silent 
pyenv install 3.10.6

# installation location
ls ~/.pyenv/versions

# remove anyversion
rm -rf ~/.pyenv/versions/2.7.15

# alternatively
pyenv uninstall 2.7.15

# Using new python
## check what versions of python you have available
pyenv versions

# current active python version
pyenv version

# path
pyenv which python

# set global system-version-python
pyenv global 3.8-dev

# back to system version of python
pyenv global system

# full path to a system executable
pyenv which pip

# activate shell-specific Python version
## sets PYENV_VERSION environment variable 
pyenv shell 3.8-dev

# deactivate shell-specific Python version
pyenv shell --unset 3.8-dev

# 1. pyenv shell
# 2. pyenv local
# 3. pyenv global
# 4. System python

pyenv-virtualenv

  • installation
# installed in the above scripts
  • creating virtual environments
pyenv install 3.10.7

pyenv virtualenv <python_version_optional> <environment_name>

pyenv virtualenv 3.6.8 myproject

pyenv virtualenv 3.10.7 django-3107

# create virtualenv from current version
pyenv version # active version
pyenv virtualenv venv_new

# list existing virtualenvs
pyenv shell venv_new
pyenv virtualenvs

activating your version

pyenv virtualenv 3.6.8 myproject
pyenv virtualenv 3.10.7 django-3107

# activate
pyenv local myproject
# creates .python-version file 

# automatic environment activation
## eval "$(pyenv virtualenv-init -)"

# manual activation
pyenv activate <environment_name>
pyenv deactivate

# delete existing virtualenv
pyenv uninstall my-vritual-env

# delete existing virtualenvs
pyev virtualenv-delete my-virtual-env

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