TranslateProject/sources/tech/20210719 Run Python applications in virtual environments.md
DarkSun f116aee074 选题[tech]: 20210719 Run Python applications in virtual environments
sources/tech/20210719 Run Python applications in virtual environments.md
2021-07-20 05:05:31 +08:00

6.0 KiB

Run Python applications in virtual environments

Avoid versioning conflicts and improve security by running Python applications in isolation with pipx. 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 &lt;3 OSDC"                                                                                                                                      
  _________
| I &lt;3 OSDC |
  =========
        \
        \
                ^__^
                (oo)\\_______
                (__)\           )\/\
                ||----w |
                ||      ||

Cowsay

(Sumantro Mukherjee, CC BY-SA 4.0)

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.


via: https://opensource.com/article/21/7/python-pipx

作者:Sumantro Mukherjee 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出