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 Home
Version: 1909
OS build: 18363.1256
System type: 64-bits operating, x64-based processor
xxxxxxxxxx
Version: 1.52.1 (user setup)
Electron: 9.3.5
Chrome: 83.0.4103.122
Node.js: 12.14.1
V8: 8.3.110.13-electron.0
OS: Windows_NT x64 10.0.18363
xxxxxxxxxx
Version: 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
xxxxxxxxxx
C:\python_projects
Hence, for the relative path, type:
xxxxxxxxxx
cd /python_projects
Create a folder for the project called project_name
and check if it is present in the working folder
xxxxxxxxxx
mkdir project_name && dir
cd
into this folder
xxxxxxxxxx
cd 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
xxxxxxxxxx
echo >> 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
xxxxxxxxxx
python -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:
xxxxxxxxxx
python -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.
xxxxxxxxxx
Python 3.9.1 64-bit ('venv')
venv
If 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, figures
echo >> __init__.py
echo >> main.py
echo >> config.py
echo >> setup.py
echo >> 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.
xxxxxxxxxx
echo >> README.md
echo >> LICENSE
echo >> .gitignore
echo >> .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
xxxxxxxxxx
python -m pip install --upgrade pip
Then, create the dependencies file named requirements.txt
xxxxxxxxxx
pip list > requirements.txt
xxxxxxxxxx
pip install package_name
If installing more than one package, type:
xxxxxxxxxx
pip install package_name_1 package_name_2
If installing a specific version of a package, type:
xxxxxxxxxx
pip install package_name=1.6
It is better to use pip freeze
instead of pip list
as it allows to pin the dependencies version.
xxxxxxxxxx
pip freeze > requirements.txt
The requirements.txt
file can be used within a new environment to install dependencies cleanly with the following command:
xxxxxxxxxx
python -m pip install -r requirements.txt
IMPORTANT: this only works if requirements.txt
was produce with pip freeze
and NOT pip list
.
xxxxxxxxxx
pip check
xxxxxxxxxx
deactivate
The following linters can be "pip-installed" and ran from the terminal:
xxxxxxxxxx
pydocstyle main.py
pycodestyle main.py
autoflake main.py
flake8 main.py
Similarly, the formatter called black
can be run:
xxxxxxxxxx
black main.py
NOTE: do not forget to pip freeze
the linter/formatter libraries to requirements.txt
.