2023-02-14 09:16:11 +00:00
|
|
|
---
|
|
|
|
tags: [python, data-types]
|
|
|
|
---
|
|
|
|
|
|
|
|
# Package management
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
- You can use `conda` (the package manager that comes with `anaconda`). This
|
|
|
|
makes it easier to work with conflicting package libraries (a bit like a
|
|
|
|
package lock).
|
2023-02-14 09:16:11 +00:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
- The alternative is the native `pip` but you have to create virtual
|
|
|
|
environments (`venv`) to manage packages at different versions.
|
2023-02-14 09:16:11 +00:00
|
|
|
|
2025-01-16 07:08:09 +00:00
|
|
|
## venv
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
To make use of virtual environments in `pip` you have to create the virtual
|
|
|
|
environment before installing anything:
|
2023-02-14 09:16:11 +00:00
|
|
|
|
2023-08-15 19:20:22 +01:00
|
|
|
```
|
|
|
|
python3 -m venv venv
|
|
|
|
```
|
2023-02-14 09:16:11 +00:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
This will create a `venv` directory in your project that will manage the
|
|
|
|
handling of modules.
|
2023-02-14 09:16:11 +00:00
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
> This is especially important in ArchLinux since without a virtual environment
|
|
|
|
> it will ask you to install packages with `pacman` which is annoying for local
|
|
|
|
> packages.
|
2023-02-14 09:16:11 +00:00
|
|
|
|
2023-08-18 19:35:50 +01:00
|
|
|
You then activate the environment with:
|
|
|
|
|
|
|
|
```
|
|
|
|
source venv3/bin/activate
|
|
|
|
```
|
|
|
|
|
|
|
|
Now you can install packages:
|
|
|
|
|
|
|
|
```
|
|
|
|
pip [library_name]
|
|
|
|
```
|
|
|
|
|
2025-01-16 07:08:09 +00:00
|
|
|
### Using venv after a system upgrade
|
|
|
|
|
|
|
|
If you update your system's version of Python, this can cause `venv` to stop
|
|
|
|
working. Resolve as follows:
|
|
|
|
|
|
|
|
```
|
|
|
|
deactivate # leave venv env if in it
|
|
|
|
rm -rf venv # remove venv
|
|
|
|
python -m venv venv # reinstall venv
|
|
|
|
source venv/bin/activate
|
|
|
|
python -m ensurepip --upgrade # explicitly install pip
|
|
|
|
pip install - e.
|
|
|
|
```
|
|
|
|
|
2023-08-15 19:20:22 +01:00
|
|
|
## requirements.txt
|
|
|
|
|
|
|
|
The `requirements.txt` file is similar to the `package.json` in Node projects.
|
|
|
|
|
2024-02-02 15:58:13 +00:00
|
|
|
Each line of the `requirements.txt`` file specifies a package that your project
|
|
|
|
depends on and, optionally, the desired version of that package. When you share
|
|
|
|
your project with others or move it between different environments (e.g., from
|
|
|
|
development to production), this file makes it easy to set up your environment
|
|
|
|
with the right dependencies.
|
2023-08-15 19:20:22 +01:00
|
|
|
|
|
|
|
### Example
|
|
|
|
|
|
|
|
```
|
|
|
|
Flask==1.1.2
|
|
|
|
requests>=2.24.0
|
|
|
|
numpy~=1.19.2
|
|
|
|
pandas
|
|
|
|
```
|
|
|
|
|
|
|
|
### Generate requirements file
|
|
|
|
|
|
|
|
```
|
|
|
|
pip freeze > requirements.txt
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## Pypy
|
|
|
|
|
|
|
|
- pypi.org is package registry like NPM
|