[New-bugs-announce] [issue27148] Make VENV_DIR relative to Script directory

Jon Nabozny report at bugs.python.org
Sat May 28 23:57:35 EDT 2016

New submission from Jon Nabozny:

I would like to see some enhancement where the activate.bat and activate scripts allow the script to determine where the python executable resides.

When the executable cannot be found (because the path doesn't exist for some reason), the system will ultimately find the installed version of python. This can cause side effects that may be non-obvious to users.

    User creates a virtual environment:
        python -m venv C:\Location\To\Project venv

     User manually changes location of project:
        xcopy /E C:\Location\To\Project C:\New\Project\Location

     User runs activate.bat:
        (venv) C:\New\Project\Location

     At this point, the user will get the modified prompt, but will         
     actually be using his/her globally installed python. Therefore,
     running anything like pip will taint their install. Further, if
     they had set up the environment previously, it can cause errors
     because modules won't be found (because it's pointing at the
     wrong Lib\site-packages folder).

        ;Replace set "VIRTUAL_ENV=__VENV_DIR__"
        set "VIRTUAL_ENV=%~dp0..\"

        FILE=$(readlink -f "$0")
        FILE_DIR=$(dirname "$FILE")
        # Replace VIRTUAL_ENV="__VENV_DIR__"

Somewhat related, I've also found that's it's incredibly convenient to also include activate and deactivate files at the root of the venv:

     python -m venv C:\project venv

     # Now, C:\project looks like this:

     # activate and deactivate are very simple:
     @ECHO OFF

     pushd env\Scripts
     call activate

     @ECHO OFF

     pushd env\Scripts
     call deactivate

components: Demos and Tools
messages: 266583
nosy: widmo
priority: normal
severity: normal
status: open
title: Make VENV_DIR relative to Script directory
type: enhancement
versions: Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list