5

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?

1
  • After reading the first answer I realise the best would be if the python code could activate the necessary environment - then argument handling etc would work like normal... Commented Apr 1, 2017 at 7:51

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Not a bad idea! But it doesn't work: Firstly deactivate is not a script. And secondly, more problematic, csvcut for example has lots of different numbers of arguments it can take...
I edited the answer so it could take even 5 arguments... and fixed the deactivate, my bed...
0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.