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 for Python with the package manager Conda.
Table of contents
- Create a Virtual Environment in Python with Conda
 
Settings at the time of writing this gist (20th of January 2021).
Edition: Windows 10 Home
Version: 1909
OS build: 18363.1256
System type: 64-bits operating, x64-based processor
Version: 1.52.1 (user setup)
Date: 2020-12-16T16:34:46.910Z
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
Conda version: 4.9.2
Python version: 3.8.5
NOTE: Python is automatically installed when installing Miniconda. This gist does not explain how to install Miniconda.
There are 2 options:
- 
Create a repository on GitHub.com BEFORE creating the new 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 new 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 toC:\python_projectsHence, for the relative path, type:
cd /python_projects - 
Create a folder for the project called
project_nameand check if it was created in the working foldermkdir project_name && dir - 
cdinto this foldercd project_name - 
Open the project folder
project_namefrom the menu in VS Code 
 - 
 
- 
Create a
.pypython file intoproject_nameandcdinto itecho >> python_script.pyAlternatively, create the
.pyfile from VS Code menu. - 
Open
python_script.pyfileNOTE: it is important to create AND open a
.pyfile BEFORE creating and activating thevenv, as it helps with venv activation - 
Choose the Python instance from the list of environments (bottom left). The Python version should be listed with
condain parentheses, e.g.Python 3.8.5 64-bit (conda)NOTE: the directory path to the project folder may be preceded by
PS, which means the default shell is 'PowerShell'. However, Conda does not work with PowerShell in VS Code. Hence, the default shell MUST be changed to 'Command Prompt' (cmd). - 
Then, open a new terminal It should now show
(base)BEFORE the directory path, e.g.(base) /Users/user_nameNOTE:
(base)represents an 'alias' and is inside parentheses This is important later on. 
These will be needed for the project, e.g.:
mkdir 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.
echo >> README.md
echo >> LICENSE
echo >> .gitignore
NOTE: the 'LICENSE' and '.gitignore' files do NOT take a file extension. Only 'README.md' does.
- 
Check Conda version and update
conda info conda --version - 
Update Conda from an environment named
environment_nameconda update --name environment_name condaIMPORTANT: like with Python base configuration, do NOT update the
baseenvironment !! This is what virtual environments are for. - 
Update Conda from inside the 'active' environment
conda update conda 
There are 2 ways:
- Manage environments from the default environment folder
 - Manage environments from a specific location
 
i.e. from /miniconda3/envs
- 
List all environments
conda env listOR
conda info --envsThe output will look like this:
base * C:\miniconda3NOTE: when running 'conda env list', the (current) active environment in the environment list will have an asterisk
*in front of the directory path (see example below). - 
Create a new Conda virtual environment named
environment_nameThe command below will create a new folder called
environment_name.conda create --name environment_nameThe output will look like this:
base * /miniconda3 environment_name /miniconda3/envs/environment_nameNOTE 1: by default, new Conda environments are created in the
envsfolder where Miniconda is installed, e.g./miniconda3/envs/environment_nameNOTE 2: there should now be
(environment_name)BEFORE the directory path. This is an 'alias' as mentioned earlier, and it is inside parentheses e.g.(environment_name) /Users/user_nameIMPORTANT: in fact, the newly created environment will not be very useful unless a Python version is installed (see Manage Python section below)
 - 
Clone the environment
environment_nameand create a new one calledclone_environmentconda create --name clone_environment --clone environment_nameThe output will look like this:
base * /miniconda3 environment_name /miniconda3/envs/environment_name clone_environment /miniconda3/envs/clone_environment - 
Activate the environment named
environment_nameconda activate environment_nameThe output will look like this:
base /miniconda3 environment_name * /miniconda3/envs/environment_name clone_environment /miniconda3/envs/clone_environment - 
Deactivate current environment
conda deactivateThis means that the deactivated environment will lose its
*when runningconda env list. Thus, the active environment will revert tobaseenvironment.The output will look like this:
base * /miniconda3 environment_name /miniconda3/envs/environment_name clone_environment /miniconda3/envs/clone_environment - 
Delete an environment (and ALL its packages) This can be done only if the environment is NOT active.
conda deactivate conda remove --name environment_name --allThe output will look like this:
base * /miniconda3 clone_environment /miniconda3/envs/clone_environment - 
Delete unused packages from writable package caches
conda clean --packages - 
After deleting an environment, clear up Conda's cache to delete left-overs
conda clean --all 
i.e. not in Miniconda's envs folder (see previous section)
- 
First,
cdinto the project working directory, e.g.(base) /Users/user_nameOR
cd /python_projectsThen, create a project folder named
project_nameandcdinto it, e.g.mkdir project_name && cd project_name - 
Create a virtual environment called
environment_name. However, the virtual environment name can be anything (likebanana). For the sake of simplicity and reproducibility, let's just call itconda_env. This is because it is common practice to use justvenvwhen a virtual environment is created with Python's 'venv' module. This is handy when copying/pasting code snippets.Then, use the RELATIVE path with the
--prefixflag to createconda_env. It will be INSIDE the newly created project folder namedproject_name.Thus, from the Terminal Prompt, run the following code (no need to
cdinto a specific folder):conda create --prefix ./conda_env conda activate ./conda_env - 
Check the environment list.
conda env listThe output will look like this:
base C:\miniconda3 * C:\python_projects\project_name_conda\conda_envIMPORTANT: before, the new
environment_namewas stored within thebaseconfiguration and it possessed an 'alias' in front of the directory path. This is not the case when using this way to creating Conda virtual environments. Thus, the ABSOLUTE path name is used as 'alias' and is inside parentheses BEFORE the ABSOLUTE path, e.g.(C:\python_projects\project_name\conda_env) C:\python_projects\project_nameNOTE: it is possible to give an alias to the absolute directory path by modifying the
.condarcfile (check futur Gist on 'environment variable' for more). - 
Delete this environment as shown earlier, i.e. run the following code:
conda deactivate conda remove --prefix ./conda_env --all conda clean --packages conda clean --all 
- 
List versions of Python available to install
conda search python - 
Check the Python version installed
python --version - 
Create new Conda environment named
conda_envwith Python installedconda create --prefix ./conda_env pythonNOTE: if not specified, the 'default' Python version installed. Here, it is version 3.8.5.
 - 
Create new Conda environment named
conda_envwith a specific version of Python (e.g. version 3.9) and a package namedpackage_nameconda create --prefix ./conda_env python=3.9 package_nameNOTE: see Manage Packages section below for more.
 - 
Update Python from inside the active environment to the latest version of a 3.x branch (e.g. version 3.4)
conda update python - 
Upgrade Python to another 3.x (e.g. version 3.6) by installing that version of Python
conda install python=3.6 
- 
Search for package in Anaconda's library/repository
conda search package_name - 
Install a package named
package_namein the current environmentconda install package_name - 
Install several packages
conda install package_name_1 package_name_2 - 
Install package from a specific source like a URL address
conda install --channel url_address package_nameIMPORTANT: it is advised to install all the packages at the same time to avoid dependency conflicts.
 - 
Search and install a package not available in Anaconda's library. Look into Conda-Forge repository
conda search --channel conda-forge package_name conda install --channel conda-forge package_name - 
If the package is not in Conda-Forge either, try
pip install(LAST RESORT!!)pip install package_name - 
Remove a package named
package_namefrom the current environmentconda remove package_name - 
List of all packages in the current environment
conda list - 
Save package list inside a
requirements.txtfileconda list > requirements.txtNOTE: this is equivalent to the
pip listcommand when using Python 'venv' module. Thus, it not appropriate when creating a new environment using therequirements.txtfile.The
conda list > requirements.txtcommand lists the libraries installed manually (i.e. the packages installed viaconda install) but also ALL the libraries installed with Conda. However, if the commandpip list > requirements.txtis used then the default Conda libraries are not included.The following is equivalent to
pip freeze:conda list --export > requirements.txtSave package URLs from Anaconda repository with the following command:
conda list --explicit > requirements.txt - 
Alternatively, save package list inside an
environment.yamlfileconda env export > environment.yamlNOTE: this is similar to a
requirements.txtfile withconda/pip list/freeze. 
There are 2 ways:
- Use a package list from a 
requirements.txtfile - Use a package list from an 
environment.yamlfile 
- 
Create a new environment called
conda_envfrom the new project folderconda create --prefix ./conda_env --file requirements.txt conda activate ./conda_envIMPORTANT: the
requirements.txtfile must be INSIDE the project folder path (i.e./project_name) - 
Alternatively, if
conda_envalready exists, run the following code:conda install --file requirements.txt conda activate ./conda_envNOTE: also works with a requirement file created with the
list --explicitflag. 
- 
Create a new environment called
conda_envfrom the new project folderconda env create --prefix ./conda_env --file environment.yaml conda activate ./conda_envIMPORTANT: the 'environment.yaml' file must be INSIDE the project folder path (i.e.
/project_name)NOTE: if the purpose were to create a new environment inside Miniconda's default folder, one would run the code below. No need to be inside Miniconda's
envsfolder.conda env create --file environment.yaml --name conda_env 
- 
Track (package) changes in current environment over time
conda list --revisions - 
Roll back the current environment to a previous (package) version
conda install --revision revision_numberNOTE: e.g.
conda install --revision 2. IMPORTANT: using revision '0' is not recommended.