halloleo
halloleo

Reputation: 10454

How to pip-install Python package into virtual env and have CLI commands accessible in normal shell

For larger Python packages which might interfere with other packages it is recommended to install them into their own virtual environment and some Python packages expose CLI commands to the shell.

Is there a way to pip-install such a package into its own virtual environment, but have the CLI commands accessible from a normal shell without switching beforehand manually to this virtual environment?

Here an example: When I install csvkit via

pip install csvkit

I have the commands csvcut, csvlook, csvgrep and others available in my shell. However if I do not want to install cvskit in my System-Python and install it in a virtual environment, say at ~/venvs/csvkit, I have csvkit only available if I have manually activated the environment csvkit.

Is there a way to create the virtual environment and install csvkit in it, so that the commands like csvcut activate the environment themselves before they run?

Upvotes: 5

Views: 619

Answers (3)

halloleo
halloleo

Reputation: 10454

A newer tool which still is very well maintained is pipx - Install and Run Python Applications in Isolated Environments. It works similar to pipsi:

  1. First install pipx. (See pipx installation)
  2. Then issue:

    pipx install csvkit
    
  3. Finally make sure pipx's bin directory (normally ~/.local/bin) is in your PATH.

Please note, pipx has additional commands for maintaining and inspecting of the generated venvs - see pipx --help.

Upvotes: 4

halloleo
halloleo

Reputation: 10454

Use pipsi. Here a description from the project's ReadMe:

pipsi installs each package into ~/.local/venvs/PKGNAME and then symlinks all new scripts into ~/.local/bin (these can be changed by PIPSI_HOME and PIPSI_BIN_DIR env variables respectively).

Compared to pip install --user each PKGNAME is installed into its own virtualenv, so you don't have to worry about different PKGNAMEs having conflicting dependencies.

It works a treat for csvkit:

  1. First install pipsi.

  2. Then issue:

     pipsi install csvkit
    
  3. Finally make sure pipsi's bin directory (normally ~/.local/bin) is in your PATH.

That's it! Now you can type on the comamnd line e.g.

csvcut --help

which calls csvcut in its own virtualenv.

There's is no need to manually activate an virtualenv and your system Python does not get polluted by additional packages (apart from the pipsi package once and for all).

Upvotes: 0

Yuval Pruss
Yuval Pruss

Reputation: 9846

You can create an aliases, such as csvcut and point them to source ~/venvs/csvkit/bin/activate && csvcut && source deactivate

If this programs accept parameters, you can use functions and defined the in the .bashrc file:

csvcut() {
    #do things with parameters like $1 such as
    source ~/venvs/csvkit/bin/activate
    csvcut $1 $2 $3 $4 $5
    deactivate
}

To call the function, just use the csvcut <your_parameter> command.

Upvotes: 0

Related Questions