venv and executing other python programs

Martin Di Paola martinp.dipaola at gmail.com
Tue Feb 15 07:36:53 EST 2022


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

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


More information about the Python-list mailing list