Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to source bashrc/bashprofile from python script [duplicate]

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

like image 528
codyc4321 Avatar asked Jun 19 '26 14:06

codyc4321


1 Answers

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.

like image 143
Mercury00 Avatar answered Jun 22 '26 05:06

Mercury00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!