calling source command within python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jie

    calling source command within python

    Hi all,

    i'm having trouble executing os.system('sour ce .bashrc') command
    within python, it always says that source not found and stuff. Any
    clue?

    Thanks,
    Jie
  • Fredrik Lundh

    #2
    Re: calling source command within python

    Jie wrote:
    i'm having trouble executing os.system('sour ce .bashrc') command
    within python, it always says that source not found and stuff. Any
    clue?
    like in

    $ python
    >>import os
    >>os.system("so urce .bashrc")
    sh: source not found and stuff
    256

    ? I get

    $ python
    >>import os
    >>os.system("so urce .bashrc")
    /... stuff printed by my .bashrc file .../
    0

    What happens if you do
    >>import os
    >>os.system("ba sh .bashrc")
    instead?

    </F>

    Comment

    • mk

      #3
      Re: calling source command within python

      Jie wrote:
      Hi all,
      >
      i'm having trouble executing os.system('sour ce .bashrc') command
      within python, it always says that source not found and stuff. Any
      clue?
      It _might_ be that the shell it fires up is /bin/sh and this in turn is
      not bash.

      Anyway, it's better to use subprocess / Popen for this sort of operation.


      Comment

      • Glenn Hutchings

        #4
        Re: calling source command within python

        Jie <Jie.Bao@gmail. comwrites:
        i'm having trouble executing os.system('sour ce .bashrc') command
        within python, it always says that source not found and stuff. Any
        clue?
        There's no 'source' program; it's a shell builtin. Even if there was, it
        almost certainly wouldn't do what you want. The .bashrc file is supposed
        to contain settings applying to the current shell, and os.system() runs in
        a subshell, so the settings will only affect that shell.

        If you're doing this to set environment variables, try modifying os.environ
        instead.

        Glenn

        Comment

        Working...