This notebook can be viewed as a slideshow by using the (RISE)[https://rise.readthedocs.io/en/stable/index.html] slide show extension for Jupyter.
Note: RISE only works with jupyter notebook
, not with jupyter lab
:-(
If you are working in an up to date clone of the (UBC-MOAD/PythonNotes repo)[https://github.com/UBC-MOAD/PythonNotes], you can run the slideshow locally. To do so:
jupyter
and rise
with:conda env create -f PythonNotes/pkgs-envs/environment.yaml
jupyter notebook
PythonNotes/pkgs-envs/PythonPkgsEnvsSlides.ipynb
Alt+r
or the Enter/Exit RISE Slideshow
toobar button to start/stop the slideshow modeSpace
and Shift+Space
to navigate forward and backward through the slide cellsconda
and pip
conda env
and virtualenv
conda env create ...
conda env update ...
pip install -r ...
pip install -e ...
pip install --user ...
Standard Library (Included with the language) (https://docs.python.org/3/library/index.html)
Built-in Functions, Built-in Constants, Built-in Exceptions
Python modules that you import
import
things from them.py
fileimport
statementsclass
)def
)Example: nowcast.workers.collect_river_data.py
Because sys.path.append(...)
doesn't scale 😱
pip install
... permission deniedsudo pip install
~ 😱pip
; it is separate, but highly recommendedScientific Python community, lead by Travis Oliphant in 2012, couldn't wait for the Python Packaging Authority (PyPA)'s plans for pip, wheels, and a sophisticated dependency resolver to come to fruition
pip
can be used inside conda
-managed environmentsconda
and pip
conda env
and virtualenv
conda env create ...
conda env update ...
pip install -r ...
pip install -e ...
pip install --user ...
pip install
... permission deniedsudo pip install
~ 😱conda env
and virtualenv
Environments¶conda env
¶The first choice!
Use them everywhere (except on Compute Canada HPC clusters):
salish
If you have anaconda
installed,
you have conda env
.
If you are starting from scratch,
use Miniconda
to get the conda
package manager and conda env
environment manager
without the hundreds of packages in the anaconda
meta-package.
python3 -m virtualenv
¶Use them on Compute Canada HPC clusters.
anaconda
on clusters.module load python/3.8.2
(or whatever is the latest version)
includes virtualenv
and pip
.
By Picasa user Seedambassadors - http://picasaweb.google.com/seedambassadors/SscVarieties#5296490359767135106&, CC BY-SA 3.0, Link
They are not pets.
They are not unique snowflakes.
They are not precious, artisinal, heritage artefacts.
Just as rutabagas are planted, harvested, eaten, and composted...
Environments should be created, used, and destroyed and replaced when they get old or rotten.
Create them carefully for specific pieces of work.
Track them using Git.
Use them to update your environments.
Commit changes.
conda env
¶Create an environment description file: often environment.yaml
Create the environment:
conda env create -f environment.yaml
Activate the environment:
conda activate env-name
To install a new package, edit environment.yaml
, and update the environment:
conda env update -f environment.yaml
The environment.yaml
for this notebook/slideshow:
!cat environment.yaml
# conda environment description for Python packages and environments # session # # Create a conda environment in which the notebooks in this repo can be run # with # # conda env create -f environment.yaml name: moad-pkgs-envs channels: - conda-forge dependencies: - jupyterlab - python=3 - rise
conda env
¶Recall that environments are (mainly) about 2 things: a directory tree, and managing PATH
conda env create
sets up the directory tree and installs the isolated packages in itconda activate
does the PATH
environment variable manipulationconda
Environment Description Files¶environment.yaml
files with your code or notebooksenvironment.yaml
files in Git when you create them,
and whenever you change thempip
as a dependencypip
to install packages from PyPI because there is no conda
package!cat /media/doug/warehouse/MEOPAR/NEMO-Cmd/envs/environment-dev.yaml
# conda environment description file for NEMO-Cmd package # development environment # # Create a conda environment in which the `nemo` command can be run # with: # # $ conda env create -f NEMO-Cmd/envs/environment.yaml # $ source activate nemo-cmd # (nemo-cmd)$ pip install --editable NEMO-Cmd # # The environment will also include all of the tools used to develop, # test, and document the NEMO-Cmd package. # # See the requirements.txt file for an exhaustive list of all of the # packages installed in the environment and their versions used in # recent development. name: nemo-cmd channels: - conda-forge - defaults dependencies: - arrow - attrs - cliff!=2.9.0 - f90nml - gitpython - pip - python=3.9 - pyyaml # For coding style - black # For unit tests - pytest - pytest-cov # For documentation - sphinx - sphinx_rtd_theme - pip: - python-hglib # For unit tests - pytest-randomly
conda
and pip
conda env
and virtualenv
conda env create ...
conda env update ...
pip install -r ...
pip install -e ...
pip install --user ...
python3 -m virtualenv
¶Load a Python module:
module load python/3.8.2
Create the virtual environment:
python3 -m virtualenv --no-download ~/venvs/env-name
Activate the environment:
source ~/venvs/env-name/bin/activate
python3 -m virtualenv
¶Create an environment description file: often requirements.txt
Install the packages:
python3 -m pip install -r requirements.txt
To install a new package, edit requirements.txt
, and do an install from it:
python3 -m pip install -r requirements.txt
The requirements.txt
for a useful jupyterlab
environment on graham
:
!cat requirements-graham-jupyter.txt
# virtualenv environment description for a useful jupyter # environment on a Compute Canada cluster # # Create a virtualenv containing these packages with: # # module load python/3.8.2 # python3 -m virtualenv --no-download ~/venvs/jupyter # source ~/venvs/jupyter/bin/activate # python3 -m pip install -r requirements-graham-jupyter.txt bottleneck cmocean jupyterlab matplotlib netCDF4 xarray
python3 -m virtualenv
¶Recall that environments are (mainly) about 2 things: a directory tree, and managing PATH
python3 -m virtualenv
sets up the directory treesource ~/venvs/env-name/bin/activate
does the PATH
environment variable manipulationpython3 -m pip install -r requirements.txt
installs the packages into the environmentvirtualenv
Environment Description Files¶requirements.txt
files with your code or notebooksrequirements.txt
files in Git when you create them,
and whenever you change thempython3 -m virtualenv
???¶The -m
option on python3
means:
Search sys.path for the named module and execute
its contents
This ensures that the virtualenv
(or other package module) that you run is the one associated with the presently activate Python environment.
If there is no environment active,
it uses the system Python 3,
or the one from the HPC module you loaded.
Getting things installed in the wrong environment is one of the biggest pain-points of using environments.
python3 -m
avoids that.
List of packages you decided to install
List of all packages with exact version details (version pinning)
conda env export
¶conda env export > environment-pinned.yaml
pip
installed packages from original environment descriptionDocs: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
pip list
¶python3 -m pip list --format=freeze > requirements-pinned.txt
python3 -m virtualenv
environments on Compute Canada clustersconda
and pip
conda env
and virtualenv
conda env create ...
conda env update ...
python3 -m pip install -r ...
python3 -m pip install -e ...
python3 -m pip install --user ...
python3 -m pip install -e
???¶The -e
option (short for --editable
) on pip install
means:
Install the package using symbolic links,
such that it’s available on sys.path,
yet can still be edited directly from its source files.
We use this for our group-developed packages.
It makes the workflow for getting updates into our installed packages (usually) a simple git pull
in the package repository clone directory.
Example:
python3 -m pip install -e moad_tools
python3 -m pip install -e
¶Editable installs facilitate:
python3 -m pip install -e
¶Editable installs avoid:
Click
or Cliff
for their CLIpython3 -m pip install --user package
import
from the package)~/.local/
, but python3 -m site --user-base
will say for sure~/.local/bin
is near the front of your PATH
We use this on HPC machines to install packages like NEMO-Cmd
, SalishSeaCmd
, and MOHID-Cmd
from our Git clones:
python3 -m pip install --user -e $PROJECT/$USER/MEOPAR/NEMO-Cmd/
It makes it so that you can do nemo run
, salishsea run
, or mohid run
without worrying about activating a Python environment.
Relies on:
module load python/3.8.2
$HOME/.local/bin
in your PATH
conda
and pip
conda env
and virtualenv
conda env create ...
conda env update ...
pip install -r ...
pip install -e ...
pip install --user ...
conda env
to create and update "project-specific" environmentspython3 -m virtualenv
on Compute Canadaenvironment.yaml
or requirements.txt
environment descriptions with Git