How does os.system differ from command line?

Viewed 4288

Why does os.system('command') from my python interpreter not have the same output as command from the terminal?

Question explained quickly :

I have

echo $CONFPATH
/home/claramart/Datamart/Parameter

but

os.system('echo $CONFPATH')

0

Why is that?

Details : I want to get my environment $CONFPATH. I'm using python3.5 and ubuntu16.04.2.

I can do this from command line :

echo $CONFPATH
/home/claramart/Datamart/Parameter

This is the answer I want.

Executing it as a python command from command line also works :

python3 -c 'import os; print(os.environ["CONFPATH"])'
/home/claramart/Datamart/Parameter

The thing is, I want to execute this from my python interpreter and not from command line. Executing it from my python interpreter does not work (I am using Pyzo4.4.1) :

print(os.environ["CONFPATH"])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python3.5/os.py", line 725, in __getitem__
    raise KeyError(key) from None
KeyError: 'CONFPATH'

I suppose this is strictly coming from my interpreter and not python itself as the python execution from the command line worked. Moreover, I can get $PYTHONPATH from my python interpreter so I guess it simply does not detect all environment variables.

To avoid this and as executing it from command line worked, I wanted to do this as a command line execution from my python interpreter, but none of my 2 command line executions work the way I want to :

os.system('echo $CONFPATH')

0

and :

os.system("""python3 -c 'import os; print(os.environ["CONFPATH"]'""")
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.5/os.py", line 725, in __getitem__
    raise KeyError(key) from None
KeyError: 'CONFPATH'
256

Once again and in both cases, it does work for $PYTHONPATH, so I suppose it must go through my interpreter at some point because my problem is specific to that variable $CONFPATH.

Why does os.system('command') from my python interpreter not have the same output as command from the terminal?

2 Answers
Related