Create a Virtual Environment with Python

The present gist is a hybrid between a 'go-to' cheat sheet and a tutorial when starting a new Data Science Project.

Its purpose is to create a virtual environment with Python using the 'venv' module.

 


Table of contents



System Settings

Settings at the time of writing this gist (12th of January 2021).

Microsoft Windows Operating System

Microsoft Visual Studio Code

Python

NOTE: this gist does not explain how to install Python.



Steps to creating a 'venv'

Create a folder for the project

There are 2 options:

  1. Create a repository on GitHub.com BEFORE creating the project folder on the local machine.

    Once the repository is created, clone it onto the local machine.

    NOTE: This procedure will not be covered here but it will be in a future Gist.

    OR

  2. Create the project folder locally (see below gist)

    • First, open a Terminal Prompt within VS Code.

    • Then, go to the folder where the new project is to be created, i.e. go to the 'working folder'.

      For example, if the main folder is called python_projects, go to

      Hence, for the relative path, type:

    • Create a folder for the project called project_name and check if it is present in the working folder

    • cd into this folder

    • Open the project folder project_name from the menu in VS Code


Create a virtual environment for the project


Activate the new virtual environment venv

If using the default command prompt (cmd), type:

If using Windows PowerShell (PS), type:

IMPORTANT: if no changes seem to occur, open a new terminal prompt to activate the environment.

The active path should now be preceded by (venv) or (banana) if a specific venv name was chosen.


Create 'default' folders and files

These will be needed for the project, e.g.:

OPTION: if the project is to be hosted on GitHub, the following files can be created, or done automatically when creating a repository.

NOTE 1: the 'LICENSE' and '.gitignore' files do NOT take a file extension. Only 'README.md' does.

NOTE 2: add a copy of .gitkeep into each folder in order to commit empty folders to GitHub.


Record the dependencies for the project


Install a Python Library

If installing more than one package, type:

If installing a specific version of a package, type:


Save the new dependencies

It is better to use pip freeze instead of pip list as it allows to pin the dependencies version.

Install dependencies from a file

The requirements.txt file can be used within a new environment to install dependencies cleanly with the following command:

IMPORTANT: this only works if requirements.txt was produce with pip freeze and NOT pip list.


Check dependency clashes after installing all packages


Deactivate the virtual environment


Bonus: Linting & Formatting

The following linters can be "pip-installed" and ran from the terminal:

Similarly, the formatter called black can be run:

NOTE: do not forget to pip freeze the linter/formatter libraries to requirements.txt.