Popen doesn't run my exe - python-3.x

(Python 3)
I'm trying to use popen in a little script to run an executable
The executable is at :
https://github.com/Gorov/FCM_nips_workshop
I'm on windows and by modifying a little the github README command I can run easily the program using
RE_FCT ../data/SemEval.train.fea.sst ../data/SemEval.test.fea.sst predict.fea.fullnerpair.onlyne.txt ../data/vectors.nyt2011.cbow.semeval.filtered 5 0.005
The exe is RE_FCT and all the following text is just arguments
So using a terminal it works just fine and display informations while runing
But when I try to make it run using my little python script, it does nothing. Actually something seems runing but it never ends and doesn't display anything
Here's my code
import subprocess
command = ['RE_FCT', '../data/SemEval.train.fea.sst', '../data/SemEval.test.fea.sst ', 'predict.fea.fullnerpair.onlyne.txt', '../data/vectors.nyt2011.cbow.semeval.filtered', '5', '0.005']
process = subprocess.Popen(test, cwd="fcm", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait()
print() #sometimes it helps
Note that I'm already running another exe with similar code and it works
Thanks in advance

Just solved it
I don't know exactly what is going on but the problem is with the process.wait(), I just deleted it and replaced the Popen function with the run fonction and it works
The difference between those two is that popen allows the script to run immediatly after you call it by creating a process, while using run you have to wait for the task to end

Related

Subprocess.wait() in Python 2.7 vz Pthon 3.x

I'm just migrating my script from Python27 to Python 394 to run my build process like this:
p = subprocess.Popen(_make_clean, creationflags = subprocess.CREATE_NEW_CONSOLE)
p.wait()
p = subprocess.Popen(_make_rebuild, creationflags = subprocess.CREATE_NEW_CONSOLE)
p.wait()
Was working pretty nice in Python 27 but now I can see in Python 394 the Python script is not waiting for subprocesses. Of course, here it is necessary to wait until the first one (clean) is finished to start the second one (rebuild). I was able to make it work without the flag 'CREATE_NEW_CONSOLE'. Unfortunately, I need to separate the outputs for users to see what is the progress. It is just a mess if all scripts are running in the same console.
Why it is not working any more?
Ok, this is an interesting finding. I'm using ConEmu as my main terminal and it is fully integrated into Windows. In the past, I was NOT running my scripts over this emulated terminal and that seems to be the reason why I have this issue today. ConEmu is opening a new terminal window for each subprocess but in a different way than the native Windows Command-Line. When the script is called over Windows CMD everything is fine.

Execute python executable and script inlucing full path

Im trying to create a python script with executes a series of python and sql scripts.
Some need to be executed using one python executable and some using another.
I've tried
from subprocess import call
call([r"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\pythonw.exe", r"C:\Path\to\python\file\blabla.py"])
And
call([r"cd C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3", "pythonw.exe", r"C:\Path\to\python\file\blabla.py"])
But the script isnt being executed.
I think the problem might be "How to execute an .exe file in windows terminal including full path to it"?
Please check my code. It is working well in my side.
from subprocess import call
call([r"C:\Python38\python.exe", r"E:\python\hello.py"])
As i think, the problem is that you use pythonw.exe.
if you use pythonw.exe, you can see any log in terminal.
Pythonw is useful for running script with GUI.

Running two shell commands using Python 3.6 on Ubuntu 18.02

In order to get some software running I need to 1. Run a script that will execute a remote license manage, 2. Execute a shell script to start the software. I can do this by opening a command window in the directory with the rlm , and then type ./rlm to run the Linux executable. Then I can go into the directory that contains the shell script, open a terminal in that location and run ./myshell.sh. This opens the GUI for my software.
I would like to run these steps using a single Python script. I have tried:
#change the working directory...
os.chdir(r'/opt/mysoftwarelocation')
#confirm location change...
print(os.getcwd() )
#run ./rlm...
os.system('./rlm')
At this point I can see from a python terminal that the rlm is running.
I would then like to run the code below to run the shell script...
os.chdir(r'/opt/mysoftwarelocation/sumsubdirectory')
print(os.getcwd() )
os.system('./some.sh')
Unfortunately, after os.system('./rlm') finishes the script stalls and will not execute further and without an errors.
How to I get the second part of my script to run within a single Python script?
Have you tried to run the rlm command in the background?
subprocess module gives a nice interface for that

SyntaxError: Non-UTF-8 code starting with '\x90' in file .\score_python.exe on line 1, but no encoding declared;

I have a python script where I need to create an executable via pyinstaller. Successfully created the exe, but shows the above error while running.
I have already searched on the web and tried many solutions but none of them is working. tried with # -*- coding:utf-8 -*- in the first line of the script but fails.
using Python3.7, PyInstaller3.5
Can anyone help me with this?
Ensure you are not calling the executable using python again.
As in
python long-path-to-the-converted-scrip\script.exe
It is a common mistake, due to the fact that you used python to run the script before, then you try to recycle the same command but forget to delete the python call. The long path to the script (now converted to exe) obfuscates the fact that python is no longer needed at the beginning...hence your error.
Resolution: Please execute the exe without python command.
Example: If you have converted test.py, please go to the directory Current Directory/dist/test/ and either double-click on test.exe or run test.exe from command line.

Python - execute sequential commands in same windows command prompt

I am very new to Python (been learning for a few days) . Can anyone help me with the following issue please?
I am trying to write a program to automate the testing of built in commands of a product which is installed on windows and linux.
Before any of the commands can run, the profile needs to be set using profile.cmd.
The commands ( command1.cmd, command2.cmd etc) then need to run in the same windows command prompt as profile.cmd as they rely on the environment which this sets.
The problem I find is that after profile.cmd has been run, python closes the windows command prompt and opens a new one for command1.cmd.
command1.cmd therefore fails to run properly as it is in a new command prompt which has not had the environment set by profile.cmd.
My code is shown below. I am currently testing on windows but will also need to test on linux.
Any suggestions would be much appreciated. Thanks - Kevin
import subprocess
rc = subprocess.run (["profile.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))
rc = subprocess.run (["command1.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))
rc = subprocess.run (["command2.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))
rc = subprocess.run (["command3.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))

Resources