pythonvirtualenvsublimetext3sublimetext4

Virtualenvs in Sublime Text 4


I have the different virtualenv's and to be able to specify which virtualenv to use with each project I used package Virtualenv (https://packagecontrol.io/packages/Virtualenv) in Sublime Text 3. That was so to the day when through the update I became the owner of the Sublime Text 4. The package Virtualenv stopped working (it can be use only with Sublime Text 3).

I manage now the virtualenv's in Sublime Text 4 in the other way. I use the virtualenv through Tools/Build System. I run my script directly without leaving Sublime like with package Virtualenv. To build the new Build System for the existing virtualenv I first run Tools/Build System/New Build System and in the newly opened file I paste:

{
    "shell_cmd": "C://python_projects/my_project/env/Scripts/python -u \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

after this I save it under the name of the project (here my_project_python) in the dir User. It create a new file named my_project_python.sublime-build (composed of the build system name followed by .sublime_build). After doing this, I go to Tools -> Build System and I can see a new option my_project_python. The different virtualenv's only differ in the path C://python_projects/my_project/env/Scripts/python and names of the file .sublime-build.

That's nice but when we have a big number of folders with different virtualenv's this can be a bit frustrating to have so many names in Tools -> Build System. I think that the package Virtualenv manage the virtualenv's with more grace.

So I would like to ask the community if there is another package like Virtualenv that can work in Sublime Text 4? Maybe someone has some way to run the package Virtualenv in Sublime Text 4?


Solution

  • There is a package Virtualenv for ST3, you can downgrade. Currently it doesn't work with ST4 (issue). You can specify build system per project:

    {
        "build_systems":
        [
            {   
                "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",  # highlight line on error
                "name": "<proj-name>",
                "selector": "source.python",
                "shell_cmd": "/path/to/virtualenv/.venv/bin/python -u \"$file\""
            },
        ],
        "folders":
        [
            {
                "path": "."
            },
        ],
    
    }
    

    More options for Project config here.

    If you find something flexible for venvs, hit me up)