Your best choice is to implement your logic as pure Python logic, as described in the first part of the answer by @tripleee. Your second best choice is to keep the external tools, but eliminate the need for a shell in invoking them and connecting them together.
See the Python documentation section Replacing Shell Pipelines.
import sys
from subprocess import Popen, PIPE
p1 = Popen(['awk', '/regex/ {print}'], stdin=open(sys.argv[1]), stdout=PIPE)
p2 = Popen(['sed', 's/old/new/g'], stdin=p1.stdout, stdout=PIPE)
x = p2.communicate()[0]
Your third best choice is to keep the shell, but pass the data out-of-band from the code:
p = subprocess.run([
"""awk '/regex/ {print}' <"$1" | sed 's/old/new/'""", # code to run
'_', # $0 in context of that code
sys.argv[1] # $1 in context of that code
], shell=True, check=True, stdout=subprocess.PIPE)
print(p.stdout)
python somefile.py 'some commands here'cmd=f"awk '/regex/ {{print}}' %s | sed 's/old/new/g'" % (sys.argv[1])sys.argv[1]contains$(rm -rf ~)? Or even$(rm -rf ~)'$(rm -rf ~)', so you can't escape it with literal single quotes?