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
Settings at the time of writing this gist (12th of January 2021).
Edition: Windows 10 HomeVersion: 1909OS build: 18363.1256System type: 64-bits operating, x64-based processor
xxxxxxxxxxVersion: 1.52.1 (user setup)Electron: 9.3.5Chrome: 83.0.4103.122Node.js: 12.14.1V8: 8.3.110.13-electron.0OS: Windows_NT x64 10.0.18363
xxxxxxxxxxVersion: Python 3.9.1
NOTE: this gist does not explain how to install Python.
There are 2 options:
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
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
xxxxxxxxxxC:\python_projects
Hence, for the relative path, type:
xxxxxxxxxxcd /python_projects
Create a folder for the project called project_name and check if it is present in the working folder
xxxxxxxxxxmkdir project_name && dir
cd into this folder
xxxxxxxxxxcd project_name
Open the project folder project_name from the menu in VS Code
Create a .py python file into project_name and cd into it
xxxxxxxxxxecho >> python_script.py
Alternatively, create the .py file from VS Code menu.
Open python_script.py file
NOTE: it is important to create AND open a .py file BEFORE creating and activating the venv, as it helps with its activation
Create a virtual environment called venv_name
The command below will create a new folder called venv_name
xxxxxxxxxxpython -m venv venv_name
NOTE: the virtual environment name can be anything (like banana).
However, it is common practice to use just venv. This is handy when copying/pasting code snippets.
Hence, use the following command:
xxxxxxxxxxpython -m venv venv
NOTE: a message will appear (bottom right) asking to use the new environment, click 'Yes'.
Alternatively, choose from the list of environments (bottom left). The Python version should be listed with venv in parentheses, e.g.
xxxxxxxxxxPython 3.9.1 64-bit ('venv')
venvIf using the default command prompt (cmd), type:
xxxxxxxxxx.\venv\Scripts\activate
If using Windows PowerShell (PS), type:
xxxxxxxxxx.\venv\Scripts\Activate.ps1
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.
These will be needed for the project, e.g.:
xmkdir data, docs, tests, sources, scripts, figuresecho >> __init__.pyecho >> main.pyecho >> config.pyecho >> setup.pyecho >> requirements.txt
OPTION: if the project is to be hosted on GitHub, the following files can be created, or done automatically when creating a repository.
xxxxxxxxxxecho >> README.mdecho >> LICENSEecho >> .gitignoreecho >> .gitkeep
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.
First, update the pip library
xxxxxxxxxxpython -m pip install --upgrade pip
Then, create the dependencies file named requirements.txt
xxxxxxxxxxpip list > requirements.txt
xxxxxxxxxxpip install package_name
If installing more than one package, type:
xxxxxxxxxxpip install package_name_1 package_name_2
If installing a specific version of a package, type:
xxxxxxxxxxpip install package_name=1.6
It is better to use pip freeze instead of pip list as it allows to pin the dependencies version.
xxxxxxxxxxpip freeze > requirements.txt
The requirements.txt file can be used within a new environment to install dependencies cleanly with the following command:
xxxxxxxxxxpython -m pip install -r requirements.txt
IMPORTANT: this only works if requirements.txt was produce with pip freeze and NOT pip list.
xxxxxxxxxxpip check
xxxxxxxxxxdeactivate
The following linters can be "pip-installed" and ran from the terminal:
xxxxxxxxxxpydocstyle main.pypycodestyle main.pyautoflake main.pyflake8 main.py
Similarly, the formatter called black can be run:
xxxxxxxxxxblack main.py
NOTE: do not forget to pip freeze the linter/formatter libraries to requirements.txt.