devsetup1-5 Exercise 3: pipx, and Choosing the Right Tool =========================================================== PART 1: pipx ------------ pipx ensurepath # once; log out and back in afterwards pipx install httpie pipx list command -v http http --version Expected: - pipx list shows httpie and its version, and the commands it provides (http, https, httpie). - command -v http prints a path in ~/.local/bin (for example /home/you/.local/bin/http). - The tool works from any directory. Show that its libraries are isolated: python3 -c "import httpie" This fails with ModuleNotFoundError, because httpie's libraries live in its own private environment, not in the system Python. (Look at ~/.local/share/pipx/venvs/httpie to see that environment.) Clean up: pipx uninstall httpie Note where the command lives: ~/.local/bin is the "installed for one user, outside apt" location from Chapter 3. dpkg -S ~/.local/bin/http would report that no package owns it. PART 2: CHOOSING THE TOOL ------------------------- 1. A Flask project that needs Flask, SQLAlchemy and a few other libraries. -> A virtual environment inside the project (python3 -m venv .venv), with the packages recorded in requirements.txt. Libraries belong to a project, and the project should be reproducible. 2. A code formatter you want to run on any file from anywhere. -> pipx. It is a program, not a library for one project; pipx gives it a private environment and a normal command. 3. A project that must run on Python 3.11. -> pyenv, to build 3.11 (Debian 13 ships 3.13), then pyenv local 3.11.x in the project, and a venv created with that Python. The .python-version file records the choice. 4. A library needed by a program you installed with apt. -> apt, using the python3-NAME package. The program runs on the system Python, so its libraries should come from Debian and stay consistent with it. Do not pip install it. 5. A quick throwaway experiment with a package you have never used. -> A temporary virtual environment (for example in /tmp or a scratch folder). Create it, try the package, delete the folder. Nothing is left behind, and the system Python is untouched. THE PATTERN ----------- Ask what you are installing: a library for a project -> venv a command-line tool -> pipx a different Python version -> pyenv something the OS itself uses -> apt WHY THIS WORKS AS AN ANSWER --------------------------- Part 1 demonstrates isolation directly instead of just describing it. Part 2 reduces four tools to one question, so the choice becomes routine rather than something to look up each time.