How to switch Python Versions and VirtualEnvs gracefully?

Lane
1 min readFeb 16, 2020

Use Homebrew to install pyenv.

https://github.com/pyenv/pyenv#homebrew-on-macos

Then brew install pyenv-virtualenv. Then config fish by vim ~/.config/fish/config.fish.

set -x PYENV_ROOT $HOME/.pyenv
status --is-interactive; and pyenv init - | source
status --is-interactive; and pyenv virtualenv-init - | source

Here are some useful commands.

pyenv versions
pyenv install -l
pyenv install 3.8.1
pyenv global 3.8.1
pyenv which python

python --version should show you 3.8.1.

Now let’s create an backend api project.

mkdir mysite
cd mysite
pyenv virtualenv 3.8.1 mysite-3.8.1
touch .python-version
vim .python-version # add mysite-3.8.1 to it
pip install --upgrade pip
python -m pip install Django
pip install ipython
django-admin startproject mysite
mv mysite api
cd api
python manage.py startapp mysite_api

Then follow https://docs.djangoproject.com/en/3.0/intro/tutorial01/

psql --username=postgres
CREATE DATABASE mysite_dev WITH OWNER postgres ENCODING 'utf-8';
python manage.py migrate
python manage.py createsuperuser

--

--

Lane
0 Followers

Ruby, JavaScript and Python developer.