Trivial environment variable question? (OS X Spyder)

1,075 views
Skip to first unread message

dcn

unread,
Nov 29, 2011, 3:22:15 AM11/29/11
to spyder
I have a package which appears to have installed OK under Spyder. To
run, it needs an environment variable set up which I have in
my .profile file as

export XUVTOP=$HOME/Documents/Python/CHIANTI_7.0_data/

If I open the Terminal and type 'env' the variable shows properly.

However, when I open Spyder and select iPython, then type 'env'

the environment variable does not show. I presume this means Spyder's
Python (installed under Macports) is not seeing the .profile file?

Where do I add the information so that Spyder will find it?

DN

(PS I seem to have three Pythons installed, Enthought, Apple and the
Macports install. I have deactivated the Enthought one in the OS
PATH.)

David Verelst

unread,
Nov 29, 2011, 4:02:51 AM11/29/11
to spyd...@googlegroups.com
Hi,

I am not sure how it works on Mac, but to expose a certain path to
Python I add it to my PYTHONPATH environment variable. I am not sure how
your different Python installations deal with the PYTHONPATH variable.

You can also add directories to your Python environment directly from a
script as follows:

from sys import path

path.append('/home/whateveruser/Documents/Python/CHIANTI_7.0_data/')


Hope this helps.

Regards,
David

David Nicholls

unread,
Nov 29, 2011, 4:17:49 AM11/29/11
to spyd...@googlegroups.com
Thanks, David,

However, I need to set a named environment variable rather than just add the path. The Spyder console has an option for adding a new path to the PYTHONPATH, but I need to do more than that. I expect that somewhere tucked away in the bowels of the disk drive there is a small file that I need to edit.

DN

> --
> You received this message because you are subscribed to the Google Groups "spyder" group.
> To post to this group, send email to spyd...@googlegroups.com.
> To unsubscribe from this group, send email to spyderlib+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/spyderlib?hl=en.
>

David Verelst

unread,
Nov 29, 2011, 6:49:57 AM11/29/11
to spyd...@googlegroups.com
I guess I don't understand the context of the question, but I'll give it
one more go :-)
You could manipulate environment variables of your running instance:

import os
os.environ['XUVTOP'] =
'/home/whateveruser/Documents/Python/CHIANTI_7.0_data/'

On the other hand, there has been a similar question on the mailing list
before. There hasn't been any response as of to date:
http://groups.google.com/group/spyderlib/browse_thread/thread/8e1f900a6e26b6c7/5f0e20ccb518d96b

But then again, this might be more a discussion about where one should
set the environment variables properly. In my case (Ubuntu 10.04), any
environment variables set in the optional ~.bash_profile (which is
invoked by ~.bashrc) are known to my shell, and hence anything I launch
from that shell, including Spyder. If I now launch Spyder from a place
which does not update the environment variables as set in ~.bashrc, I
don't have access to those specific environment variables I hoped for.
For instance, again referring to Ubuntu 10.04, if I launch spyder from a
custom made launcher, it seems not to have any of the environment
variables I set myself in the ~.bashrc file. From my end I conclude that
~.bashrc is not the correct place to set system wide environment
variables. Note that any environment variables in my ~.profile file are
only set during the login procedure but are system wide (available for
spyder at all times), so any value I add during a session is not set for
the current session. I am not sure if that's the same for Mac but that
might explain your case?

It also illustrates my lack of knowledge on the matter of environment
variables :-)

Regards,
David

>> To post to this group, send email tospy...@googlegroups.com.
>> To unsubscribe from this group, send email tospyderlib...@googlegroups.com.

Ryan Clary

unread,
Nov 29, 2011, 10:36:31 AM11/29/11
to spyd...@googlegroups.com
I would add the following: first, ~/.bashrc, ~/.bash_profile, ~/.profile, etc. behave identically for Linux installations and Mac OS X; second, if one is launching Spyder from a custom launcher (as I do, using an Automator application), it is necessary to use the command 'source ~/.profile' (without quotes) to make available any environment variables set in ~/.profile. If one is launching Spyder directly from the command line, then the environment variables in the current session (manually specified or obtained from any profile scripts) should be inherited by Spyder.

I hope this clarifies and/or helps.

Ryan Clary

> To post to this group, send email to spyd...@googlegroups.com.
> To unsubscribe from this group, send email to spyderlib+...@googlegroups.com.

David Nicholls

unread,
Nov 29, 2011, 5:42:21 PM11/29/11
to spyd...@googlegroups.com
Thanks, David, that line looks like it should do the job.

DN

> To post to this group, send email to spyd...@googlegroups.com.
> To unsubscribe from this group, send email to spyderlib+...@googlegroups.com.

David Nicholls

unread,
Nov 29, 2011, 5:46:37 PM11/29/11
to spyd...@googlegroups.com
Yes! I do use an automator script to launch Spyder. And as soon as I tried launching it from the command line, the .profile environment appeared. Thanks, Ryan.

DN

David Nicholls

unread,
Nov 29, 2011, 5:57:05 PM11/29/11
to spyd...@googlegroups.com
Ryan,

A quick question, as I'm not at all familiar with Automator scripting:

I'm currently using a single line saved as a .app file using the Apple Script editor that says:

  do shell script "/opt/local/bin/spyder"

This launches Spyder OK.  However, I can't work out how to add the 'source ~/.profile' to the command to get Spyder to read the file.

Putting the line 

  do shell script "/source ~/.profile"

ahead of the Spyder line does not work.

DN

Ryan Clary

unread,
Nov 29, 2011, 6:39:46 PM11/29/11
to spyd...@googlegroups.com
David,
I've attached my Automator application. You should be able to unzip it and open it for editing inside Automator. When you do so, you should see that I used the "Run Shell Script" action, not the "Run AppleScript" action. This allows you to write an entire shell script if you wish, rather than passing commands to Applescript which will in turn pass them to a shell. You will also see that I've included both 'source /etc/profile' and 'source ~/.profile' lines. I do this because I have modifications in both profile files that I want included in the environment - you should comment one or the other per your needs.

One more thing…I've included a Spyder icon with my application so it looks pretty ;)

Spyder.app.zip

David Nicholls

unread,
Nov 29, 2011, 6:56:37 PM11/29/11
to spyd...@googlegroups.com
Thanks, Ryan.

Meantime I had worked out how to use Automator (rather than Script Editor) which allowed me to incorporate what I needed (using the 'Run Shell Script' option). And I too already had a nice Spyder icon, quite like yours but with a different background :-)

I have attached the app.

DN

Spyder.app 2.zip
Reply all
Reply to author
Forward
0 new messages