Sunday, September 4, 2022

[SOLVED] Virtualenvs in Sublime Text 4

Issue

I have the different virtualenv's and to be able to specify which virtualenv to use with each project I used package Virtualenv (rel="nofollow noreferrer">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:

  • 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
Answer Checked By - Robin (WPSolving Admin)