Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a single line way to run a command in a Python venv?

I have a command that only runs correctly inside a Python virtual environment I've configured (as intended). I know that I can run the command as

$ cmd args

once I've activated the venv. But (due to the constraints of the tool I'm using) I need to activate run (and deactivate?) in one line: something equivalent to running

$ activate_somehow cmd args

outside the command line.

Is there a way to do this?

like image 691
orome Avatar asked Jan 09 '18 18:01

orome


People also ask

How do I run a Python script from VENV?

To use the virtual environment you created to run Python scripts, simply invoke Python from the command line in the context where you activated it. For instance, to run a script, just run python myscript.py .

How do I get out of VENV command line?

You can exit from the virtualenv using exit command, or by pressing Ctrl+d.

How do I run a program in virtual env?

To run it, activate the virtual env first then run the python command. venv activation is a convenience for interactive work. It sets the venv's "bin" (Unix) or "Scripts" (Windows) directory at the start of PATH and also sets the VIRTUAL_ENV environment variable (which py.exe in Windows looks for in some cases).


1 Answers

You can generally run something in a virtual environment simply by using a fully qualified path to the script. For example, if I have:

virtualenv .venv

Then I can install something into that virtual environment without activating it by running:

.venv/bin/pip install foo

This should be true for anything installed using standard Python mechanisms.

like image 116
larsks Avatar answered Sep 19 '22 08:09

larsks