I follow some simple rules for [[Python]] packages. - Don't use the OS installed Python - Never touch the OS site-packages with `pip` - Never run `pip` with escalated privileges (`sudo`) - Always use a local (`$HOME`) `pip` - Use the OS package manager if installing to `site-packages` to avoid competing package managers (`pip` and `dnf`, for example) Where’s `site-packages` located? ```bash python -m site python -m user-site ``` Read up on the Python docs and the [pip docs](https://pip.readthedocs.io/en/latest/user%5Fguide/#user-installs) to get a better understanding. # asdf In late 2021 I started using [[asdf]] for [[Python]] to avoid broken virtual environments after system upgrades. I have surrendered. The OS can have Python all by itself. Yeah, well, I'm gonna go build my own theme park. Install `asdf` by following [`asdf`'s getting started instructions](http://asdf-vm.com/guide/getting-started.html) and run: ```bash asdf plugin add python asdf install python latest asdf global python latest ``` `asdf` can also be used for [[Terraform]], [[Node.js]] and many other things. # How to win Once Python is installed and set up we will install only one more package in the user-environment: `pipx`. Make sure you are actually using the local pip by running `which pip`. ```bash python -m pip install --user pipx ``` After this command succeeds, a `pipx` binary will be placed in `~/.local/bin`. Make sure you have this folder **prepended** to your `$PATH`: ```bash export PATH=$HOME/.local/bin:$PATH ``` `pipx` installs each package into `~/.local/pipx/venvs/PKGNAME` and then symlinks the scripts (CLI entry points) into `~/.local/bin`. This makes it so you don’t have to activate the virtual environments manually to use the programs. We can use `pipx` to install `pew` (a virtualenv manager): ```bash pipx install pew ``` I prefer `pipx` over `pipsi` as it lets you easily upgrade the virtual environments after a system upgrade of Python (see [#146](https://github.com/pipxproject/pipx/issues/146)) (although `asdf` circumvents this entirely): ```bash pipx reinstall-all ``` # Overview ```bash pipx --version pipx list pip -V ``` # Jupyter notebooks I like to follow the same principles for Jupyter notebooks. ```bash pipx install notebook ``` Create a virtualenv that can be used as a notebook kernel: ```bash pew new pandas3 --python=python3 pip install ipykernel pip install pandas ``` Create the kernel spec: ```bash ipykernel install --user --name 'python3-pandas' --display-name 'Python 3 (venv pandas3)' ``` It will be placed in `~/.local/share/jupyter/kernels/python3-pandas/kernel.json` Whenever you want to use `pandas` in Jupyter you create a new notebook with this kernel. # Other I use the OS package manager to install some packages, for example `python3-neovim` and `python2-neovim` in the Debian repos. Hopefully they are optimized to work with the `neovim` package. # Inspiration [Hitchhiker’s guide to Python](http://docs.python-guide.org/en/latest/starting/install/linux/). Now, tell me how to do it even better. # Updates - <2021-12-28> - Recommend `asdf` instead of OS Python - <2020-02-16> - Recommend `pipx` over `pipsi` - <2020-02-16> - Don’t install `virtualenv` (use `python3 -m venv` instead)