Virtualenvs in Sublime Text 4

Question:

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?

Answers:

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:

  • Create sublime project or add existing directory to sublime project
  • Goto Project -> Edit Project
  • Add this and save in root of you 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": "."
        },
    ],

}
  • If you don’t want to store .sublime-project files in project’s root dir, specify full path to project’s root dir in: "path": "/path/to/proj"
  • Still, you need to chose build system before running scripts. In this case it’ll be <proj-name>
  • You can generate this config programmatically.

More options for Project config here.

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

Answered By: 9rik

There is a build system variable $folder which gives the path to the top level folder open in the sidebar. If you specify a working directory in the project file this will be the project folder.

Add the path to your project file:

{
     "folders":
    [
        {
            "name": "ProjectName",
            "path": "/path/to/your/project"
        }
    ]

Then create a file Py3Venv.sublime-build:

{
    "cmd": ["$folder/.venv/bin/python", "-u", "$file"],
    "file_regex": "^[ ]*File "(...*?)", line ([0-9]*)",
    "selector": "source.python",
    "encoding": "utf8",
}

This file needs to be placed in User subfolder of the Sublime packages directory. Easiest way to find is "Preferences > Browse Packages" (macOS, ymmv).

ProjectManager plugin stores project files in SublimeText settings folder, rather than the project folder. Worth checking if you don’t already use.

Answered By: Paul Jacobson