Script hangs when importing pandas - python

I have an issue when importing pandas and running the script on windows task scheduler. In the end the program just hangs and no error occurs. When I execute the script in command prompt, there's no problem. I've tried a lot of different things but couldn't fix the problem so far.
What I'm looking for now is a way to import pandas in verbose mode and write the output real time to a file. I've found a lot of explanations to do this with e.g. python -v module.py 2> output.txt in the shell. But what I'd like to do is something like this:
with profiler as context:
import pandas
with open("output.txt", "w+") as file:
file.write(context.output())
The script should write the output in real time so that I can kill the task an still have the output until the program is hanging.

I had a similar problem, only I was using PyCharm.
I had an old project from my previous job, when I tried opening this project in my new PC, python got stuck after importing Pandas, "the program just hanged and no error occurred". Oddly enough, in my other projects pandas worked fine.
I noticed that when starting, pycharm displayed the path where the python interpreter was running. In my old project the path was
C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe
In my other projects the path was
D:\Users\user\Anaconda3\python.exe
I solved this in pycharm by choosing the interpreter from my other projects to my old project.
I think that you can start by knowing what interpreter is running in your windows task scheduler and what interpreter runs in the command prompt. You can check this with
import sys
sys.executable
If it is the case that there are different interpreters running, maybe this thread will result useful: Change default python version for command prompt

Related

Python script not running with Windows task scheduler due to numpy

Running a basic python script with the windows task scheduler only works when I comment out the import statement for numpy and I can't figure out why.
Details:
I was previously trying to run a more complex script but in order to narrow down the issues I was having I ended up creating the following basic script to test run with the task scheduler.
import numpy
outDIR = "C:\\Users\\trill_gates\\OneDrive\\Documents\\DS Projects\\"
f = open(outDIR + "demofile2.txt", "w")
f.write("Test Text")
f.close()
Running the script normally does not produce any issues. However, when I set it up to run with task scheduler, the file its supposed to write to isn't created. Only when the import numpy statement is commented out does it work again. In the proper script I need run on a schedule, I'm using numpy and that seems to also be what was causing the script to not run. I tried this on two separate computers, uninstalled and reinstalled anaconda as well as created a clean conda environment with just the necessary packages and I'm still getting the same error.
This is the setup I'm using in task scheduler to call the script.
I'm using an anaconda distribution of python 3.7.7 and numpy 1.18.5.
Any help would be appreciated.
I have something similar. Updated to Python 3.7 yesterday and it seems to have killed the ability to execute scheduled tasks. The error I get is numpy related even though the script doesn't use numpy. . More shortly.

Debugging htcondor issue running python script

I am submitting a python script to condor. When condor runs it it gets
an import error. Condor runs it as
/var/lib/condor/execute/dir_170475/condor_exec.exe. If I manually copy
the python script to the execute machine and put it in the same place
and run it, it does not get an import error. I am wondering how to
debug this.
How can I see the command line condor uses to run it? Can the file
copied to /var/lib/condor/execute/dir_170475/condor_exec.exe be
retained after the failure so I can see it? Any other suggestions on
how to debug this?
You can simply run an interactive job (basically just a job with sleep or cat as command) and do ssh_to_job to run it.
Generally you need to set-up your python environment on the compute node, it is best to have a venv and activate it inside your start script.

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.

subprocess.Popen not working in ubuntu (working fine in Windows)

I am right now in the same directory in which the file "lookup.csv" is residing.
I have tried following commands in Python 2.7:
import subprocess
subprocess.Popen("lookup.csv", shell = True)
The above is producing the following error:
lookup.csv : not found
I have double-checked for the working directory, tried lot of available troubleshooting options given in StakExchange, tried the same in Windows (and surprisingly it was working there), what more can I do?
There's no reason why this should work on Windows because lookup.csv clearly is a file which you want to open with open whereas subprocess.Popen creates a new process for working with the lookup.csv binary. Maybe Windows handles process failure differently.

Problems running python script by windows task scheduler that does pscp

Not sure if anyone has run into this, but I'll take suggestions for troubleshooting and/or alternative methods.
I have a Windows 2008 server on which I am running several scheduled tasks. One of those tasks is a python script that uses pscp to log into a linux box, checks for new files and if there is anything new, copies them down to a local directory on the C: drive. I've put some logging into the script at key points as well and I'm using logging.basicConfig(level=DEBUG).
I built the command using a variable, command = 'pscp -pw xxxx name#ip:/ c:\local_dir' and then I use subprocess.call(command) to execute the command.
Now here's the weird part. If I run the script manually from the command line, it works fine. New files are downloaded and processed. However, if the Task Scheduler runs the script, no new files are downloaded. The script is running under the same user, but yet yields different results.
According to the log files created by the script and on the linux box, the script successfully logs into the linux box. However, no files are downloaded despite there being new files. Again, when I run it via the command line, files are downloaded.
Any ideas? suggestions, alternative methods?
Thanks.
You can use the windows Task Scheduler, but make sure the "optional" field "Start In" is filled in.
In the Task Scheduler app, add an action that specifies your python file to run "doSomeWork" and fill in the Start in (optional) input with the directory that contains the file.. So for example if you have a python file in:
C:\pythonProject\doSomeWork.py
You would enter:
Program/Script: doSomeWork.py
Start in (optional): C:\pythonProject
I had the same issue when trying to open an MS Access database on a Linux VM. Running the script at the Windows 7 command prompt worked but running it in Task Scheduler didn't. With Task Scheduler it would find the database and verify it existed but wouldn't return the tables within it.
The solution was to have Task Scheduler run cmd as the Program/Script with the arguments /c python C:\path\to\script.py (under Add arguments (optional)).
I can't tell you why this works but it solved my problem.
I'm having a similar issue. In testing I found that any type of call with subprocess stops the python script when run in task scheduler but works fine when run on the command line.
import subprocess
print('Start')
test = subprocess.check_output(["dir"], shell=True)
print('First call finished')
When run on command line this outputs:
Start
First call finished
When run from task scheduler the output is:
Start
In order to get the output from task scheduler I run the python script from a batch file as follows:
python test.py >> log.txt
I run the script through the batch file both on command line and through task scheduler.
Brad's answer is right. Subprocess needs the shell context to work and the task manager can launch python without that. Another way to do it is to make a batch file that is launched by the task scheduler that calls python c:\path\to\script.py etc. The only difference to this is that if you run into a script that has a call to os.getcwd() you will always get the root where the script is but you get something else when you make the call to cmd from task scheduler.
Last edit - start
After experiments... If you put there full path to python program it works without highest privileges (as admin). Meaning task settings like this:
program: "C:\Program Files\Python37\python.exe"
arguments: "D:\folder\folder\python script.py"
I have no idea why, but it works even if script uses subprocess and multiple threads.
Last edit - end
What I did is I changed task settings: checked Run with highest privileges. And task started to work perfectly while running python [script path].
But keep in mind, that title contains "Administrator: " at the begining... always...
P.S. Thanks guys for pointing out that subprocess is a problem. It made me think of task settings.
I had similar problem when one script is running from Windows Task Scheduler, and another one doesn't.
Running cmd with python [script path] didn't work for me on Windows 8.1 Embedded x64. Not sure why. Probably because of necessity to have spaces in path and issue with quotes.
Hope my answer helps someone. ;)
Create a batch file add your python script in your batch file and then schedule that batch file .it will work .
Example : suppose your python script is in folder c:\abhishek\script\merun.py
first you have to go to directory by cd command .so your batch file would be like :
cd c:\abhishek\script
python merun.py
it work for me .
Just leaving this for posterity: A similar issue I faced was resolved by using the UNC (\10.x.xx.xx\Folder\xxx)path everywhere in my .bat and .py scripts instead of the letter assigned to the drive (\K:\Folder\xxx).
I had this issue before. I was able to run the task manually in Windows Task Scheduler, but not automatically. I remembered that there was a change in the time made by another user, maybe this change made the task scheduler to error out. I am not sure. Therefore, I created another task with a different name, for the same script, and the script worked automatically. Try to create a test task running the same script. Hopefully that works!
For Anaconda installation of python in windows below solution worked for me
create a batch file with below
"C:\Users\username\Anaconda3\condabin\activate" && python "script.py" &&
deactivate
Setup task to run this batch file

Categories

Resources