Using Subprocess to run another python script - python

I am a newbie to python and would like to seek some advice. I having a script now where the function of this script can only be executed after I run a command i.e. python run trial.py. However, I would like to automate this process by using a subprocess function in a new python file called 'run.py' to run the trial.py script.
After that, I would wish to convert run.py file into an exe file to ease the execution for other users.
I have tried to perform the below steps.
1. saved the scripts (trial.py & run.py) in same directory.
2. Execute both of the files in same conda virtual environment.
3. Successfully execute run.py by using ```subprocess.run('xxx run trial.py')```
4. Converted the run.py into an exe file
5. Tried to execute the exe file and it is running, but **failed** to show the output that suppose to be appeared after running trial.py.
Would like to seek advice is any steps on above did wrongly or need to be improvised? I need to deal with confidential data hence the easiest way I can do is by using pyinstaller to allow another user to execute.
Hope to hear some advice
UPDATE
I had tried to use the codes below,
import subprocess
import sys
from subprocess import PIPE, STDOUT
command ='python run trial.py'
run = subprocess.Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
Now the exe file is able to be generated and able to run, but it doesn't appear the expected output. It ended without any error message. It works well when I run the script in python..
Wish to hear advice from all of you..

With subprocess, you can try it:
command = 'Your command' # Example: command = 'python trial.py'
process = subprocess.Popen(command, shell=True, stdout=sys.stdout, stderr=sys.stderr)

I would wish ... to ease the execution for other users.
As I've understood your case, using subprocess and making an exe file out of your python file (which is not an easy task) is not a good fit for you.
Instead, I recommend you to have a look at make files as they are well-known for simplifying your commands.
for example you can have a make file like this:
run:
python trial.py
And users can simply run make run, and python trial.py will run instead.
The possibilities are endless.
You can also make a bash file that is an executable,
# !/bin/bash
python trial.py
And it will simply run like exe files.

Related

How to send value from python script to console and exit

My python script generate a proper command that user need to run in the same console. My scenario is that user is running a script and then as a result see the command that must to run. Is there any way to exit python script and send that command to console, so user do not need to copy/paste?
A solution would be to have your python script (let's call it script.py) just print the command: print('ls -l') and use it in a terminal like so: $(python3 script.py). This makes bash run the output of your script as a command, and would basically run a ls -l in the terminal.
You can even go a step beyond and create an alias in ~/.bashrc so that you no longer need to call the whole line. You can write at the end of the file something like alias printls=$(python3 /path/to/script.py). After starting a new terminal, you can type printls and the script will run.
A drawback of this method is that you have no proper way of handling exceptions or errors in your code, since everything it prints will be run as a command. One way (though ugly) would be to print('echo "An error occured!"') so that the user who runs the command can see that something malfunctioned.
However, I'd suggest going for the "traditional" way and running the command directly from python. Here's a link to how you can achieve this: Calling an external command in Python.
Python can run system commands in new subshells. The proper way of doing this is via the subprocess module, but for simple tasks it's easier to just use os.system. Example Python script (assuming a Unix-like system):
import os
os.system('ls')

Running an exe file in python - not working

I have a crazy problem.
I have a cmd to run an exe file and it executes with no errors. The cmd in command prompt is
E:\project\cpp\myfirst.exe
I have to call this exe file within my python script. I use subprocess.call. But I get an error. The code and error is as follows
import subprocess
subprocess.call('E:\\project\\cpp\\myfirst.exe')
The error i get is
ERROR: Could not open myfirst setup file
1
I couldnt find the solution. I also tried os.system call. But still the same error. can you guys help me.
NOTE: the exe file is generated from a cpp code
thanks
The program seems to be seeking for some configuration file in the working directory, which is not always the same as the one where the executable is. Try instead:
import subprocess
subprocess.call('myfirst.exe', cwd=r'E:\project\cpp')
If you have written myfirst.exe yourself, consider changing the lookup logic so that it checks the executable's own directory.
Under Linux I have always found the popen mechanism to be more reliable.
from subprocess import Popen, PIPE process = Popen(['swfdump',
'/tmp/filename.swf', '-d'], stdout=PIPE, stderr=PIPE) stdout, stderr =process.communicate()
Answer taken from
How to use subprocess popen Python

Create a process that runs in background using python

I want to write a python program that runs in the background.
I mean, like we install Python package. And later, we can run any script using python in front of the script name. This means that some python process is running in background which can take inputs and perform actions.
And in case of linux, you can call grep from anywhere. That means grep is also running in the background somehow.
I want to write something like that in python. When I call certain function with name and arguments at any time, it should perform the intended action without caring for the original code. But I am not able to find how to achieve that.
Can anyone please help me here?
Thanks in advance.
Clarification: the fact that you can run python or grep in a console just by typing their name, does not mean that they run in background. It means that there exist an executable file in some location, and this location is listed in the environment variable PATH.
For example, on my system I can run Python by typing python. The python executable is installed at /usr/local/bin/python, and has the execute permission bit on.
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
and yes, /usr/local/bin is contained in PATH.
You can do the same with python scripts:
ensure that the very first line of your script contains #!/usr/bin/python or #!/usr/bin/env python
give your script execute permissions: chmod a+x yourScript
either move your script to one of the directories contained in $PATH, or add the directory where your script is located to PATH: export PATH=$PATH:/home/you/scripts
Have a look at
http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
you can roll out your own daemon by inheriting the Daemon class and overriding run method
from daemon import Daemon
class run_daemon(Daemon):
def run(self):
import sys
run_daemon.execute_shell_command(sys.argv[1])
#staticmethod
def execute_shell_command(ShellCommand):
import subprocess
process = subprocess.Popen(ShellCommand, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.communicate()

"script command" and logging in python

I have a python app that has lots of outputs on the screen which can be used for debugging. out of all the logging techniques, "script" command works well for me because I can see the output on the screen as well as logging it. I want to include that at the beginning of my python app to run automatically and log everything, when I do, however, the python program doesn't run. as soon as I type exit at the terminal (which stops script logging) the app starts working. The command I'm using is:
command="script /tmp/appdebug/debug.txt"
os.system(command)
I have also tried script -q but the same issue is there. Would appreciate any help.
Cheers
Well, I did find the answer for anyone who is interested:
https://stackoverflow.com/questions/15507602/logging-all-bash-in-and-out-with-script-command
and
Bash script: Using "script" command from a bash script for logging a session
I will keep this question as others might have the same issue and finding those answers wasn't exactly easy :)
Cheers
Try to use subprocess, like so:
from subprocess import Popen, PIPE
p = Popen(['script', '/tmp/appdebug/debug.txt'], stderr=PIPE, stdout=PIPE)
stdout, stderr = p.communicate()
script is a wrapper for a session of interactions. Even if it appears to terminate quickly after being started in a shell, this is not so; instead it starts a new shell in which you can interact so that everything is logged to a file.
What does this mean for you?
Your approach of using script cannot work. You start script using os.system which will wait for script to terminate before the next Python statement is executed. script's work will only happen before it terminates (i. e. during the uninteresting waiting period of your Python program).
I propose to use script -c yourprog.py yourprog.log instead. This will execute and wrap the yourprog.py and the session will be stored in yourprog.log.

Python 2.4.6 subprocess is befuddling me

I have written a nice python script that parses XML and adds some sophisticated logic to then interface with an external command via subprocess module.
Most of the subprocess.Popen calls do exactly what they're supposed to, but the last one simply refuses to execute. No error message , it just doesn't do what it's supposed to. I even put the actual CMD into a shell script surrounded by debugging statements, and the shell script gets executed, but not the actual CMD.
More infuriatingly, the very same line of code in a separate .py file executes just fine.
I have no idea why or how this could be?
The python code is generating a file and tries to invoke the external command with options
p = subprocess.Popen([CMD,'object','new_host','--file','/tmp/add.1234'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
r = p.communicate()
print r
this logic works in the standalone file, but not in the larger python script (which has other working Popen calls in it).
Does anybody have an idea why this could be?
PS: I can not update python to a more recent version

Categories