2

This is strange to me. I have set a variable in bash and exported it, like this:

export EMAIL="example@example.com"

I have used the command, "source ~/.bash_profile" to reset bash, and have even logged out and back in, and here's the issue:

All is good when I do this in the Python 3 interpreter:

>>> import os 
>>> print(os.environ["EMAIL"])
example@example.com

All is good when I put the above into a script and run it in terminal.

Jeffreys-MacBook-Pro:jeff$ python3 check_var.py 
example@example.com

However, when I run the script in my text editor, Sublime Text 2, I get this:

    raise KeyError(key) from None
KeyError: 'EMAIL'

All my scripts run fine in the text editor but, when I try to grab this variable from the environment running the script in the text editor, it chokes. Any thoughts on how to resolve this issue?

Jeff F
  • 975
  • 4
  • 14
  • 24
  • 1
    I believe you may want to put that into your .bashrc. .bash_profile is only executed in logon shells. If you're launching Sublime from somewhere other than the command prompt that you logged in at, I suspect it may not be populating that environment. – David Hoelzer May 27 '15 at 17:21
  • What shell is sublime using to launch the script? Is it even launching a shell to run the python script? That shell is almost certainly not loading your interactive shell startup scripts (so neither `.bash_profile` nor `.bashrc`) by default. – Etan Reisner May 27 '15 at 17:31
  • Tried putting that into .bashrc. Same error. – Jeff F May 27 '15 at 17:32
  • 1
    You probably aren't starting Sublime Text from a shell, so it doesn't have `EMAIL` in *its* environment to pass on to the shell it starts to execute the script. – chepner May 27 '15 at 17:35
  • As far as I know, I'm not starting Sublime from a shell; just clicking the Sublime icon. I thought using export in bash made the variable available to anything I do in my login. No? – Jeff F May 27 '15 at 17:41
  • 1
    http://stackoverflow.com/questions/28940230/sublime-does-not-see-env-variables – Eric Appelt May 27 '15 at 17:43

1 Answers1

0

Turns out this is a Mac specific issue. Or, at least what works for me now is a Mac specific answer. Here's what I did to make this all work:

First run this in terminal (replacing "variablename" and "value" with your own whatever:

launchctl setenv variablename value

This sets a variable so that it is picked up by all applications (graphical applications started via the Dock or Spotlight, in addition to those started via the terminal).

The above goes away when you log out or reboot. If you want to have the variables available every time you login, do this:

1) Launch AppleScript Editor and enter a command like this:

do shell script "launchctl setenv variablename value"

(Use multiple lines if you want to set multiple variables)

2) Save under whatever name you want but it must be saved as a file format type: Application.

3) Open System Settings → Users & Groups → Login Items and add your new application. This way the the script will be invoked when you log in.

Now, doing this will not make your variables available in bash. You still need to add them to the .bash_profile file if you want the variables available to the shell/terminal.

NOTE: I took much/most of this from an answer to another similar question and if you want to see that, go here:

Setting environment variables via launchd.conf no longer works in OS X Yosemite/El Capitan/macOS Sierra?

Community
  • 1
  • 1
Jeff F
  • 975
  • 4
  • 14
  • 24