Windows, Python, VENV, and Sublime Text Build systems: Path Error When Activating VENV

Issue

Okay, I’ve looked through a bunch of python venv with sublime text questions, and I’m pretty sure this issue is due to the specifics of venv on windows.

I’m on Windows, using Python 3.10, and the virtualenv package to manage my virtual environments.

I’m trying to set up a custom build in a Sublime project file that does the following:

  • activate the venv for the local project
  • echos the VIRTUAL_ENV to the shell to show the correct venv has been activated

I’ve been having issues with getting both into one build command, so here’s the current array with the two steps as seperate builds:

    "build_systems":
    [
        {
            "name": "activate",
            "cmd": "$project_path/venv/Scripts/activate.bat",
            "shell": true
        },
        {
            "name": "Current VENV?",
            "cmd": "echo %VIRTUAL_ENV%",
            "shell": true
        },
    ]

Currently, when I run the activate build, I get the following:

The system cannot find the path specified.
[Finished in 80ms with exit code 1]
[cmd: C:\Users\kreeh\Repos\project/venv/Scripts/activate.bat]
[dir: C:\Users\kreeh\Repos\project]

If I run C:\Users\kreeh\Repos\project/venv/Scripts/activate.bat in a separate cmd.exe window, it correctly activates the venv, and when I print the %VIRTUAL_ENV% var, it correctly returns the venv location.

C:\Users\kreeh>C:\Users\kreeh\Repos\project/venv/Scripts/activate.bat

(venv) C:\Users\kreeh>echo %VIRTUAL_ENV%
C:\Users\kreeh\Repos\project\venv

I assume this is an issue with how the Sublime Text build system is handling the windows path formatting. If I try and utilize the working directory param, it doesn’t work, because the cmd.exe doesn’t do relative paths nicely.

        {
            "name": "activate",
            "cmd": "venv/Scripts/activate.bat",
            "working_dir": "${project_path}",
            "shell": true
        },

returns

‘venv’ is not recognized as an internal or external command,
operable program or batch file.
[Finished in 60ms]

So, is there a way to have the Sublime Build system handle the windows path correctly, or is there a way to use a relative path in the cmd?

Solution

@MattDMo pointed out the flaw in my logic, which is that the venv activation doesn’t actually do anything in and of itself — it changes the python interpreter used for future commands in that prompt window. As such, it’s kind of a useless thing for a build system. Instead, I added a venv-specific pip install and build commands so far.

{
    "build_systems":
    [
        {
            "name": "pip install",
            "cmd": "${project_path}/venv/Scripts/pip3.exe install -r requirements.txt ",
            "working_dir": "${project_path}",
            "shell": true
        },
        {
            "name": "VENV Build",
            "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
            "selector": "source.python",
            "cmd": "${project_path}/venv/Scripts/python.exe $file",
            "shell": true,
        },

    ],
    "folders":
    [
        {
            "follow_symlinks": true,
            "path": "."
        }
    ],

One thing to note, as a Windows user, I had to add the follow_symlinks=true to the folders list — this is because the way venv works on windows (or at least on MY install), there’s not an actual python.exe in that venv folder, it’s a symlink.

Answered By – klreeher

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published