I'm trying to set the environment variable of my .bashrc
using Spyder; in other words I'm looking for a python command that reads my .bashrc
. Any idea?
-
1`.bashrc` is just a file, you can read/write to it using the standard calls... – tzaman Dec 18 '14 at 18:50
-
2Do you mean you are trying to add a line to bashrc, set an environment variable in the parent process's environment, or set an environment variable in your own process's environment? – kdopen Dec 18 '14 at 18:52
-
2If you want to read the environment variables, simply use `os.environ`. – jme Dec 18 '14 at 18:53
-
Possible duplicate of http://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python?rq=1? – kdopen Dec 18 '14 at 18:53
2 Answers
.bashrc
should automatically be loaded into the environ on login
import os
print os.environ
if you wanted to create a dictionary of values from a bash source file you could in theory do something like
output = subprocess.check_output("source /path/to/.bashrc;env")
env = dict(line.split("=") for line in output.splitlines() if "=" in line))
print env

- 110,522
- 12
- 160
- 179
-
2Actually no because when I do `print os.environ['PATH']` into Spyder it returns. `/Applications/Spyder.app/Contents/Resources:/usr/bin:/bin:/usr/sbin:/sbin:` which is not what I defined on my `.bashrc` (I'm on mac just to be clear). The problem is Spyder itself I suppose – ChrisB Dec 19 '14 at 08:45
-
yes that is because spyder is probably setting the path for you ... Im guessing you can tell spyder to use your .bashrc somehow – Joran Beasley Dec 19 '14 at 16:18
-
see https://groups.google.com/forum/#!msg/spyderlib/jh-QCm4mtsc/mOCa0JxvfOsJ and look at davids answer ... that took me a whole 2 minutes of googling ... but it lookslike spider wants you to use a .profile file instead of .bashrc for whatever reason – Joran Beasley Feb 11 '15 at 17:20
-
1Thanks for that Joran. It actually works know for the _PATH_ variable for example, however for the _PYTHON_PATH_ when doing `os.environ` in the spyder console I get _'PYTHONPATH': '/Applications/Spyder-Py2.app/Contents/Resources'_. When doing that in my bash terminal `echo $PYTHONPATH` I get _/Applications/Spyder-Py2.app/Contents/Resources:/Applications/Trelis-15.0.app/Contents/MacOS_ which is what I want. So why the PYTHONPATH in Spyder is not overwritten by my .bashrc PYTHONPATH? – ChrisB Feb 11 '15 at 18:23
-
do you set the python_path variable in your .profile ? I assume spyder __needs__ access to that resources directory so it alters the python path to append that path ... not replace it – Joran Beasley Feb 11 '15 at 18:24
-
furthermore if you are having this much trouble with your ide ... it begs the question is it the best IDE for you to be using? – Joran Beasley Feb 11 '15 at 18:25
-
Well I really like the matlab like features of Spyder so I would like to use it – ChrisB Feb 11 '15 at 18:28
-
in my .bash_profile I have the following `export PYTHONPATH=$PYTHONPATH:/Applications/Trelis-15.0.app/Contents/MacOS` – ChrisB Feb 11 '15 at 18:35
-
wait ... that talks about a `.profile` I am pretty sure it would have said `.bash_profile` if thats what they meant ...I could be wrong however – Joran Beasley Feb 11 '15 at 18:36
-
well on mac it's a bash_profile, in my .bash_profile I launch my .bashrc. Then in Spyder all the environment variables seem to be the one I set in my .bashrc, except the _PYTHONPATH_ – ChrisB Feb 11 '15 at 18:52
-
try making a `.profile` file instead and delete your `.bash_profile` ... if spyder is actually *overwriting* your python path that is surely a bug and should be sent to the people who make spyder – Joran Beasley Feb 11 '15 at 18:55
-
Sorry for the misunderstood, it's not overwriting everyhting apparently, it appends properly for example the _PATH_ variable but it seems that it's overwriting the _PYTHONPATH_. Anyway I'll let the Spyder group know about that. – ChrisB Feb 11 '15 at 19:03
The shell's startup file is the shell's startup file. You really want to decouple things so that Python doesn't have to understand Bash syntax, and so that settings you want to use from Python are not hidden inside a different utility's monolithic startup file.
One way to solve this is to put your environment variables in a separate file, and source
that file from your .bashrc
. Then when you invoke a shell from Python, that code can source
the same file if it needs to.
# .bashrc
source $HOME/lib/settings.sh
# Python >=3.5+ code
import subprocess
subprocess.run(
'source $HOME/lib/settings.sh; exec the command you actually want to run',
# Need a shell; need the shell to be Bash
shell=True, executable='/bin/bash',
# basic hygiene
check=True, universal_newlines=True)
(If you need to be compatible with older Python versions, try subprocess.check_call()
or even subprocess.call()
if you want to give up the safeguards by the check_
variant in favor of being compatible all the way back to Python 2.4.)

- 175,061
- 34
- 275
- 318
-
I get: FileNotFoundError: [Errno 2] No such file or directory: 'source ~/.bashrc': 'source ~/.bashrc' – Clément Moissard Sep 25 '20 at 07:01