From 4a031bf0dca29ce5ae9ec49983f1c3fa82a0bfa6 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Thu, 20 Jun 2024 15:36:53 +0000 Subject: [PATCH] markdown source builds Auto-generated via {sandpaper} Source : cf62deec9734d28d2e1eb32e054007798cff5e86 Branch : main Author : christopher-wild <93984046+christopher-wild@users.noreply.github.com> Time : 2024-06-20 15:36:08 +0000 Message : Merge pull request #11 from christopher-wild/acessing_packages Accessing packages episode --- accessing-packages.md | 149 ++++++++++++++++++++++++++++++++++++++++++ md5sum.txt | 3 +- 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 accessing-packages.md diff --git a/accessing-packages.md b/accessing-packages.md new file mode 100644 index 0000000..3eb077a --- /dev/null +++ b/accessing-packages.md @@ -0,0 +1,149 @@ +--- +title: 'Accessing Packages' +teaching: 30 +exercises: 2 +--- + +:::::::::::::::::::::::::::::::::::::: questions + +- What are the different ways of downloading python packages? +- What are package managers? +- How can I access my own package? + +:::::::::::::::::::::::::::::::::::::::::::::::: + +::::::::::::::::::::::::::::::::::::: objectives + +- Learn about package managers such as PIP +- Install packages using PIP +- Install packages from source + +:::::::::::::::::::::::::::::::::::::::::::::::: + + +## Introduction + +Due to Pythons popularity as a language, it is quite likely that you won't be the first person to set off on solving any particular task. +Many others have worked on common problems and then shared their solution in the form of a package, which you can conveniently integrate into your own code and use! + +::: callout + +### Popular Packages +Some of the most popular packages you may have heard of are: + +- Numpy +- Pandas +- Tensorlow +- Matplotlib +- Requests + +::: + +To use a package that is installed you use the key word `import` in python. + +```python +# This imports the pandas package and gives it a new name 'pd'. +import pandas as pd + +# Use the package to read a file +pd.read_csv("/my_data.csv") +``` + +## Python Package Index (PyPI) + +The Python Package Index or PyPI is an online repository of Python packages hosting over 500,000 packages! While there are alternatives such as [conda-forge](https://conda-forge.org), PyPI is by far the most commonly used and likely to have all you need. + +::: challenge + +### Exercise 1: Explore PyPI + +Explore [PyPI](https://pypi.org/project/pip/) to get familiar with it, try searching for packages that are relevant to your research domain / role! + +::: + +::: callout +### pip + +pip (package installer for Python) is the standard tool for installing packages from PyPI. +You can think of PyPI being the supermarket full of packages and pip being the delivery van bringing it to you. + +::: + + +### Using pip + +pip itself is a python package that can be found on [PyPI](https://pypi.org/project/pip/). It however comes preinstalled with most python installations, for example [python.org](https://python.org) and inside virtual environments. + +The most common way to use pip is from the command line. At the top of a package page on PyPI will be the example line you need to install the package + +``` +py -m pip install numpy +``` + +The above will install [numpy](https://pypi.org/project/numpy/) from PyPI, a popular scientific computing package enabling a wide range of mathematical and scientific functions. + + +::: challenge +### Exercise 2: Create venv and install Numpy + +Step 1: Create a venv in the .venv directory using the command `py -m venv .venv` and activate it with + +::: tab + +### Windows + +`.\.venv\Scripts\activate` + + + +### macOS / Linux + +`source .venv/bin/activate` + + +::: + +Step 2: Install Numpy into your new environment + +Step 3: Check your results with `py -m pip list` + +Step 4: Deactivate your environment with `deactivate` + +::: + +::: spoiler + +### Virtual Environments + +Check out [this documentation](https://docs.python.org/3/l[PyPI](https://pypi.org/project/pip/)ibrary/venv.html) or the FAIR4RS course on virtual environments to learn more! + +::: + + +pip can also be used to install packages from source. This means that the package file structure (source) is on your local computer and pip installs it using the instructions from the `setup.py` or `pyproject.toml` file. This is especially handy for packages either not on PyPI, like ones downloaded from github, or for your own packages you're developing. + +``` +py -m pip install . +``` + +::: instructor + +The above command should be universal on both windows and mac/unix setups. It may be worth checking with the class at this point that they are all familiar with the -m notation, and what the above command does exactly + +::: + +Here the `.` means to install your current directory as a Python package. For this to work the directory your command line interface is currently in needs to have a packaging file, i.e. `setup.py` or `pyproject.toml`. + + + +::: keypoints +- pip can be used to download and install Python packages +- PyPI is an online package repository which pip downloads from +- pip can also install local packages like your own +::: + + + + + + diff --git a/md5sum.txt b/md5sum.txt index 4489d92..8bb1c82 100644 --- a/md5sum.txt +++ b/md5sum.txt @@ -1,11 +1,12 @@ "file" "checksum" "built" "date" "CODE_OF_CONDUCT.md" "c93c83c630db2fe2462240bf72552548" "site/built/CODE_OF_CONDUCT.md" "2024-06-20" "LICENSE.md" "b24ebbb41b14ca25cf6b8216dda83e5f" "site/built/LICENSE.md" "2024-06-20" -"config.yaml" "c6433682f9acc676400c395b08e4770b" "site/built/config.yaml" "2024-06-20" +"config.yaml" "e86c7fed4086d9313ec886318a767779" "site/built/config.yaml" "2024-06-20" "index.md" "a02c9c785ed98ddd84fe3d34ddb12fcd" "site/built/index.md" "2024-06-20" "links.md" "8184cf4149eafbf03ce8da8ff0778c14" "site/built/links.md" "2024-06-20" "episodes/introduction.md" "977c92fa5c60f351e4ead45bfab1c28d" "site/built/introduction.md" "2024-06-20" "episodes/package-file-history.Rmd" "7e70e2133c8c61dbfaca95ec89604ce6" "site/built/package-file-history.md" "2024-06-20" +"episodes/accessing-packages.md" "03c8f19155f411b7e2e3f367cfcfef85" "site/built/accessing-packages.md" "2024-06-20" "episodes/versioning.md" "425e206c183ea417ab4bd87e3d9f06a1" "site/built/versioning.md" "2024-06-20" "episodes/releasing.md" "ed18737a7aa05932c657f1bd687ea64b" "site/built/releasing.md" "2024-06-20" "instructors/instructor-notes.md" "cae72b6712578d74a49fea7513099f8c" "site/built/instructor-notes.md" "2024-06-20"