1

I am aware that many similar questions have been posted here but none of them seems to work in my case. I have a few commands in my bash profile like below

export HEADAS=/Users/heasoft/x86_64-apple-darwin18.7.0
alias heainit=". $HEADAS/headas-init.sh"
. $HEADAS/headas-init.sh

export SAS_DIR=/Users/sas-Darwin-16.7.0-64/xmmsas
alias sas=". $SAS_DIR/setsas.sh"

sit='source ~/.bash_profile'

in which I created an alias to run them consecutively: alias prep1='sit; heainit; sas. This works just fine when I execute it in the command line. But I want to insert in a python script and run it from there. I am running Python (v 3.7.4). So, as suggested in here, I tried

import subprocess

command = "prep1"
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True)
output = process.communicate()   
print(output[0].decode())

But I get an error saying command not found. I tried to export it in bash profile but got an error stating -bash: export: prep1: not a function

I also tried the method suggested in here, but still nothing. Related to this, I couldn't even run a shell command like below in python

epatplot set=evli.FTZ plotfile="pn_filtered_pat.ps" 2>&1 | tee pn_filtered_pat.txt

Here is my Python script attempt

command = "epatplot set=evli.FTZ plotfile="pn_filtered_pat.ps" 2>&1 | tee pn_filtered_pat.txt"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()

I get SyntaxError: invalid syntax. I know where this syntax error is rising from but don't know how to fix.

I am a beginner in python so I appreciate any help/guidance.

1
  • You can create a bash child processes, which reads your alias-commands, but what are you then going to do with this? You can not evern execute them in the very same subprocess (unless you explicitly turn on alias processing for non-interactive shells), and of course their definition is gone in every other subprocess you are creating later. Commented Jun 29, 2020 at 10:54

1 Answer 1

3

Please see this answer: https://askubuntu.com/a/98791/1100014

The recommendation is to convert your aliases to bash functions and then export them with -f to be available in subshells.

When you call Popen, execute "bash -c <functionname>".

As for your last script attempt, you have a conflict in quotation marks. Replace the outer quotes with single quotes like this:

command = 'epatplot set=evli.FTZ plotfile="pn_filtered_pat.ps" 2>&1 | tee pn_filtered_pat.txt'
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
Sign up to request clarification or add additional context in comments.

1 Comment

in macOS (Ventura 13.2) bash functions defined without the "function" keyword would cause error: syntax error near unexpected token `('. A definition with the keyword (cf. linuxize.com/post/bash-functions) would execute just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.