1

I am using the Spyder Anaconda IDE for Python. I am writing a code in the Spyder IDE that requires few environment variables to be set ($CPATH, $LIBRARY_PATH and $LD_LIBRARY_PATH) for the Theano library.

I am starting Spyder using the command

sudo ./spyder 

and it starts fine. Even though I set the environment variables in my

/root/.bashrc

file, the code still fails to accept the path and if I try printing

print os.environ["LD_LIBRARY_PATH"]

it raises a KeyError.

I tried all the above with a normal user but still it fails. How can I get Spyder IDE to be able to view files in the above paths and where can I set them inside Spyder?

London guy
  • 24,942
  • 40
  • 110
  • 169
  • 1
    Try sudo env. This will separate problems with spyder with problems with the shell not running .bashrc – Robert Jacobs Jul 01 '15 at 13:07
  • 1
    `.bashrc` is only read for interactive shells – mata Jul 01 '15 at 13:09
  • 1
    try to declare variables directly in your code: `os.environ["LD_LIBRARY_PATH"]="exact/path/to/folder"` – Andersson Jul 01 '15 at 13:09
  • @Andersson I even did setting using os.environ. It doesn't work :(. – London guy Jul 01 '15 at 13:11
  • @RobertJacobs I did a sudo env and it prints lots of environment variables. I do not see my variables in the list. Is that a concern? – London guy Jul 01 '15 at 13:11
  • 1
    Can you add the env variables before sudo ? Or run a script that sets them before calling spyder. Or run sudo -i ,then run spyder. – Robert Jacobs Jul 01 '15 at 13:19
  • Thanks everyone. It finally works only when I do "sudo LD_LIBRARY_PATH=/usr/lib/ LIBRARY_PATH=/usr/lib CPATH=/usr/lib python ". Which means only if I set it in the command line before running the file it runs fine. Setting it in /root/.bashrc doesn't help at all. – London guy Jul 01 '15 at 13:30
  • 3
    This is maybe a silly question, but why do you need to run your editor as root? In particular, preserving `LD_LIBRARY_PATH` as an environment variable probably has more serious security implications than you might think, and I wonder if you need it at all? – SingleNegationElimination Jul 01 '15 at 13:37

1 Answers1

1

You need to tell the sudoers file which Environmental Variables to keep when using the sudo command.

To edit the sudoers file run.

sudo visudo

Then add the following line to the end of it.

Defaults env_keep = "LD_LIBRARY_PATH CPATH LIBRARY_PATH"

Then export your variable.

export LD_LIBRARY_PATH="/path/to/library"

Now you should be able to run it.

More info can be found here How to keep Environment Variables when Using SUDO

Community
  • 1
  • 1
Dobz
  • 1,173
  • 1
  • 12
  • 34
  • I am trying this now. – London guy Jul 01 '15 at 13:31
  • Can you help me append more environment variables to keep? Should I just leave a space within the quotes and add more ?I have 3 of them – London guy Jul 01 '15 at 13:36
  • I am trying this now. – London guy Jul 01 '15 at 13:39
  • This is generally a **very bad** advice. Exactly those envirnoment vairables are not preseved by sudo because keeping them is a **big security risk**. The avdice should be to create a wrapper script or not executing an IDE as root in the first place, _not_ allowing sudo to preserve insecure environment variables. – mata Jul 01 '15 at 13:53