This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Make VENV_DIR relative to Script directory
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.6
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: vinay.sajip, widmo
Priority: normal Keywords:

Created on 2016-05-29 03:57 by widmo, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg266583 - (view) Author: Jon Nabozny (widmo) Date: 2016-05-29 03:57
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
msg266742 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2016-05-31 07:24
Thanks for the suggestion, but I don't propose to change the way things work in this area, for the following reasons:

1. pyvenv tries to use the same directory layout/location of scripts as virtualenv does. This allows users who are used to virtualenv to have less to think about when switching to pyvenv.
2. Virtual environments (venvs) aren't meant to be movable. Scripts installed into virtual environments have the venv's absolute path hard-coded into their shebang. This is true for both virtualenv and pyvenv. However, a venv can always be created at another location and have the same stuff installed into it, and the old venv discarded.
3. Making changes like this would not be backwards compatible - they would probably break existing code which relies on a common layout between virtualenv and pyvenv, or the existing relative locations of script files.
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71335
2016-05-31 07:24:18vinay.sajipsetstatus: open -> closed
resolution: wont fix
messages: + msg266742
2016-05-29 16:27:56SilentGhostsetnosy: + vinay.sajip

components: + Library (Lib), - Demos and Tools
versions: - Python 3.2, Python 3.3, Python 3.4, Python 3.5
2016-05-29 03:57:35widmocreate