Python Installation

Anh-Thi Dinh

Windows

Update 11/Sep/20: Install python on WSL2 using Miniconda.

MacOS

If you use Apple M1, check note DS & ML with Mac M1.
By default, Python 2 is already installed on MacOS, you can check this by
1python --version
2# to be sure, check if python3 is installed?
3python3 --version

Linux (Ubuntu)

Python is already installed on Ubuntu. You would like to install Anaconda, download and install it.
Wanna install Miniconda instead? 👉 Download .sh file and install inside Linux environement (including WSL2).

Jupyer Notebook

👉 Note: Jupyter notebook
If you use VSCode, you should use its Jupyter Notebook extension, it's quick, clean and very easy to use.
Anaconda contains JN in it, no need to install it again. cd to the folder you wanna work on and run
1# RUN (after installing Anaconda)
2python -m notebook
1# If `ImportError: DLL load failed`
2active base # active env "base" in anaconda
3jupyter notebook
The -m option allows you to execute a module or package as a script (ref)
1# If `import _ssl`, `ImportError`
2set CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1
3python -m notebook

Check GPU

👉 Note: PyTorch
1# with pytorch
2import torch
3print('cuda is available? ', torch.cuda.is_available())
4print('device_count: ', torch.cuda.device_count())
5print('current device: ', torch.cuda.current_device())
6print('device name: ', torch.cuda.get_device_name(0))
1# with tensorflow
2import tensorflow as tf
3print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

pip

Update pip

1# Check pip version
2pip -V
1# update pip
2easy_install -U pip

Install packages with pip

Install pip (It's actually installed with Anaconda). If you wanna upgrade it to the latest version:
1python -m pip install --user --upgrade pip # install for current user only
2python -m pip install --upgrade pip # need to run cmder as administrator
1# INSTALL A PACKAGE
2pip install <package> # admin <-- SHOULDN'T!!!
3pip install --user <package> # current user only
4
1# LIST ALL INSTALLED PACKAGES
2pip freeze
1# REMOVE
2pip uninstall <package>
3pip uninstall --user <package>
1# CHECK VERSION OF A PACKAGE
2pip show <package>
1# version <=
2pip3 install -U "pillow<7"
1# Install a package from a git repository
2pip install git+https://github.com/TimeSynth/TimeSynth.git
If install packages with pip, they are installed in which environment of conda? Where pip is executed from.
1which python
2which pip
3
4conda info --envs
5# or
6# conda env list
1# Ouput
2/c/ProgramData/Anaconda3/python
3/c/ProgramData/Anaconda3/Scripts/pip
4
5# conda environments:
6base                  *  C:\\ProgramData\\Anaconda3
7fastai                   C:\\Users\\thi\\.conda\\envs\\fastai
8
Install packages with requirement file,
1pip install -r requirements.txt

pip vs conda?

👌 Fact: each conda environment has a pip, just install package in each env with the corresponding pip.
1conda activate base
2which pip # /Users/thi/miniforge3/bin/pip
3
4conda activate working
5which pip # /Users/thi/miniforge3/envs/working/bin/pip

Python virtual environnement

👌
Fact: Use conda!

pyenv

👉 Read more on SO. Note that, these methods didn't work on MacOS M1.
👌
Fact: Use conda!

Conda

Install / Update conda

1# INSTALL CONDA BY PIP (without Anaconda)
2pip install conda
1# UPDATE CONDA
2conda --version # check version
3conda update -n base -c defaults conda

Install packages with conda

👌
Fact: Activate an environment conda, use its pip to install any package.

Conda's environements

👉 Check an official doc or this useful post.
1# Create a new environment with python version 3.7
2conda create -n <env-name> python=3.10
3# activate this env
4source activate <env-name>
👇
Most of below commands are assumed to be run in an environment named env which is already activated.
1# Activate an env
2activate <env> # windows
3source activate <env> # linux / macos
1# Deactivate an env
2conda deactivate # Linux
3deactivate # Windows
4source deactivate # MacOS
1# REMOVE AN ENV
2conda remove -n <env> --all
1# SHOW LIST OF CURRENT ENV
2conda info --envs
3# or
4conda env list
1# EXPORT env list TO A ENV FILE
2conda env export -f <file>.yml
1# Rename an env (conda >= 4.14)
2conda rename -n old_name -d new_name
1# Update packages listed in an env file to current env,
2conda env update -n <env> -f /path/to/<file>.yml --prune
We cannot change the default environment in conda!

Kernel 2 & 3 for Jupyter Notebook

Check if nb_conda_kernels is installed by conda list. If not, install it by:
1conda install nb_conda_kernels
If you are using Python 2 and you wanna separate Python 3
1conda create -n py37 python=3.7 ipykernel # "py37" is a custom name
If you are using Python 3 and you wanna separate Python 2
1conda create -n py27 python=2.7 ipykernel # "py37" is a custom name
Restart the Jupyter Notebook, the list of kernels is available under New.

Conda Revisions

1# Check revisions
2conda list --revisions
1# Go back to revision `1`,
2conda install --revision 1

Error?

1# UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 975: ordinal not
2# solution: instead of
3pip3 install sesd
4# use
5LC_ALL=C.UTF-8 pip3 install sesd
1# conda: The following packages are not available from current channels:
2# Solution 1: One can use pip in this case (the same env with conda)
3# Solution 2:
4conda install -c anaconda <package>
1# The following required packages can not be built: * freetype (from matplotlib)
2# try to use conda to install matplotlib
3conda install matplotlib
4# it actually install the same thing as pip does on the same env
1# dtaidistance: C-library is not available
2pip install -vvv --upgrade --force-reinstall dtaidistance
1# zsh: command not found: conda
2# Make sure your installation folder is already
3# in the $PATH
4export PATH="/home/thi/miniconda3/bin:$PATH"
Loading comments...