Python run installed package

pipx — Install and Run Python Applications in Isolated Environments

Upgrade pipx with python3 -m pip install —user —upgrade pipx .

On Windows, install via pip (requires pip 19.0 or later)

# If you installed python using the app-store, replace `python` with `python3` in the next line. python -m pip install --user pipx 

It is possible (even most likely) the above finishes with a WARNING looking similar to this:

WARNING: The script pipx.exe is installed in `\AppData\Roaming\Python\Python3x\Scripts` which is not on PATH 

If so, go to the mentioned folder, allowing you to run the pipx executable directly. Enter the following line (even if you did not get the warning):

This will add both the above mentioned path and the %USERPROFILE%\.local\bin folder to your search path. Restart your terminal session and verify pipx does run.

Upgrade pipx with python3 -m pip install —user —upgrade pipx .

Via zipapp

You can also use pipx without installing it. The zipapp can be downloaded from Github releases and you can invoke it with a Python 3.7+ interpreter:

python pipx.pyz ensurepath 

Shell completions

Shell completions are available by following the instructions printed with this command:

Читайте также:  Html table data width

Overview: What is pipx ?

pipx is a tool to help you install and run end-user applications written in Python. It’s roughly similar to macOS’s brew , JavaScript’s npx, and Linux’s apt .

It’s closely related to pip. In fact, it uses pip, but is focused on installing and managing Python packages that can be run from the command line directly as applications.

How is it Different from pip?

pip is a general-purpose package installer for both libraries and apps with no environment isolation. pipx is made specifically for application installation, as it adds isolation yet still makes the apps available in your shell: pipx creates an isolated environment for each application and its associated packages.

pipx does not ship with pip, but installing it is often an important part of bootstrapping your system.

Where Does pipx Install Apps From?

By default, pipx uses the same package index as pip, PyPI. pipx can also install from all other sources pip can, such as a local directory, wheel, git url, etc.

Python and PyPI allow developers to distribute code with «console script entry points». These entry points let users call into Python code from the command line, effectively acting like standalone applications.

pipx is a tool to install and run any of these thousands of application-containing packages in a safe, convenient, and reliable way. In a way, it turns Python Package Index (PyPI) into a big app store for Python applications. Not all Python packages have entry points, but many do.

If you would like to make your package compatible with pipx, all you need to do is add a console scripts entry point. If you’re a poetry user, use these instructions. Or you’re using hatch, try this.

Features

  • Expose CLI entrypoints of packages («apps») installed to isolated environments with the install command. This guarantees no dependency conflicts and clean uninstalls!
  • Easily list, upgrade, and uninstall packages that were installed with pipx
  • Run the latest version of a Python application in a temporary environment with the run command

Best of all, pipx runs with regular user permissions, never calling sudo pip install (you aren’t doing that, are you? 😄).

Walkthrough: Installing a Package and its Applications With pipx

You can globally install an application by running

This automatically creates a virtual environment, installs the package, and adds the package’s associated applications (entry points) to a location on your PATH . For example, pipx install pycowsay makes the pycowsay command available globally, but sandboxes the pycowsay package in its own virtual environment. pipx never needs to run as sudo to do this.

>> pipx install pycowsay installed package pycowsay 2.0.3, Python 3.7.3 These apps are now globally available - pycowsay done! ✨ 🌟 ✨ >> pipx list venvs are in /home/user/.local/pipx/venvs apps are exposed on your $PATH at /home/user/.local/bin package pycowsay 2.0.3, Python 3.7.3 - pycowsay # Now you can run pycowsay from anywhere >> pycowsay mooo ____ < mooo >==== \ \ ^__^ (oo)\_______ (__)\ )\/\ ||----w | || || 

Installing from Source Control

You can also install from a git repository. Here, black is used as an example.

pipx install git+https://github.com/psf/black.git pipx install git+https://github.com/psf/black.git@branch # branch of your choice pipx install git+https://github.com/psf/black.git@ce14fa8b497bae2b50ec48b3bd7022573a59cdb1 # git hash pipx install https://github.com/psf/black/archive/18.9b0.zip # install a release 

The pip syntax with egg must be used when installing extras:

pipx install "git+https://github.com/psf/black.git#egg=black[jupyter]" 

Walkthrough: Running an Application in a Temporary Virtual Environment

This is an alternative to pipx install .

pipx run downloads and runs the above mentioned Python «apps» in a one-time, temporary environment, leaving your system untouched afterwards.

This can be handy when you need to run the latest version of an app, but don’t necessarily want it installed on your computer.

You may want to do this when you are initializing a new project and want to set up the right directory structure, when you want to view the help text of an application, or if you simply want to run an app in a one-off case and leave your system untouched afterwards.

For example, the blog post How to set up a perfect Python project uses pipx run to kickstart a new project with cookiecutter, a tool that creates projects from project templates.

A nice side benefit is that you don’t have to remember to upgrade the app since pipx run will automatically run a recent version for you.

Okay, let’s see what this looks like in practice!

This will install the package in an isolated, temporary directory and invoke the app. Give it a try:

> pipx run pycowsay moo --- < moo >--- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || 

Notice that you don’t need to execute any install commands to run the app.

Any arguments after the application name will be passed directly to the application:

> pipx run pycowsay these arguments are all passed to pycowsay! ------------------------------------------- < these arguments are all passed to pycowsay! >------------------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || 

Re-running the same app is quick because pipx caches Virtual Environments on a per-app basis. The caches only last a few days, and when they expire, pipx will again use the latest version of the package. This way you can be sure you’re always running a new version of the package without having to manually upgrade.

If the app name does not match that package name, you can use the —spec argument to specify the package to install and app to run separately:

pipx run --spec PACKAGE APP 

You can also specify specific versions, version ranges, or extras:

Running from Source Control

You can also run from a git repository. Here, black is used as an example.

pipx run --spec git+https://github.com/psf/black.git black pipx run --spec git+https://github.com/psf/black.git@branch black # branch of your choice pipx run --spec git+https://github.com/psf/black.git@ce14fa8b497bae2b50ec48b3bd7022573a59cdb1 black # git hash pipx run --spec https://github.com/psf/black/archive/18.9b0.zip black # install a release 

Running from URL

You can run .py files directly, too.

pipx run https://gist.githubusercontent.com/cs01/fa721a17a326e551ede048c5088f9e0f/raw/6bdfbb6e9c1132b1c38fdd2f195d4a24c540c324/pipx-demo.py pipx is working! 

Summary

That’s it! Those are the most important commands pipx offers. To see all of pipx’s documentation, run pipx —help or see the docs.

Testimonials

—Jannis Leidel, PSF fellow, former pip and Django core developer, and founder of the Python Packaging Authority (PyPA)

Credits

pipx was inspired by pipsi and npx. It was created by Chad Smith and has had lots of help from contributors. The logo was created by @IrishMorales.

pipx is maintained by a team of volunteers (in alphabetical order)

Contributing

Issues and Pull Requests are definitely welcome! Check out Contributing to get started. Everyone who interacts with the pipx project via codebase, issue tracker, chat rooms, or otherwise is expected to follow the PSF Code of Conduct.

Источник

Pipx : Python CLI package tool

In this article, we will explore the basics of pipx python CLI package tool. Pipx is a tool in Python that allows us to run python packages that have a CLI interface in the global context of your system. It uses its own environment for managing the packages.

Here, we will cover its installations, setting up its environment, and how we can uninstall packages. This can be done by following the step by step instructions provided below:

Downloading and Installing Pandas

Now run the following command:

 demonstrated by running the same command in other places

pipx has the following available command options:

optional environment variables:

  • PIPX_HOME Overrides default pipx location. Virtual Environments will be installed to $PIPX_HOME/venvs.
  • PIPX_BIN_DIR Overrides location of app installations. Apps are symlinked or copied here.
  • USE_EMOJI Overrides emoji behavior. Default value varies based on platform.
  • PIPX_DEFAULT_PYTHON Overrides default python used for commands.

Optional arguments:

  • -h, –help show this help message and exit
  • –version Print version and exit

Subcommands: Get help for commands with pipx COMMAND –help

  • install Install a package
  • inject Install packages into an existing Virtual Environment
  • upgrade Upgrade a package
  • upgrade-all Upgrade all packages. Runs `pip install -U ` for each package.
  • uninstall Uninstall a package
  • uninstall-all Uninstall all packages
  • reinstall Reinstall a package
  • reinstall-all Reinstall all packages
  • list List installed packages
  • run Download the latest version of a package to a temporary virtual environment, then run an app from it. Also compatible with local `__pypackages__` directory (experimental).
  • runpip Run pip in an existing pipx-managed Virtual Environment
  • ensurepath Ensure directories necessary for pipx operation are in your PATH environment variable.
  • completions Print instructions on enabling shell completions for pipx

Install a package with CLI support

To install any package which has a Command-line Interface, we need to simply use the install command with pipx

Let’s say we want to run the httpie package from anywhere on our system. We can install the package with pipx.

 demonstrated by running the same command in other places

This will install httpie package in its own environment, not a single environment for pipx but a separate virtual environment for each package we install with pipx. So we basically have access to these packages via pipx anywhere on our system.

Let’s install one more package with pipx

Path of packages installed

Now, once we have a couple of packages installed with pipx, we can see the path of PIPX_HOME directory where it has stored all of its packages in its own virtual environments.

As we can see the PIPX_HOME directory is in the ~/.local/pipx/venvs

This folder will store all the virtual environments whose packages have been installed via pipx. Each package has its own virtual environment, so we can run packages in an isolated environment.

Pipx list command

We can even list out all the installed packages with pipx. We can use the list command to display and check which packages and commands we can run with pipx.

Run commands with pipx for packages

Once we have a few packages installed and set up with pipx, we can try running them via pipx. Remember that the packages need to have a CLI to interact and enter commands in the shell/CMD. Pipx will also install those packages whose apps/executables can be run within a CLI environment.

To run a command with the package in pipx, you simply need to parse the run command along with the parameters associated with the particular command or package.

Demonstrated by running the same command at other places:

Uninstall packages with pipx

We can even uninstall the packages in pipx, the command will remove the package from the global context that pipx uses to run the command in that package’s virtual environment.

Источник

Оцените статью