Imagine you want to install a video downloader written in python like let’s say you-get. What would naturally come to mind would be to use pip
to install the application. This works well but the concern is that you will pollute your global environment with dependencies that will later conflict with other applications you might install. This is not terrible. The next idea will be to create a virtual environment in a folder and install it. Now we have a clean environment where we can invoke the command line (CLI) application. But, personally, I don’t want to have to go to that folder every time, activate the virtual environment and then call the CLI. It is too cumbersome, I want to be able to call it everywhere in my shell by just calling the command name. This is where pipx
comes in. It installs applications globally in its own virtual environment to avoid dependencies conflicts and they are available everywhere!
Installation
To install pipx, you will need python3.7+ and then you can run:
$ pip install --user pipx
$ pipx ensurepath
Yeah ironically, we need pip
to install pipx
, but this is the best way to install it 😄. The second command ensures that the folder where applications will be installed is in the path environment variable.
On Mac OS, you can install it using the venerable brew
command.
$ brew install pipx
$ pipx ensurepath
Usage
The first command you may want to run is pipx completions
. It will print instructions to get command completion for your shell if supported (spoiler alert: Windows users, your shell is not considered! 😛).
To list all packages installed with pipx, you can type pipx list
. Probably your list is empty now, so let’s add the video downloader application I mentioned below.
$ pipx install you-get
Now, if you list your applications, you will have something like the following:
$ pipx list
venvs are in /home/lewoudar/.local/pipx/venvs
apps are exposed on your $PATH at /home/lewoudar/.local/bin
package you-get 0.4.1650, installed using Python 3.7.16
- you-get
There is some interesting information here.
The first line shows the folder where applications are installed.
The second line explains how the applications are available globally. In my case, there are exposed in my path environment variable via the folder
/home/lewoudar/.local/bin.
If I look at that folder, I see that there are symbolic links to the executables exposed in/home/lewoudar/.local/pipx/venvs.
$ ls -al /home/lewoudar/.local/bin
...
lrwxrwxrwx 1 lewoudar lewoudar 52 janv. 10 19:55 you-get -> /home/lewoudar/.local/pipx/venvs/you-get/bin/you-get
The third line shows the package installed, its version, and the version of python used to install it.
To see that it works, let’s download this video of cute animals on Youtube.
$ you-get https://www.youtube.com/watch?v=ZuRLolB4N8U
site: YouTube
title: Cute Animals for When You are Stressed
stream:
- itag: 22
container: mp4
quality: medium
size: 54.8 MiB (57413911 bytes)
# download-with: you-get --itag=22 [URL]
Downloading Cute Animals for When You are Stressed.mp4 ...
100% ( 54.8/ 54.8MB) ├███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████┤[1/1] 5 MB/s
Saving Cute Animals for When You are Stressed.en.srt ... Done.
It works! You can try the command from different locations in your shell, it will always work, great!
If you don’t need anymore this application you can delete it with:
$ pipx uninstall you-get
If for some reason, the command line doesn’t work as expected you can try to reinstall it with:
$ pipx reinstall you-get
One cool feature of pipx is that you can actually run applications without installing them! This is useful if you have some security concerns about an application or you don’t think you will use it twice, so it is useless if it takes you more space disk.
In this case, the application is installed in a temporary folder and cached for a few days. Let’s try it with the pycowsay command, an implementation of the venerable cowsay command in python.
$ pipx run pycowsay hello substack users!
---------------------
< hello substack users! >
---------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
If you list your pipx
packages, you will not see pycowsay but you are able to use it. 🙃
You can also use projects from source control or URLs. Here are some examples.
$ pipx run --spec git+https://github.com/psf/black.git black
# branch of your choice
$ pipx run --spec git+https://github.com/psf/black.git@branch black
# git hash
$ pipx run --spec git+https://github.com/psf/black.git@ce14fa8b497bae2b50ec48b3bd7022573a59cdb1 black
# install a release
$ pipx run --spec https://github.com/psf/black/archive/18.9b0.zip black
# install from URL
$ pipx run https://gist.githubusercontent.com/cs01/fa721a17a326e551ede048c5088f9e0f/raw/6bdfbb6e9c1132b1c38fdd2f195d4a24c540c324/pipx-demo.py
pipx is working!
This is all for this article, hope you enjoy reading it. Take care of yourself and see you next time! 😁