Run Python applications in virtual environments

Avoid versioning conflicts and improve security by running Python applications in isolation with pipx.
54 readers like this.
Digital creative of a browser on the internet

If you use Python, you probably install a lot of Python applications. Some are tools you just want to try out. Others are tried and true applications you use every day, so you install them on every computer you use. In either situation, it can be useful to run your Python applications in virtual environments to keep them and their dependencies separate from one another to avoid versioning conflicts and to keep them from the rest of your system to improve security.

This is where pipx comes into the picture.

Most Python apps can be installed using pip, which just installs the Python package. Pipx, however, creates and manages a virtual environment for your Python applications and helps you run them.

Installing pipx

Pipx is primarily an RPM package, and you can install it on any Fedora, RHEL, or CentOS machine with:

$ sudo dnf install pipx

Using pipx

I'll demonstrate how to use pipx with the Cowsay package and the Concentration tool.

Installing packages

After pipx is installed, you can install Python packages with:

$ pipx install <python_package>

To install the Cowsay package:

$ pipx install cowsay                                                 ✔ │ 20:13:41   
  installed package cowsay 4.0, Python 3.9.5
  These apps are now globally available
	- cowsay
done! ✨ ? ✨

Now you can run Cowsay anywhere in your system to talk to you through your terminal!

$  cowsay "I <3 OSDC"                                                                                                                                       
  _________
| I <3 OSDC |
  =========
     	\
      	\
        	^__^
        	(oo)\_______
        	(__)\   	)\/\
            	||----w |
            	|| 	||

Installing with special permissions

Not all applications are as simple as Cowsay. For instance, the Concentration application interacts with many other components on your system, so it requires special permissions. Install it with:

$ pipx install concentration                                                                           ✔ │ 10s  │  │ 20:26:12   
  installed package concentration 1.1.5, Python 3.9.5
  These apps are now globally available
	- concentration
done! ✨ ? ✨

Concentration is designed to help you focus by blocking specific websites listed in a "distractors" file. To do that, it needs to run with sudo or root privileges. You can do this with OpenDoas, a version of the doas command that runs any command with specific user privileges. To use doas to run Concentration with sudo privileges:

$ doas concentration improve                                                                  ✔ │  │ 20:26:54   
doas (sumantrom) password:  
Concentration is now improved :D!

As you can see, the isolated application was able to change something in the system.

Listing installed apps

The pipx list command displays all apps installed with pipx and their executable paths:

$ pipx list                                                                                                                                              
venvs are in /home/sumantrom/.local/pipx/venvs
apps are exposed on your $PATH at /home/sumantrom/.local/bin
   package concentration 1.1.5, Python 3.9.5
	- concentration
   package cowsay 4.0, Python 3.9.5
	- cowsay

Uninstalling apps

It's important to know how to uninstall things when you're finished with them. Pipx has a very simple uninstall command:

$ pipx uninstall <package name>

Or you can remove every package:

$ pipx uninstall-all 

pipx uninstall-all                                                     2 ✘ │ 20:13:35   
uninstalled cowsay! ✨ ? ✨
uninstalled concentration! ✨ ? ✨

Try pipx

Pipx is a package manager for popular Python applications. It has access to everything on PyPi, but it can also install applications from a local directory containing a valid Python package, a Python wheel, or a network location.

If you install a lot of Python apps, try pipx.

What to read next
Tags
User profile image.
Hey, open source folks! I am Sumantro, hailing from India (the eastern part - former capital during the British era AKA Kolkata). I love sharing knowledge and writing about technology and experiences (mostly that I try every day).

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.