1

I try to get some dependency stuff on pacman automated via python. What I want to issue in the shell (and get output from) is:

pacman -Si [a_package] | grep Conflicts | awk -F: '{print $2}'

So this is my python code:

getconflicts1 = "pacman -Si"
getconflicts2 = "| grep Conflicts | awk -F: '{print $2}'"
pkgs = # an example package list
for x in range (1,2):
    getconflicts = getconflicts1 + pkgs[x] + getconflicts2
    process2 = subprocess.Popen(getconflicts.split(), stdout=subprocess.PIPE)
    conflicts = process2.communicate()[0]
    print(conflicts)

However pacman says "invalid option -- F" when I use the python program, whereas issueing the command in the shell directly works.

This:

print(getconflicts.split())

gives me:

['pacman', '-Si', 'frameworkintegration', '|', 'grep', 'Conflicts', '|', 'awk', '-F:', "'{print", "$2}'"]

I'm kinda new to python and subprocesses in python even more, so do you have a hint on how to do that correctly?

ljrk
  • 751
  • 1
  • 5
  • 21
  • related: [How do I use subprocess.Popen to connect multiple processes by pipes?](http://stackoverflow.com/q/295459/4279) – jfs Mar 29 '15 at 21:52
  • don't put solution into the question, post it as an answer instead. – jfs Mar 29 '15 at 21:53
  • The question was already answered, but I added the solution to the question, as I in my experience find it very useful to have it in the post, too. – ljrk Mar 29 '15 at 21:55
  • it does not prevent your from following the StackOverflow format: questions are for questions. Answers are for answers. You don't need to accept your own answer but you should put your solution as an answer rather than updating your question. http://stackoverflow.com/help/self-answer – jfs Mar 29 '15 at 21:59
  • I don't want to repost a given solution. I just think that it's much better readable when the answer is kind of embedded. – ljrk Mar 29 '15 at 22:02
  • at the very least read the tour http://stackoverflow.com/tour If I came from google, I look at the answers to find a solution. Your approach forces people to read your question in detail (possibly irrelevant for them). I already know the question (the google send me based on a query, I don't want to reread your question). – jfs Mar 29 '15 at 22:03
  • I think I'm not the only one who knowingly ignores this, due to better readability. – ljrk Mar 29 '15 at 22:06
  • It prevents other people on voting and commenting on *the solution* itself. If you think it is correct; it does not mean that it is correct. – jfs Mar 29 '15 at 22:11
  • @J.F.Sebastian That is a valid point I have not thought of. Thanks for heads-up. – ljrk Mar 29 '15 at 22:12

1 Answers1

2

I think I've run into this before, where unless you're using shell=True you can't use pipes there. I think the reason is that subprocess isn't using a shell to launch your command unless you tell it to, so the | is just being passed as another argument to the executable you're invoking.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • This at least get's rid of the inital error but now pacman says: "error: no operation specified..." though the operation "-Si" is included in "getconflicts.split()" – ljrk Mar 29 '15 at 21:02
  • What was the change you actually made to get that? If you changed to use shell=True, you don't need to split any more, it takes the command as a string and hands that to the shell to process. – Eric Renouf Mar 29 '15 at 21:06