Run .exe from a python script called by mercurial in cmd shell - python

I have tied a python (2.7) script to a commit in mercurial. In this script, a .exe is called (via the subprocess module), which has previously been generated via the cx_freeze. This .exe basically opens a cmd prompt for receiving user inputs.
When I run a commit through the hg workbench, everything works as intended... the Python script runs, calls the executable, and does its stuff, and the commit works without a hitch.
However, when running a commit via "hg commit" in an initial cmd prompt, the executable portion of this setup never appears. I know the python script still runs. No errors are ever displayed/returned.
Am I missing something obvious, and is there a simple way to get this executable to run properly even when called from a commit in cmd prompt?

So you have mercurial calling a hook that runs a python script that launches an executable that is a python script compiled to an exe? Likely the 3-layer deep script is being run w/o a "terminal" (headless), but it sounds like if you un-snarled a few of those layers you might be better off.

Related

VSCode Remote SSH: Run Python File in New Terminal

I am using the Remote SSH extension for VSCode. Whenever I try to run a Python file in the Python terminal using the Run Python File in Terminal command, while the terminal is already executing another python program, VSCode tries to execute the new program in the same Python terminal, instead of opening a new terminal. Is there a way to execute each python program in a new terminal so that multiple programs can run simultaneously?
Edit: Tried to open a new Python terminal explicitly, but still, the Run Python File in Terminal is always routed to the first terminal. If I run the Python program explicitly in the new terminal by typing python my_prog.py it works, but I am looking for an interactive way.
In VS Code, the command "Run Python File in Terminal" is executed by default in a "default" "Terminal", which avoids the impact of runtime confusion and reduces the time to find python and file paths at runtime.
If synchronization is required, VS Code currently does not support running two or more terminals simultaneously. You can refer to this link:
Can VSCode automatically run python scripts in two or more terminals at the same time?
If you don’t require synchronization, only need to be in a new independent terminal every time the code is executed, you can set it in "launch.json": "console": "externalTerminal",
Then click Ctrl+F5 to run the code or click F5 to debug the code:

Running an executable from Python "This application has requested the Runtime to terminate it in an unusual way."

I'm getting an interesting problem and I can't determine whether it's a problem with my code or the executable that I'm running. Basically I have a Python program that needs to call an external executable to process some data. If I call the executable via PowerShell or cmd, it works fine. However, if I attempt to run the executable via os.system() or subprocess.run(), I get the following error:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
From my understanding of googling the issue, it would appear that this is some sort of C++-related issue, which is the language of the .exe that I'm running. I reinstalled the latest Visual C++ Redist and that did not seem to affect the problem. I've also tried to create a .bat and .ps1 script that runs the .exe. These both run fine via PowerShell and CMD, but raise the same error when run via os.system() and subprocess.run(). The error message is rather nondescript so I'm wondering if anyone knows anything about it and why os.system() etc. might be throwing it.
The relevant code is simply
os.system("GaussBin.exe gaussInput.txt gaussOutput.txt")
When the string is pasted into cmd, it runs perfectly. Additionally, if the parameters of the system() call are incorrect, the exe properly displays the usage function. It's only when I add the output.txt and the program is supposed to run fully that things start to break.
I've had some confusion about what directory os.system runs in. Should I be using .\ when calling the exe?
The .exe file is provided, not built by me.

Unable to Correctly Run Python Scripts Through Scheduler

So I've researched and tested multiple methods to get this to work, but both our IT guy and myself are unsure how to proceed. Ultimately I need to run three python scripts on a server which look at a set of Excel files and scans them for errors. If there are errors, it will email the appropriate people to fix them. The scripts themselves work fine when run through the CMD prompt, either individually or in a batch file. These scripts work perfectly when run manually from the command prompt.
However, when I try to schedule one of the scripts or the batch file, they do not run in their entirety. I have a print statement early on in the scripts and that runs fine in the Python.exe, but then the script abruptly quits. This makes me think that Python runs fine, but the contents of the script aren't running correctly through the scheduler.
To simplify troubleshooting I created two test scripts, test.py and test2.py, and placed them in a simple directory of the server, C:\Store. All this first script does is print something to the console, and write something to a file and save it to the root directory, after which it will wait for user input. The second script is simpler and just prints the architecture of Python and it's build path. Both scripts work fine when run from the command prompt manually.
**test.py** Does NOT Work as scheduled task
import io
print('JUST WORRRRRKKK!')
with open("//kaicmapp/Store/foobar.txt", "w") as file:
file.write("Hello!")
file.close()
raw_input("press any button")
**test2.py** Works as scheduled task
import sys
import platform
print("Python EXE : " + sys.executable)
print("Architecture : " + platform.architecture()[0])
raw_input("\n\nPress ENTER to quit")
What I am using for these test scripts:
Python 2.7 32 bit
Windows Server 2012
Windows User Profile with Admin Rights
Python 2.7 exe added as environment variable. Renamed to python2.exe in installation directory, as I also have Python 3 installed on the serverand want to be able to run them exclusively
Things I've tried:
Running Task Scheduler as Admin
Run with/without highest privileges
Run only when current user is logged in
Run regardless of current user logged in, password entered correctly
Run the scripts in a batch file
The solution listed here, results in same outcome in both scripts
Things I've noticed:
Using python2 "C:\Store\test2.py" in the scheduler will run the script successfully, with the prompt waiting for user input. Run result from the scheduler says The operation completed successfully. (0x0)
Using the same task as above, but running test.py instead, will not result any output from the Python shell, it simply pops up and disappears. Run result from scheduler says Incorrect function. (0x80070001)
Any ideas on how to proceed? I am not very familiar with server administrative tasks, so I feel pretty lost here.
This is a Microsoft scheduler issue.
https://social.technet.microsoft.com/Forums/windows/en-US/c8ad90e1-8435-4e13-9af9-098e72a1926d/error-code-incorrect-function-0x80070001-when-running-a-scheduled-task?forum=winserver8gen
Please check if this solution works for you.

Python Script Won't Run from Aws Ubuntu Terminal

I just set up my first aws server for a personal project I'm doing. I'm running ubuntu linux, and I have a python script that accesses an sqlite database file in order to send email. I have these same files on my own ubuntu machine and the script works fine. I'm having trouble, however, figuring out how to run my script from the terminal in my aws vm. Normally I use idle to run my python script on my linux machine, so I'm trying to figure out how to run it from the terminal and it's giving me some trouble.
I tried
python script.py
which did nothing, so I converted it to an executable, ran it, and got
./script.py: line 1: import: command not found
...and so on
I realized that I had to add
#!/usr/bin/env python3
to my script, so I did that, converted to executable again, and ran it by entering
./script.py
which also did nothing. If the program had run, it would have delivered an email to my inbox. Is there any way I can tell if it's actually trying to run my script? Or am I trying to run it incorrectly?
You can modify the script to add verbose that prints out to the console the status of the script, or if you just want to know whether your script is running in the background, you can check whether the process is active using ps (process name would be the name of the script) :
ps aux | grep "script.py"
Anyways the former is a better practice, since you can exactly know execution flow of your script.

Python Subprocess not Running on Jenkins

I have a python script that handles running my unit tests for Eclipse:
Imports projects to a workspace
Builds the project
Runs the Executable
Here is a Gist of my Python script
The cibuild_tests.bat referenced in the script does this:
set eclipse_dir=%1
set workspace_dir=%2
set project_dir=%3
set name=%4
set config=%5
%eclipse_dir% -nosplash --launcher.suppressErrors -application org.eclipse.cdt.managedbuilder.core.headlessbuild -data %workspace_dir% -import %project_dir% -cleanBuild "%name%/%config%"
This all runs perfectly fine, the problem is when subprocess.call(ex) is called in run_executable(...). The output from that executable is never displayed in the console. The output is displayed when the batch script is run for building the project from Eclipse. The executable is a Google Test build and i have it set to output an XML file of the results. I can find no record of the file being created anywhere leading me to believe it is never being run.
But my check to see if the executable exists do find the executable so it is present.
Everything runs just as i expect on my local machine.
Question: What prevents the second subprocess call from outputting to the console, or running, in Jenkins but the first one has no issues?
So i remoted in to the computer and found the issue. It seems a window was popping up reporting a missing DLL for my run process. Once i ensured the DLL was available the error went away and everything built.

Categories

Resources