3

I am wondering how to source your bashrc from python. I automate turning scripts into aliases and whatnot. Here is what I see in shell:

In [6]: subprocess.call(['sudo', 'source', '/home/cchilders/.bashrc'])
sudo: source: command not found
Out[6]: 1

In [7]: subprocess.call(['sudo', '.', '/home/cchilders/.bashrc'])
sudo: .: command not found

Thank you

codyc4321
  • 9,014
  • 22
  • 92
  • 165
  • 4
    `source` is a bash builtin, so you'd have to do `bash -c "source ..."`. However, that won't have the effect you're looking for since the shell settings are going to vanish when the bash process ends. You're gonig to need to do this *before* you launch python. – glenn jackman Dec 10 '15 at 23:13
  • what if I call a shell script that just sources from within the python – codyc4321 Dec 10 '15 at 23:16
  • 1
    this wont do what you want .... there is no way to call the rc and have it have any effect within the python shell or script .... what are you actually trying to accomplish? there maybe other ways to do it ... – Joran Beasley Dec 10 '15 at 23:19
  • @codyc4321, remember this: a child process *cannot* alter the environment of its parent process. – glenn jackman Dec 10 '15 at 23:20
  • What exactly do you want to achieve with that? Just running a bunch of shell commands in a subprocess isn't going to affect the state of the Python program (or any parent process for that matter). – David Foerster Dec 10 '15 at 23:34

4 Answers4

1

When you use subprocess.call, you're not using a shell - note from: https://docs.python.org/2/library/subprocess.html#subprocess.call

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

the default for subprocess.call is no environment as you're not in a shell. Source is a bash builtin, so there's no program for subprocess to execute. And likely most of the code in your bashrc would be meaningless to call within the context of subprocess.

What you may want to do is provide more detailed information on what you're trying to accomplish by sourcing the bashrc file that can be done in a more pythonic way.

Mercury00
  • 80
  • 2
  • 10
0

sudo source wouldn't work because source is a shell built-in. I'm not sure of the technical reasons why you can't sudo source, as a sudo echo works just fine, but try using subprocess.call(['.', '/home/cchilders/.bashrc']) or whatever the equivalent would be.

Lando
  • 419
  • 3
  • 8
  • 3
    or `type -a source` versus `type -a echo` -- that's why "sudo echo" works – glenn jackman Dec 10 '15 at 23:19
  • I think you could do `subprocess.call("source /home/user/.bashrc",shell=True)` ... but as mentioned in the comments on OP this will not have the desired effect even if he manages to make it work – Joran Beasley Dec 10 '15 at 23:20
0

Try the following:

import os

os.system("source /home/cchilders/.bashrc")

if you want to invoke any tools or run any other scripts:

os.system("source /home/cchilders/.bashrc && python3 script name")
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
0

You can do this with subprocess.run and a subshell.

Instead of this:

subprocess.call(['sudo', 'source', '/home/cchilders/.bashrc'])

Try this:

subprocess.run("sudo bash -c 'source /home/cchilders/.bashrc'", shell=True)
a505999
  • 370
  • 5
  • 11