Python Forum
Understanding subprocess.Popen
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding subprocess.Popen
#1
The code I use is from here, Part 5

Just playing around with generators, I have a Python script to simulate a log file which is constantly updated, then another script, follow.py to get the last line of the log file.

In the instructions it says, I should use subprocess like this:

# the file logsim.py is here: '/home/pedro/myPython/yield/tutorial2008/generators/run/foo/logsim.py'
# navigate to /home/pedro/myPython/yield/tutorial2008 in bash
# then enter ./runserver.py
# but this does not work
p1 = subprocess.Popen(['python','logsim.py'], cwd='generators/run/foo')
But I get this error

Quote:# FileNotFoundError: [Errno 2] No such file or directory: 'python'

However this works fine:

foofile = '/home/pedro/myPython/yield/tutorial2008/generators/run/foo/logsim.py'
p1 = subprocess.Popen([foofile], cwd='generators/run/foo')
I don't understand subprocess.Popen(). The cwd command does not seem to work.

What should I actually put in subprocess.Popen()?

The whole script, which works fine, is this:

#! /usr/bin/python3
# runservers.py
# a simulated log file
import subprocess
import time

print("Running simulated web servers")
print() 
print("This runs a simulated server that writes these log files")
print()
print("run/foo/access.log")
print("run/bar/access.log")
print()
print("Please leave this running as a background process while")
print("working on examples related to infinite input streams")

#p1 = subprocess.Popen(['python','logsim.py'], cwd='generators/run/foo')
foofile = '/home/pedro/myPython/yield/tutorial2008/generators/run/foo/logsim.py'
p1 = subprocess.Popen([foofile], cwd='generators/run/foo')
time.sleep(600)
#p2 = subprocess.Popen(['python','logsim.py'], cwd='generators/run/bar')
barfile = '/home/pedro/myPython/yield/tutorial2008/generators/run/bar/logsim.py'
p2 = subprocess.Popen([barfile], cwd='generators/run/bar')
p1.wait()
p2.wait()
Reply
#2
The strange thing in your error is that Popen does not find the 'python' command. Does 'python' work in a terminal or do you have to use 'python3' ?

Try these commands for example
>>> import subprocess as sp
>>> sp.run(['python', '-c', 'import os; print(os.getcwd())'])
???
>>> sp.run(['python', '-c', 'import os; print(os.getcwd())'], cwd='generators/run')
???
and the same with 'python3'
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Aha! Thanks!

Using python I just get a lot of blurb +

Output:
FileNotFoundError: [Errno 2] No such file or directory: 'python'
But if I use python3, it works:

sp.run(['python3', '-c', 'import os; print(os.getcwd())'])
The above returns:

Output:
CompletedProcess(args=['python3', '-c', 'import os; print(os.getcwd())'], returncode=0)
Reply
#4
(May-12-2024, 09:54 AM)Pedroski55 Wrote: Using python I just get a lot of blurb +
By curiosity what is your OS where 'python' is not a known command? In Ubuntu you could perhaps install the package 'python-is-python3'.
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
I use Ubuntu 22.04 LTS
Reply
#6
(May-12-2024, 10:20 AM)Pedroski55 Wrote: I use Ubuntu 22.04 LTS
Then I recommend installing python-is-python3
« We can solve any problem by introducing an extra level of indirection »
Reply
#7
OK, thanks again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information subprocess.Popen() suddenly giving me grief? davecotter 3 740 Dec-13-2023, 10:49 PM
Last Post: davecotter
  Use subprocess.Popen and time.sleep chucky831 2 2,015 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  Subprocess.Popen() not working when reading file path from csv file herwin 13 15,640 May-07-2021, 03:26 PM
Last Post: herwin
  Did subprocess.Popen() causes main routine to pause stdout? liudr 4 3,760 May-04-2021, 08:58 PM
Last Post: liudr
  disable subprocess.popen prompt echo paul18fr 1 2,098 Feb-04-2021, 02:50 AM
Last Post: Larz60+
  how to pass the interactive string to Popen subprocess maiya 1 1,943 Sep-18-2020, 09:36 PM
Last Post: Larz60+
  How to get program output from subprocess.Popen? glestwid 1 2,436 Aug-19-2020, 05:44 AM
Last Post: buran
  subprocess.Popen() and encodings voltron 0 5,854 Feb-20-2020, 04:57 PM
Last Post: voltron
  Mock call to subprocess.Popen failing with StopIteration tharpa 7 6,061 Nov-08-2019, 05:00 PM
Last Post: Gribouillis
  no coding= option fo subprocess.Popen() Skaperen 5 5,894 Jun-21-2019, 10:24 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020