Python script hangs without throwing exception/error on Windows - python

There isn't a particular point in the script where it hangs (I've seen it getting stuck at random points in the script), so checking the logs didn't yield much insight. It doesn't even throw an exception or an error. It just keeps running while stuck.
I'm basically calling this python script from a powershell file (which later gets called by Task scheduler).
$python = "C:\Python34\python.exe"
$python_path = "C:\Source\main.py"
cd (split-path $python_path)
while($true)
{
& $python $python_path
}
Is there something I need to do to make sure it doesn't get stuck?

You Will have to do your path strings like this path ="c:\\test\\file"
Because it ignorer the first backslash

Related

Python in Execute Process Task

I have written a python code, then ran it through SSIS in the Execute Process Task, because I want to schedule it with table refresh dependency(which is not possible through windows scheduler or Alteryx).
The code reads and writes to SQL management studio to translate via Google API (pip install googletrans==4.0.0-rc1), with no output or input through SSIS. the input and output are done through sqlacademy in python.
The codes runs perfectly fine in Jupyter notebook, and I saved it to be .py file, but I face a lot of error in the Execute Process Task in SSIS.
Here are a couple of the solutions I tried:
in EXCUTABLE (in command prompt written where python copied .exe path)
in Arguments C:......\file.py
in WorkingDirectory C:......\ (only removing the file name while keeping the backslash
no white spaces a solution from stackflow)
Error:
"C:\Users\me\AppData\Local\Programs\Python\Python311\python.exe" "UC2.py" at "C:\Users\me\Downloads\folder\test", The process exit code was "1" while the expected was "0".
in EXCUTABLE (in command prompt written where python copied .exe path)
in Arguments 'file.py' (file name in quataion a solution from stackflow)
in WorkingDirectory C:......\ (only removing the file name while keeping the backslash
no white spaces a solution from stackflow)
Error:
The process exit code was "2" while the expected was "0"
move the .exe to a folder were no other files exists.
The process exit code was "-1073741515" while the expected was "0"
I also tried opening a new project.
then I tried right click change success value no error when running in SSIS, but the following error pops up:
enter image description here
everything even the sql server is installed on the remote desktop.
HelloWorld.err.py
A trivial python script that will write a string to standard out, another to standard error and the exit with return code 1.
import sys
print("Hello world")
print("Egads, something bad happened!", file=sys.stderr)
exit(1)
SSIS package
A sample SSIS package would look something like the following. An Execute Process Task has a successor of a Script Task.
Double click the precedent constraint (the line between the two) and change that from the default of Success to Completion
EPT Python
An Execute Package Task that is configured as shown
Executable: C:\Program Files\Python39\python.exe
Arguments: C:\ssisdata\SO_75361110.err.py
StandardOutputVariable: User::StdOut
StandardErrorVariabele: User::StdErr
SCR Echo Back
This is a generic Script Task that I use and abuse to dump the values of SSIS Variables to the output log/results tab. It's easy, lightweight and let's you print values without popups or any hard work.
All you need to do is add the Variable(s) in the readonly/readWrite collection. In this case, I selected User::StdOut and User::StdErr
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
namespace ST_ede4b38a087c4724a71149ffc5ed6be9
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
bool fireAgain = false;
foreach (Variable item in Dts.Variables)
{
Dts.Events.FireInformation(0, "SCR Echo Back", string.Format("{0}->{1}", item.QualifiedName, item.Value), "", 0, ref fireAgain);
}
Dts.TaskResult = (int)ScriptResults.Success;
}
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
}
}
Results
When my package runs, as "expected" the SuccessValue from my run will be 1 given that I use exit(1) from the python script.
Let's see what the Output tab looks like
SSIS package "C:\Users\bfellows\source\repos\SO_Trash\SO_Trash\SO_75361110.dtsx" starting.
Error: 0xC0029151 at EPT Python the thing, Execute Process Task: In Executing "C:\Program Files\Python39\python.exe" "C:\ssisdata\SO_75361110.err.py" at "", The process exit code was "1" while the expected was "0".
Task failed: EPT Python the thing
Warning: 0x80019002 at SEQC Do the thing: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED. The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
Information: 0x0 at SCR Echo Back, SCR Echo Back: User::StdErr->Egads, something bad happened!
Information: 0x0 at SCR Echo Back, SCR Echo Back: User::StdOut->Hello world
Warning: 0x80019002 at SO_75361110: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED. The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
SSIS package "C:\Users\bfellows\source\repos\SO_Trash\SO_Trash\SO_75361110.dtsx" finished: Failure.
The program '[68044] DtsDebugHost.exe: DTS' has exited with code 0 (0x0).
Of note, are these two lines
Information: 0x0 at SCR Echo Back, SCR Echo Back: User::StdErr->Egads, something bad happened!
Information: 0x0 at SCR Echo Back, SCR Echo Back: User::StdOut->Hello world
That's our Script Task echoing back the value of our Standard Out and Standard Error text streams. My suspicion is that given a "normal" run is 10 minutes and the runs from SSIS are measured in seconds, the process execution is throwing errors to the caller but nobody is listening. This will add a listener and give you better information to diagnose your root cause.

how to schedule a python script

I want to Schedule a python script.
I need help in ACTIONS tab: Here I have provided the below
I'm unable to run the task.
Please assist.
On a Windows 10 system with standard Anaconda installation, I ran the
following action, leaving all the other screens defaulted, except for
selecting trigger at a certain time for a one-time run:
Program/script: C:\ProgramData\Anaconda3\python.exe
Add arguments: "try csv 2.py" (double quotes since spaces in script name)
Start in: C:...\folder (in your case: C:\helloo)
On first try, I ran this task with the arguments without double quotes; this resulted in Last Run Result (0x2). I think this is the return code from execution, with 0 being success.

Using backticks in bash script, to call python script into variable had previously worked, but is broken now

I have a simple script I have written for a game. This script is called from ~/.profile in the game user's home directory, and is supposed to simulate a fake login that runs another script upon successfully comparing two variables. It worked for a little while and then abruptly stopped working.
(Edit: When I mean it stopped working, I don't mean I changed something and it messed everything up, I mean it stopped working after not touching it for a night.)
#!/bin/bash
pause(){
read -p "Place [CARD] on scanner and press [ENTER] key..."
}
init(){
cat /etc/issue
pause
READ=`sudo ~/bin/Read.py`
CARD=`cat ~/var/keycode.txt`
if [ "$READ" == "$CARD" ]; then
~/bin/mmenu.sh
else
echo "Incorrect... Please try again"
fi
}
while true
do
init
done
What am I doing wrong?
Thanks in advance.
Edit:
The output of Read.py is 12345, and the output of cat keycode.txt is 12345
yet, it doesn't seem to be able to get them to compare correctly.
The script has been dumbed down, with Read.py not printing, just writing its contents to a file called rcard.txt, and both variables like this:
CARD=`cat ~/var/kcard.txt`
READ=`cat ~/var/rcard.txt`
Try as I might, even with [ "$READ" == "$CARD" ], it still fails the if statement. Even with the contents being the exact same.
I really don't get it.
There are a few possible sources of error here, which depend on what your error message is (if you are even getting one). It's possible that sudo ~/bin/Read.py is returning more than one word of text, separated by spaces, which would cause an error like so:
$ foo=hello world
$ bash: world: command not found
This same error could also be occuring with cat ~/var/keycode.txt. To avoid this potential error, even if this is not the error you are receiving, I would wrap your backticks in quotation marks like so:
READ="`sudo ~/bin/Read.py`"
CARD="`cat ~/var/keycode.txt`"
If you are receiving no error message, but the code is not working when you have what should be correct input, I would check the files ~/bin/Read.py and ~/var/keycode.txt and see if you can identify your issue there - an inconsistency could be occurring in one of the two to cause an uncaught error.
If you can offer more clarification on how your program "abruptly stopped working," that would be useful.
I think it still loops in the else statement, because there are several lines to compare, and it won't be OK like this. In addition, you assignement may join some lines in once.
You can replace your if statement this way:
if [ $( sudo ~/bin/Read.py |grep -wc "$CARD" ) -ge 1 ]; then
This grep instruction with the -c option, will return the count of lines in "$CARD" mathing those returned by your Read.py execution. Thus, if there is at least one line matching, you will loop in the then statement like you want.

(1) Running a .py in cmd and (2) with variable in same line

I figured out the zip code in same line out. It's sys.argv[1], I had other code I neglected to comment out when trying out [1] that gave me the error. All I need help with now is getting weather.py to run without having to call the whole file path.
I will preface with I'm not very experienced with python and may get certain names wrong or think something might work that obviously doesn't, bear with me I tried to word this to make as much sense as possible.
So I need to run a program using the command line. The program is complete and 100% functioning when ran in PyDev. The program is called weather.py, and what needs to trigger it in cmd is
python weather.py (5 digit zip)
I cannot get the program to run using just 'python weather.py' first off. I have added C:\python27 to PATH as well as C:\python27\python.exe (not sure if that does anything). Getting the .py to run via those two keywords doesn't seem to work with what I've tried. I also need to be able to add a zip code to the same line to trigger the program. I was told about
zipcode = sys.argv[0]
to allow the zip code to be automatically initialized as a variable, but I get
IndexError: list index out of range
when I run the program using
python C:\python27\weather.py
I tried replacing 0 with 1 or 2 because I'm unfamiliar with .argv but neither of those worked either. Any help getting the program to run using just python weather.py OR getting the zip code input to function on the same line is greatly appreciated.
Make sure you import sys in your code.
import sys
zipCode = sys.argv[1]
and actually provide an argument
EDIT:
For clarity, if sys was not imported, you would get NameError and not an IndexError. Additionally, when passing args in from the command line, the indexing actually begins at 0 where sys.argv[0] is always the program name and the provided args begin at 1. So, in this case, the zip code would be at sys.argv[1]
EDIT2:
variable name to avoid using reserve words :)

Running Another program from python

I want to call a program multiple times from a python code, and save the output of that program in a text file. My first problem right now is just calling the other code. I have to redirect to a different directory and call ./rank on output.txt. This is how Im trying to do it:
TheCommand = "~/src/rank-8-9-2011/rank output.txt"
os.system(TheCommand)
but im getting a parsing error.
[Parsing error on line ]Unknown error: 0
Im running python2.7 on Mac OS 10.5.8. Im not sure what the problem is. I also tried using subprocess:
subprocess.call(["~/src/rank-8-9-2011/rank", "output.txt"])
This does not find the directory (I have a feeling Im using the subprocess incorrectly), but I dont know what is wrong with the os.system.
the name of the program in the first argument to subprocess.Popen must not contain ~ as it doesn't pass the string to the shell for processing (which like always using parameterized queries in sql, protects one from string injection attacks, e.g. if instead of output.text one had ;rm -rf /, the system version would run rank and then run rm -rf . but the subprocess.Popen would only have rank open a file named ;rm -rf .), so one should expand it by calling os.path.expanduser:
subprocess.Popen([os.path.expanduser('~/src/rank-8-9-2011/rank'), "output.txt"])
although it is possible to turn shell processing on by passing shell=True, it is not recommended for the aforementioned reason.
you should try http://docs.python.org/library/os.path.html#os.path.expanduser
import os.path
subprocess.call([os.path.expanduser("~/src/rank-8-9-2011/rank"), "output.txt"])
I'm fairly certain your parsing error is coming from rank, not from your os.system command, as nothing there looks weird. What happens if you run rank manually?
subprocess seems to have a problem with '~', although I'm not immediately sure why. Put the full path and it should work (although you'll likely get that parsing error if it is indeed a problem with rank).

Categories

Resources