subprocess.call command runs but doesn't actually do anything - python

I get no error message when running the following code, however it doesn't perform the required task (to be more precise, the output is an empty file).
import subprocess
chemin_in_troncon = r"C:\Users\HBT\Desktop\Run\1OUTIL_CREATION_DB\donnees_out\2-FAX0W0\FAX0W0900_2.in"
chemin_strapontin = r"C:\Users\HBT\Desktop\STRAPONTIN\strapontin9120.exe"
result900 = subprocess.call([chemin_strapontin, chemin_in_troncon], shell=True)
However, when I execute the equivalent task directly from the shell with the following command, the .exe file does its job and gives me a non-empty output :
strapontin9120.exe ../Run/1OUTIL_CREATION_DB/donnees_out/2-FAX0W0/FAX0W0900_2.in
Here, I wrote this shell command while in the directory containing "strapontin9120.exe".
Any idea what is wrong in my code?

Related

Why Does Python Call a Subprocess Command Incorrectly?

This is a follow up on a previous question as I have made progress(which is irrelevant at this point). It is worth noting that I am learning python and barely know what I am doing, however, I am familiar with programming. I am trying to call an SCP command in the windows terminal through python. However, it is not doing the desired effect. The script runs smoothly with no errors and it prints the debug commands as I have written them. However, the SCP call does not actually go through on the other end. To make sure I have the right command, I have it set to print the same command that it called afterwards. When I copy this printed command and paste it into the windows command terminal, it gives the desired effect. Why is the same command not working correctly in python? Thanks. This is my script:
import subprocess
subprocess.run(['scp', 'c:/users/<name>/desktop/OOGA.txt', 'pi#<IP>:here/'], shell=True)
print ("done")
print ('scp', 'c:/users/<name>/desktop/OOGA.txt', 'pi#<IP>:here/')
Try using raw string if shell is set to True:
from subprocess import run as subrun
status = subrun(r'scp c:/users/<name>/desktop/OOGA.txt pi#<IP>:here/',shell=True)
print("Done")
print(status)

How to run a powerShell script from a python file

I couldn't find much information on this unfortunately, but I wish to run a powerShell script from a python file I've written. I want the user to actually see the powerShell script being run and the user can enter inputs that the powerShell script requires from python. I am using pyCharm as an IDE.
When I run the script to call this powerShell script, it gives me this error:
File "C:\TestAutomation\eFuse\eFuse.ps1", line 19
SyntaxError: Non-ASCII character '\xc2' in file C:\Test\eK\eK.ps1 on line 19, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Here is the relevant part of the code:
elif switch_result == "eTool":
subprocess.call(['python', 'C:\\TestAutomation\\eFuse\\eFuse.ps1'], stdout=sys.stdout)
This elif statement is a part of other if/elif statements that run other python files using the subproccess module, but for some reason I can't get this powerShell file to be run. Any advice is appreciated. Thank you
First, you should already know .py file's interpreter is python.exe
So, it's easy to understand .ps1 file's interpreter is powershell.exe, not python.exe
I just copy & paste from yours, your code should look like following,
subprocess.call('powershell.exe -File "C:\\TestAutomation\\eFuse\\eFuse.ps1"', stdout=sys.stdout)
Details about powershell.exe -?
You can do following for provide input from user to PS (powershell) script from python :
1) Create parameterised powershell script
2) Take input in python and set it as PS script parameter.
3) run/execute the script with given params.
Here is the sample to run powershell script from python with param. Its a python code :
* Hope you have the PS script with parameters.
import subprocess
params = ['param1', 'param2'] # POWERSHELL SCRIPT PARAMETERS ( optional )
script_path = "C:\\PowershellScripts\\test.PS1" # POWERSHELL SCRIPT PATH
commandline_options = ["Powershell.exe", '-ExecutionPolicy', 'Unrestricted', script_path] # INITIALIZING COMMAND
for param in params: # FOREACH LOOP OF PARAMETERS
commandline_options.append(param) # ADDING SCRIPT PARAMETERS TO THE COMMAND
result = subprocess.run(commandline_options, stdout = subprocess.PIPE, stderr = subprocess.PIPE, universal_newlines = True) # RUN THE SCRIPT USING SUBPROCESS WITH PARAMS
print(result.returncode) # PRINT THE RETURN CODE FROM POWERSHELL SCRIPT
print(result.stdout) # PRINT THE STANDARD OUTPUT FROM POWERSHELL SCRIPT
print(result.stderr) # PRINT THE STANDARD ERROR FROM POWERSHELL SCRIPT
You can see the output of python if it exist ( if there is no error in powershell script. you will not get any output from last line )

ununderstandable behavior subprocess.Popen(cmd,stdout) and os.system(cmd)

I use an external command inside a python script using firstly:
subprocess.Popen(cmd, stdout=subprocess.PIPE)
then i get the stdout.
The problem is that the result of this external command when executing it inside the script is not the same if i execute it directly in the command line.
I use then os.system(cmd), but the same problem.
Is this instructions in python use some buffers?
How can i explain the difference between the two results (command line and inside the script).
I'm using this tool as a local command from comman line after installing it:
https://potassco.org/clingo/run/
I use some file as input like this one:
edge("s1","s3").
edge("s2","s4").
edge("s3","s4").
path(X,Y) :- edge(X,Y). % x and y are strings
path(X,Z) :- path(X,Y), path(Y,Z).
:- path(X,Y), path(Y,X). %cyclic path.
To do it the tool generate a model like this :
edge("s1","s3") edge("s2","s4") edge("s3","s4") path("s1","s3") path("s2","s4") path("s3","s4") path("s1","s4")
SATISFIABLE
When i call the command inside my python script it doesn't compute all the model, it generate an incomplete model. This problem appear only in the big examples, which requires the computation of a large model . That's why i'm asking if this commands : subprocess.Popen and os.system use some buffers...

Python subprocess.run with stderr=subprocess.PIPE redirects input(text)

I wrote a program (myProg.py) that uses subprocess module to run other python programs through the run function. I noticed that arg in input(arg) statements in these other python programs are not being displayed to the console (stdout), while args in print(args) are displayed properly. This only happens when I run my program through console. This does not happen when I run my program through LiClipse.
Here is the simplest way to replicate this situation:
Using python 3.6.2, and windows 10
create a python program containing the following two lines and save it as someProgram.py:
a = input("Enter anything")
print("a has: " + a)
open cmd, type: python
type: import subprocess
type: subprocess.run(["python", "path..to..someProgram.py"], stderr=subprocess.PIPE)
No ouptut so far but the interpreter is waiting on input... type something and hit Enter.
You will see the output from the print statement.
If you remove , stderr=subprocess.PIPE you will see the correct outputs.
Now, save the code from steps 2 & 3 in another file, call it myProg.py in the same folder as someProgram.py. Run myProg.py from LiClipse. You will find that the myProg.py runs fine and produces the correct outputs.
My questions are:
Why is this problem with subprocess.run happening
How can I solve it
Why would there be a difference between running code through commandline and through an IDE.
input function print prompt text to stderr, this is a known issue
you redirected stderr, therefore prompt text not shown, when you run from LiClipse, stderr not get redirected.
you could output prompt by your self, like:
print("Enter anything", end='')
a = input()
alternatively, import readline module before input, then input will use GNU readline lib (if you have), which prints to stdout. While in your case, the program runs as a subprocess, you could still achieve this:
python -c "import readline; import runpy; runpy.run_path('/path/to/program.py')"

Python code acting differently than when run in terminal

When I pass my mpirun command through terminal, the normal (and expected) outcome is an output file with a bunch of data in it.
However when I pass the code through my python script, all of the output files that are expected are created, however they contain no data. Is there any global explanation for this? I have tried the code many different ways, using both os.system and subprocess. I have also tried running the code in the background as well as just running. And I have also tried just having the program spit out the data vs. saving it to the output file, and all give the same result.
Here is the code:
os.system("mpirun -np 4 /home/mike/bin/Linux-ifort_XE_openmpi-1.6_emt64/v2_0_1/Pcrystal > mgo.out")
The simplest way to get that behavior is if mpirun is not being successfully run.
For instance if, from the command line, I run
not_actually_a_command > myFile.txt
myFile.txt will be created, but will be empty (the "command not found" message is printed to stderr so won't be caught by ">").
Try using the fully resolved path to mpirun
There doesn't seem to be something inherently wrong with your approach. When I do
os.system("echo hello, world >hello.txt")
it ends up with "hello, world" in it, so if you get your command to run it should work for you.
You should start with providing a complete path
os.system("/complete/path/to/mpirun
and print the result, print(os.system...etc.),
and post the error so we know what is wrong.
When using the subprocess module it may require a "shell=True"

Categories