Sublime Forum

Environment Variables

#1

I’m having difficulty with ST3 environment variable management

When I make a modification to, e.g. $PYTHONPATH in my .bashrc, this change is not noticed by ST3.

It requires a restart and opening from terminal via subl for the environment variables to be visible to the python interpreter(!) This is quite frustrating behavior.

Although I have "shell_environment": true, this is apparently only available for macos.

Please help me find a reasonable work around.

0 Likes

#2

ST reads environment variables from system upon startup and caches them internally as determining the login shell, start it and read its env is an expensive operation on some OSs (if not all). The same thing many desktop environments do, too, for the same reason. Therefore you need to start ST from the terminal to see changes in realtime.

In case you want to modify the environment of an external python interpreter which runs your scripts, you can create a custom python built script with some extra environment variables to be passed to it. You still won’t be able to place them into the .bashrc but directly into the *.sublime-build

To learn how to create/modify a build script you could refere to https://www.sublimetext.com/docs/3/build_systems.html

An example can be found here:

0 Likes

#3

Here’s a viable solution
Using PackageControl, or manually, install the ProjectEnvironment package.

using settings like the following, make sure your environment file, env_file, is set for your operating system. Also make sure you’ve set your PATH variable.

// Settings in here override those in "/ProjectEnvironment/ProjectEnvironment.sublime-settings",
{
    // If true some debugging information will be preinted on console
    "print_output": false,
    // If true some variables available in Sublime's API, will be exposed as
    // standard environment variables.
    // these are:
    // "project_path", "project", "project_name", "project_base_name", "packages"
    "set_sublime_variables": false,
    // It may be useful to add a prefix to those variables so that they don't confict with yours
    "sublime_variables_prefix": "",
    // those variables can be all capitalised if you wish. "project" will become "PROJECT"
    "sublime_variables_capitalized": false,
    // files with this extensions will be run through source_vars(.bat|.sh)
    // DON'T FORGET THE DOT!
    "command_line_to_wrap_extensions": [
        ".bat",
        ".sh"
    ],
    // This is only for windows, since Mac and Linux can use shebang:
    "execute_ext_with": {
        ".py": "python",
        ".zsh": "zsh",
        ".sh": "zsh",
        ".bash": "bash"
    },
    "settings": {
        "project_environment": {
            "env": {},
            "env_file": "",
            "windows": {},
            "osx": {
                "PATH": "$PATH:/usr/local/bin",
                "env_file": "~/.zshenv"
            },
            "linux": {}
        }
    }
}

Can’t say I understand why, but I couldn’t get it to work without setting both

0 Likes

Why doesn't Sublime3 use global PATH variable? And how can I force it to use it?