3

I'd like to run this script at startup on a sublime text session:

import os
import sys
import re
import subprocess
from pprint import pprint


def check_vs_env():
    try:
        p = subprocess.Popen("cl",
                             stdout=subprocess.PIPE,
                             shell=False)
        stdout, stderr = p.communicate()
        print(stdout)
    except:
        print("Visual studio compiler not found!")

print('-' * 80)
check_vs_env()
print('-' * 80)

print("VS environment setup")
p = subprocess.Popen("vcvarsall.bat amd64 && set",
                     cwd=r"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC",
                     stdout=subprocess.PIPE,
                     shell=True)
stdout, stderr = p.communicate()
print("Finished VS environment setup [OK]")
print('-' * 80)

print("ST environment setup")
for varname, varvalue in re.findall(r'^(.*?)=(.*)', stdout.decode("utf-8"), re.MULTILINE):
    os.environ[varname] = varvalue
print("ST environment setup [OK]")

print('-' * 80)
check_vs_env()
print('-' * 80)

Output:

--------------------------------------------------------------------------------
Visual studio compiler not found!
--------------------------------------------------------------------------------
VS environment setup
Finished VS environment setup [OK]
--------------------------------------------------------------------------------
ST environment setup
ST environment setup [OK]
--------------------------------------------------------------------------------
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23918 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

b'usage: cl [ option... ] filename... [ /link linkoption... ]\r\n'
--------------------------------------------------------------------------------

So far so good, but once I check the os.environ in the SublimeText3 console it seems the new content is not persisted.

Any clue how to go about this?

ADDITIONAL NOTES

The communication from my script to SublimeText is not done properly.

On the other hand, modifying os.environ from SublimeText console and then read it from my script works ok.

BPL
  • 9,632
  • 9
  • 59
  • 117
  • 1
    Environment variables set in a script will not persist after the script executes: http://stackoverflow.com/a/716046/3642398. You might find this plugin helpful: https://packagecontrol.io/packages/Environment%20Settings – elethan Oct 06 '16 at 16:47
  • @elethan Not really, that package will help in a per-project basis, I want to run sublime text with the vs environment as exposed in this other [thread](http://stackoverflow.com/questions/39881091/how-to-run-sublimetext-with-visual-studio-environment-enabled/) – BPL Oct 06 '16 at 17:16
  • What exactly is your end goal? Do you want to run build systems from within Sublime that utilize env vars set by `vcvarsall.bat`? This question and your last suffer from [the XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - you need to define what you ultimately want to do, as there very well may be another way besides what you're trying right now. – MattDMo Oct 06 '16 at 23:58
  • @MattDMo You're absolutely right! My goal is quite simple actually, I'd like been able to have hundred of swig sublime-project files within the same sublime text workspace and being able to build & test those wrappers effortlessly inside sublime text without switching context (similar approach to VS). Said otherwise, I want to improve my workflow to spend more time coding and less time doing unnecesary stuff like opening terminals, setting env vars, typing commands, I just want to use mouse and shortcuts like in VS to improve my coding sessions, that's pretty much. Hope it makes sense – BPL Oct 07 '16 at 00:04
  • The question [How to run Sublime Text with visual studio environment enabled](https://stackoverflow.com/questions/39881091/how-to-run-sublimetext-with-visual-studio-environment-enabled/) (also by BPL) has an answer to this question. – OdatNurd Jan 30 '18 at 06:19

0 Answers0