Python Forum
problem in using subprocess module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem in using subprocess module
#1
hi
when i was reading the page https://realpython.com/python-eval-funct...es-of-eval about eval() in python, i encounterd a problem.
in the code (the below code is on the above page):
# Run the echo command
import subprocess
eval("subprocess.getoutput('echo Hello, World')")

# Launch Firefox (if available)
eval("subprocess.getoutput('firefox')")
when I ran it, I took the below :
Error:
"'firefox' is not recognized as an internal or external command,\noperable program or batch file."
I think the code is used for running Firefox from inside Python. is it true?
why did not the code run successfully?
I have another question:
is it possible to take information about something in Python? For example, suppose I want to know about %
usage in Python. when I am in idle , I wrote help(%), and it takes an error as
Error:
SyntaxError: invalid syntax
. how can I take information about something in idle?
thanks
Reply
#2
(Sep-23-2023, 12:01 PM)akbarza Wrote: I think the code is used for running Firefox from inside Python. is it true?
why did not the code run successfully?
Yes it will try to run the FireFox binary,but i think you misunderstand how this work.
As the Firefox(Browser) is a long running process and may give no output to stdout at all.
The eval make no sense here,can only use Python eval if output make any sense for this and are secure for task like this.

To give example of that make some sense,so eg when run ping on command line,
it give output about request messages to a target Ip.
import subprocess

response = subprocess.run(['ping', '-n' ,'4', 'python-forum.io'], capture_output=True, encoding='utf-8')
print(response.stdout)
Output:
Pinging python-forum.io [2606:4700:3035::6815:1b29] with 32 bytes of data: Reply from 2606:4700:3035::6815:1b29: time=37ms Reply from 2606:4700:3035::6815:1b29: time=26ms Reply from 2606:4700:3035::6815:1b29: time=37ms Reply from 2606:4700:3035::6815:1b29: time=42ms Ping statistics for 2606:4700:3035::6815:1b29: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 26ms, Maximum = 42ms, Average = 35ms
So now in subprocess running the ping command and capture is output.

Also subprocess.getoutput() is not common used at all.
Doc Wrote:Using the subprocess Module

The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle.
For more advanced use cases, the underlying Popen interface can be used directly.
akbarza likes this post
Reply
#3
(Sep-23-2023, 01:32 PM)snippsat Wrote:
(Sep-23-2023, 12:01 PM)akbarza Wrote: I think the code is used for running Firefox from inside Python. is it true?
why did not the code run successfully?
Yes it will try to run the FireFox binary,but i think you misunderstand how this work.
As the Firefox(Browser) is a long running process and may give no output to stdout at all.
The eval make no sense here,can only use Python eval if output make any sense for this and are secure for task like this.

To give example of that make some sense,so eg when run ping on command line,
it give output about request messages to a target Ip.
import subprocess

response = subprocess.run(['ping', '-n' ,'4', 'python-forum.io'], capture_output=True, encoding='utf-8')
print(response.stdout)
Output:
Pinging python-forum.io [2606:4700:3035::6815:1b29] with 32 bytes of data: Reply from 2606:4700:3035::6815:1b29: time=37ms Reply from 2606:4700:3035::6815:1b29: time=26ms Reply from 2606:4700:3035::6815:1b29: time=37ms Reply from 2606:4700:3035::6815:1b29: time=42ms Ping statistics for 2606:4700:3035::6815:1b29: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 26ms, Maximum = 42ms, Average = 35ms
So now in subprocess running the ping command and capture is output.

Also subprocess.getoutput() is not common used at all.
Doc Wrote:Using the subprocess Module

The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle.
For more advanced use cases, the underlying Popen interface can be used directly.

hi snippet
I am not familiar with the subprocess library and your code:

response = subprocess.run(['ping', '-n' ,'4', 'python-forum.io'], capture_output=True, encoding='utf-8')
how(where) can I gain more info about the above code?
do you have any opinion about my second question?
thanks a lot.
Reply
#4
(Sep-24-2023, 11:09 AM)akbarza Wrote:
(Sep-23-2023, 01:32 PM)snippsat Wrote: Yes it will try to run the FireFox binary,but i think you misunderstand how this work.
As the Firefox(Browser) is a long running process and may give no output to stdout at all.
The eval make no sense here,can only use Python eval if output make any sense for this and are secure for task like this.

To give example of that make some sense,so eg when run ping on command line,
it give output about request messages to a target Ip.
import subprocess

response = subprocess.run(['ping', '-n' ,'4', 'python-forum.io'], capture_output=True, encoding='utf-8')
print(response.stdout)
Output:
Pinging python-forum.io [2606:4700:3035::6815:1b29] with 32 bytes of data: Reply from 2606:4700:3035::6815:1b29: time=37ms Reply from 2606:4700:3035::6815:1b29: time=26ms Reply from 2606:4700:3035::6815:1b29: time=37ms Reply from 2606:4700:3035::6815:1b29: time=42ms Ping statistics for 2606:4700:3035::6815:1b29: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 26ms, Maximum = 42ms, Average = 35ms
So now in subprocess running the ping command and capture is output.

Also subprocess.getoutput() is not common used at all.

hi snippet
I am not familiar with the subprocess library and your code:

response = subprocess.run(['ping', '-n' ,'4', 'python-forum.io'], capture_output=True, encoding='utf-8')
how(where) can I gain more info about the above code?
do you have any opinion about my second question?
thanks a lot.

hi
I ran your code in idle.
the print line without the stdout statement printed messy but with it, the output is clean.
when must the stdout statement be used?
thanks
Reply
#5
stdout is a method, not a statement. This is a statement.
print(response.stdout)
The statement calls the print function, passing response.stdout as the argument.

If you read the documentation for subprocess: https://docs.python.org/3/library/subprocess.html

Output:
class subprocess.CompletedProcess The return value from run(), representing a process that has finished. args The arguments used to launch the process. This may be a list or a string. returncode Exit status of the child process. Typically, an exit status of 0 indicates that it ran successfully. A negative value -N indicates that the child was terminated by signal N (POSIX only). stdout Captured stdout from the child process. A bytes sequence, or a string if run() was called with an encoding, errors, or text=True. None if stdout was not captured. If you ran the process with stderr=subprocess.STDOUT, stdout and stderr will be combined in this attribute, and stderr will be None.
response is a CompletedProcess object. response.stdout is the captured stdout from the child process.

Python documentation is pretty good.
Reply
#6
(Sep-24-2023, 11:09 AM)akbarza Wrote: hi snippet
I am not familiar with the subprocess library and your code:
response = subprocess.run(['ping', '-n' ,'4', 'python-forum.io'], capture_output=True, encoding='utf-8')
how(where) can I gain more info about the above code?
To explain the basic of this,so ping is a external program ping.exe that you have on your OS Windows .
From cmd:
C:\>ping -n 4 python-forum.io

Pinging python-forum.io [172.67.168.227] with 32 bytes of data:
Reply from 172.67.168.227: bytes=32 time=21ms TTL=53
Reply from 172.67.168.227: bytes=32 time=37ms TTL=53
Reply from 172.67.168.227: bytes=32 time=32ms TTL=53
Reply from 172.67.168.227: bytes=32 time=35ms TTL=53

Ping statistics for 172.67.168.227:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 21ms, Maximum = 37ms, Average = 31ms 
So it's a way to take a external programs into Python,and run it if program have output can also catch that.
Like this will just start FireFox from Python.
import subprocess

subprocess.run(['C:/Program Files (x86)/Mozilla Firefox/firefox.exe'])
Look at Doc for eg run().
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with memory_graph module akbarza 3 375 Mar-04-2024, 10:15 AM
Last Post: snippsat
  problem using coloeama module akbarza 3 582 Jan-08-2024, 07:31 AM
Last Post: akbarza
  problem in import module from other folder akbarza 5 1,439 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  problem in using pyqrcode module to create QRcode akbarza 9 1,999 Aug-23-2023, 04:17 PM
Last Post: snippsat
  Problem with Pyinstaller. No module named '_tkinter' tonynapoli2309 0 1,017 May-15-2023, 02:38 PM
Last Post: tonynapoli2309
  Problem with module time and leap seconds Pedroski55 3 1,255 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  Question on subprocess module. knoxvilles_joker 3 2,708 Apr-11-2021, 12:51 AM
Last Post: knoxvilles_joker
  keyboard module; must be root problem philipbergwerf 2 19,127 Apr-04-2021, 11:40 AM
Last Post: philipbergwerf
  Problem with Flask Bcrypt import module marcello86 2 5,746 Aug-31-2020, 08:10 PM
Last Post: marcello86
  Js2Py module not found problem Charliedactyl1 5 7,946 Jun-14-2020, 07:39 PM
Last Post: buran

Forum Jump:

User Panel Messages

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