How to use the command 'pipx' (with examples)
This article provides examples of using the command ‘pipx’, which is a tool used to install and run Python applications in isolated environments.
Use case 1: Run an app in a temporary virtual environment
Code:
pipx run pycowsay moo
Motivation: By using ‘pipx run’, you can quickly run Python applications in a temporary virtual environment. This is useful when you want to test an application or execute a specific command without affecting your global Python environment.
Explanation:
pipx run
: This command runs a Python application in an isolated environment.pycowsay moo
: This is the name of the Python application and the command you want to execute.
Example OUTPUT:
______
< moo! >
------
\ ^__^
\ (oO)\_______
(__)\ )\/\
||----w |
||
Use case 2: Install a package in a virtual environment and add entry points to path
Code:
pipx install package
Motivation: Installing a package using ‘pipx’ allows you to set up an isolated virtual environment specifically for that package. This helps prevent conflicts with other packages and ensures the package has all the required dependencies.
Explanation:
pipx install
: This command installs a Python package in an isolated environment.package
: This is the name of the package you want to install.
Example OUTPUT:
Installed package package to location /path/to/virtual/environment
Use case 3: List installed packages
Code:
pipx list
Motivation: This command allows you to view all the packages installed in isolated environments using ‘pipx’. It gives you an overview of the packages you have installed and their versions.
Explanation:
pipx list
: This command lists all the packages installed using ‘pipx’.
Example OUTPUT:
Package Version Location
------- ------- ---------------------------------
package 1.0 /path/to/virtual/environment
Use case 4: Run an app in a temporary virtual environment with a package name different from the executable
Code:
pipx run --spec httpx-cli httpx http://www.github.com
Motivation: Sometimes, a package has a different name for the executable compared to its package name. By using the ‘–spec’ option, you can specify the package name to be different from the executable name.
Explanation:
pipx run
: This command runs a Python application in an isolated environment.--spec httpx-cli
: This option specifies the package name to be different from the executable name.httpx
: This is the command you want to execute.http://www.github.com
: This is an argument passed to the ‘httpx’ command.
Example OUTPUT:
Output of the httpx command executed with the specified arguments.
Use case 5: Inject dependencies into an existing virtual environment
Code:
pipx inject package dependency1 dependency2 ...
Motivation: Sometimes, you may need to add additional dependencies to an already existing virtual environment. The ‘pipx inject’ command allows you to inject new dependencies without reinstalling the entire package.
Explanation:
pipx inject
: This command injects additional dependencies into an existing virtual environment.package
: This is the name of the package whose environment you want to modify.dependency1 dependency2 ...
: These are the additional dependencies you want to inject.
Example OUTPUT:
Injected dependencies dependency1, dependency2 into the package environment.
Use case 6: Install a package in a virtual environment with pip arguments
Code:
pipx install --pip-args='pip-args' package
Motivation: Occasionally, you may need to pass additional arguments to ‘pip’ when installing a package using ‘pipx’. The ‘–pip-args’ option allows you to specify custom pip arguments during installation.
Explanation:
pipx install
: This command installs a Python package in an isolated environment.--pip-args='pip-args'
: This option allows you to pass custom pip arguments during installation.package
: This is the name of the package you want to install.
Example OUTPUT:
Installed package with custom pip arguments to location /path/to/virtual/environment
Conclusion:
The ‘pipx’ command provides a convenient way to install and run Python applications in isolated environments. Whether you want to test an application, manage dependencies, or install packages with specific configurations, ‘pipx’ has various use cases to suit your needs.