venv and executing other python programs

Peter J. Holzer hjp-python at hjp.at
Tue Feb 15 17:30:18 EST 2022


On 2022-02-15 06:35:18 +0100, Mirko via Python-list wrote:
> I have recently started using venv for my hobby-programming. There
> is an annoying problem. Since venv modifies $PATH, python programs
> that use the "#!/usr/bin/env python" variant of the hashbang often
> fail since their additional modules aren't install inside in venv.
> 
> How to people here deal with that?

Use the hashbang line to couple your scripts to their venvs.

For example, here's the start of one of my scripts:

#! /usr/local/usradm/venv/bin/python3

import json
import os
...
import psycopg2
import psycopg2.extras
import cx_Oracle
...

When this script is run, the python interpreter will inspect it's own
pathname and pull in the venv from /usr/local/usradm/venv automatically,
regardless of the environment it is started in. I could run this from
cron (which has only /usr/bin and /bin in its PATH) or from my normal
command line or from a different venv.

You could use a different venv for every script, but that's pretty
wasteful, so you probably want to organize your venvs so that several
scripts can share their environment. For example by project or product
(as in my example - "usradm" is a our in-house user administration
system) or by capabilities (for example you might want one venv with
pandas and all your favourite graph libraries for all your analytics
scripts and another with Qt5 for some gui tools).


> Please note: I'm not interested in discussing whether the
> env-variant is good or bad. ;-) It's not that *I* use it, but
> several progs in /usr/bin/.

Progs in /usr/bin should IMHO never use /usr/bin/env. They should always
use #!/usr/bin/python3. You might want to report that as a bug to your
distribution.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20220215/c766589f/attachment.sig>


More information about the Python-list mailing list