Python Forum

Full Version: Understanding subprocess.Popen
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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'
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)
(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'.
I use Ubuntu 22.04 LTS
(May-12-2024, 10:20 AM)Pedroski55 Wrote: [ -> ]I use Ubuntu 22.04 LTS
Then I recommend installing python-is-python3
OK, thanks again!