venv and executing other python programs

Barry Scott barry at barrys-emacs.org
Tue Feb 15 12:31:55 EST 2022



> On 15 Feb 2022, at 12:36, Martin Di Paola <martinp.dipaola at gmail.com> wrote:
> 
> I did a few experiments in my machine. I created the following foo.py
> 
>  import pandas
>  print("foo")
> 
> Now "pandas" is installed under Python 3 outside the venv. I can run it successfully calling "python3 foo.py".
> 
> If I add the shebang "#!/usr/bin/env python3" (notice the 3), I can also run it as "./foo.py".

> 
> Calling it as "python foo.py" or using the shebang "#!/usr/bin/env python" does not work and it makes sense since "pandas" is installed
> only for python 3.
> 
> New I create a virtual env with "python3 -m venv xxx" and activate it.
> 
> Once inside I can run foo.py in 4 different ways:
> 
> - python foo.py
> - python3 foo.py
> - ./foo.py (using the shebang "#!/usr/bin/env python")
> - ./foo.py (using the shebang "#!/usr/bin/env python3")
> 
> Now all of that was with "pandas" installed outside of venv but not inside.
> 
> I did the same experiments with another library, "cryptonita" which it is not installed outside but inside and I was able to executed in 4 different ways too (inside the venv of course):
> 
> - python foo.py
> - python3 foo.py
> - ./foo.py (using the shebang "#!/usr/bin/env python")
> - ./foo.py (using the shebang "#!/usr/bin/env python3")

If you have activated the venv then any script that uses /usr/bin/env will use executables from the venv
bin folder.

I'm seeing the following in the venv I made on my mac:

 % ls venv.tmp/bin
Activate.ps1        dmgbuild*           modulegraph*        pyi-archive_viewer* pyinstaller*        python3.10@
activate            ds_store*           pip*                pyi-bindepend*      pylupdate5*         pyuic5*
activate.csh        macho_dump*         pip3*               pyi-grab_version*   pyrcc5*             wheel*
activate.fish       macho_find*         pip3.10*            pyi-makespec*       python@
colour-print*       macho_standalone*   py2applet*          pyi-set_version*    python3@

You can see that "python", "python3" and "python3.10" are present.

So it does not help to change the /usr/bin/env to use python3.
Indeed its a feature that if you use /usr/bin/env you obviously want to use the
executable from the activated venv.

I avoid all these issues by not activating the venv. Python has code to know
how to use the venv libraries that are installed in it when invoked. It does not 
depend on the activate script being run.

Barry


> 
> Do you have a particular context where you are having troubles? May be there is something else going on...
> 
> Thanks,
> Martin.
> 
> On Tue, Feb 15, 2022 at 06:35:18AM +0100, Mirko via Python-list wrote:
>> Hi,
>> 
>> 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?
>> 
>> 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/.
>> 
>> Thanks for your time.
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 



More information about the Python-list mailing list