Skip to content

Instantly share code, notes, and snippets.

@dansayo
Last active October 4, 2022 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dansayo/2f2bd35f6c6d322fdd1d6426be32a514 to your computer and use it in GitHub Desktop.
Save dansayo/2f2bd35f6c6d322fdd1d6426be32a514 to your computer and use it in GitHub Desktop.
Setting up a new Python project folder, version, and virtual environment

Update:

overriding pyenv python versions, if you want to use a python version located on a different folder, i.e. /usr/local/bin/python3

on a new project, then:

mkdir <new_project>
cd <new_project>
<on new_project> mkdir venv
<on new_project> /usr/local/bin/python3 -m venv venv

Setting up a new Python project

This how-to uses the native venv. Goodbye to pipenv. pyenv still used. Tested as my current workflow on Mac OS. Linux should be happy, too.

A. Detailed steps

  1. Choose your Python version
  • From available local versions -
~/dev $ pyenv versions
  • Or from the Internet, if no local version exists -
~/dev $ pyenv install -l
~/dev $ pyenv install 3.7.4
  1. Set the version as system default
~/dev $ pyenv global 3.7.4
  1. Set up the project folder
~/dev $ mkdir project-x
~/dev $ cd project-x
~/dev/project-x $ mkdir venv
  1. Create the virtual environment
~/dev/project-x $ $(pyenv which python3) -m venv venv
  1. Revert the system default Python version, if needed
~/dev/project-x $ pyenv global 3.6.8
  1. Activate the virtual environment
~/dev/project-x $ source venv/bin/activate
  1. Upgrade pip, if needed
(venv) ~/dev/project-x $ pip list
(venv) ~/dev/project-x $ pip install --upgrade pip
  1. Work on the other Python packages, start-up VS Code, etc.

  2. On VS Code, create a helloworld Python file to trigger VS Code asking for a Python interpreter; choose the appropriate venv and Python version. I also install pylint and black (via call to Format Document). Sample pip list output of the base install -

(venv) ~/dev/project-x $ pip list
Package           Version
----------------- -------
appdirs           1.4.3
astroid           2.2.5
attrs             19.1.0
black             19.3b0
Click             7.0
isort             4.3.21
lazy-object-proxy 1.4.2
mccabe            0.6.1
pip               19.2.3
pylint            2.3.1
setuptools        40.6.2
six               1.12.0
toml              0.10.0
typed-ast         1.4.0
wrapt             1.11.2
(venv) ~/dev/project-x $

How to use linting: Using pylint in VS-Code

  1. Before closing a working day's session, deactivate the virtual
(venv) ~/dev/project-x $ deactivate
~/dev/project-x $

B. To recap

  • to activate
~/dev/project-x $ source venv/bin/activate
  • to deactivate
(venv) ~/dev/project-x $ deactivate

C. Transitioning from a pyenv virtualenv to the native venv

Be sure to pip freeze > requirements first.

pyenv uninstall <name of pyenv virtual env>

Proceed to create the venv virtualenv.

Restore Python package dependencies

pip install -r requirements.txt

D. Transitioning from a pipenv virtualenv to the native venv

Create a requirements from the pipenv lock output (copy-paste the output to requirements.txt) and remove the vm

pipenv lock -r
pipenv uninstall
pipenv --rm

Proceed to create the venv virtualenv.

Restore Python package dependencies

pip install -r requirements.txt

E. Updating pyenv

In Homebrew:

$ brew update && brew upgrade pyenv

Update pyenv:

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