venv and executing other python programs

Mirko mirkok.lists at googlemail.com
Tue Feb 15 09:27:16 EST 2022


Am 15.02.2022 um 08:53 schrieb Barry Scott:
> Or are you running the program from the command line after activating the
> venv?

This ...

Am 15.02.2022 um 11:18 schrieb Roel Schroeven:
> Suppose you're working on a program which, for example, prints json
> to stdout. And suppose you want to use a tool like jq (let's call it
> pjq as an example) to process that output, by piping the output of
> your program to it:

... and this.


Am 15.02.2022 um 08:21 schrieb Reto:
> Don't activate the venv for those programs then?
> The point of a venv is that you only enter it when you actually want
> that specific python stack.

Well, it's not that I activate the venv *for* those troubling
programs. I activate it to work on the particular project. ;-)

> Get yourself a terminal that can either multiplex, or add something like
> tmux or screen to the mix if you frequently need other python tools
> during development.

I already do that (terminator, since I don't get tmux to behave as I
want it).


Am 15.02.2022 um 13:36 schrieb Martin Di Paola:
> I did a few experiments in my machine. I created the following foo.py
[SNIP]
> Do you have a particular context where you are having troubles? May
> be there is something else going on...

It seems to me that on your system, venv includes the site-packages
by default. For that I've found a solution: Create the venv with
--system-site-packages or in the venv's pyvenv.cfg just set/add
"include-system-site-packages = true"

Without this, I get the following (Python 3.6.9, Linux Mint 19,
Pandas installed in /usr/lib/python3/dist-packages/pandas):


[2,  0] mirko at wizbox:~$ vim /home/mirko/bin/foo.py
[2,  0] mirko at wizbox:~$ chmod +x /home/mirko/bin/foo.py
[2,  0] mirko at wizbox:~$ cat /home/mirko/bin/foo.py
#!/usr/bin/env python3

import pandas
print("Works")

[2,  0] mirko at wizbox:~$ foo.py
Works

[2,  0] mirko at wizbox:~$ python3 -m venv xxx
[2,  0] mirko at wizbox:~$ source xxx/bin/activate

(xxx) [2,  0] mirko at wizbox:~$ foo.py
Traceback (most recent call last):
  File "/home/mirko/bin/foo.py", line 3, in <module>
    import pandas
ModuleNotFoundError: No module named 'pandas'

(xxx) [2,  1] mirko at wizbox:~$ deactivate
[2,  0] mirko at wizbox:~$ foo.py
Works


So, the problem seems to be solved. Thanks all! :-)


More information about the Python-list mailing list