问题
Sublime Text 3 on OSX.
I am writing a plugin that loads up a particularly poorly-written 3rd-party Python module that relies on the value of an Environment Variable to function properly (in particular, DYLD_LIBRARY_PATH
).
Of course, I could set this globally, but I would love for the Sublime Plugin to be self-contained. I notice that plugins run in a child process of Sublime itself - is there any way to tell Sublime to provide the plugin_host
process with a particular Environment Variable before it spins it off?
If not, does anyone know of another way to solve this problem? For performance and simplicity reasons, I would greatly prefer to have the python script be self-contained, rather than calling out to an external script that utilizes the library. Thank you.
回答1:
Check out os.putenv() and os.environ for details on setting environment variables from Python. So, for your case, before importing your 3rd-party module, use something like this:
import os
os.environ['DYLD_LIBRARY_PATH'] = '/usr/lib/foo:' + os.environ.get('DYLD_LIBRARY_PATH')
import third_party
# and so on...
来源:https://stackoverflow.com/questions/21565189/sublime-text-3-set-environment-variable-for-plugin-host