[Tutor] Python project organisation

Cameron Simpson cs at cskk.id.au
Sat Apr 22 22:49:00 EDT 2023


On 23Apr2023 11:30, Phil <phillor9 at gmail.com> wrote:
>I knew about activate but I didn't know that the addition to the 
>PYTHONPATH is temporary. I suppose it's only a matter of activating a 
>new projects environment to deactivate the previous one?

One thing often glossed over with venvs is that the "python" executable 
inside the venv/bin directory actually sets its search path correctly 
when you invoke it - you don't need to set your shell's $PYTHONPATH at 
all. So:

     ./venv/bin/python3 -m my.module ...

would run the python3 from the venv, using the local my.module in the 
project.

When I work with a venv, which I do routinely, I don't "activate" it.  
Instead I have a little "dev" script/alias which prepends the venv/bin 
to the front of $PATH (and whatever other local "dev mode" envar changes 
i might have), then executes my command. So:

     which python3

will find the normal, nondev python3. But:

     dev which python3

will find the venv python3, which takes care of the search path when you 
invoke it.

Part of this is because my own code is intwined in my normal operations.  
So if I run a command, I normally want the "installed" version, _not_ 
the (possibly broken) version in the project folder I'm standing in.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list