Sublime Forum

ST4 updating os.environ in a plugin is not working

#1

Hi folks,

I’m trying to use the ProjectEnvironment plugin (source code here) on ST4 (4107) on Windows 10. The plugin basically allows you to define environment variables for your Sublime project

It’s not setting the environment variables, so I decided to debug the plugin. In the ProjectEnvironment.py file, I put a line to output the environment variables

print('DEBUG]] ' + str(os.environ))

This works and the Sublime console shows:

  • All of my system variables :+1:
  • The new variable that gets set :+1: In my instance, I’m setting one called CHROMIUM_BUILDTOOLS_PATH

I can see all of this output in the Sublime console. However, if I try to get the value from the console, it’s empty:

>>> os.getenv('CHROMIUM_BUILDTOOLS_PATH')

Trying to get another variable works just fine.

>>> os.getenv('SYSTEMROOT')
'C:\\Windows'

I read about how environment variables are handled in this great post here by @OdatNurd. However, it’s not entirely clear to me why these are not persisting.

My guess (based on description in the post) is that the plugin is being loaded by a process which (after running) is going out of scope (does Sublime just shell out to Python?). It seems that the Sublime console is running under a different process which is why I don’t see this.

Is there a working solution for what I’m trying to do? I swear I had gotten this plugin working last year on ST3 :frowning: (plugin code has not changed)

0 Likes

#2

Sublime Text 4 has two plugin processes, one for Python 3.3 and one for 3.8. The console by default uses the 3.8, but that plugin uses 3.3. If you right click on the console input you can select the 3.3 plugin process and check that it’s working.

0 Likes

#3

Oh wow- that’s definitely it! :smile: That explains why the console can’t see the variable set

Is there a way to force the package to use Python 3.8? I’m trying to find this - guessing it’s one of the files in the plugin itself. I should be able to create a pull request for the project author

edit: found it from the ST4 release notes:

PYTHON 3.8 API

  • Added a Python 3.8 API environment for plugins
  • Plugins can choose Python version via .python-version file in plugin folder
  • Existing plugins are fully supported via legacy Python 3.3 API
  • Many API improvements and additions - see API section for more details

I created a file .python-version in the plugin folder root and simply put 3.8 in it. This works great :smile: Thanks again @bschaaf!

1 Like