I am trying to set up a windows scheduled task to run a python script that runs two other scripts. I use os package to do it with the code like below:
#mainScript.py
import os
os.system('script1.py 1')
os.system('scripts.py 1')
The script works in a scheduled task except when I add the above code. What I mean If use script1.py in my bat file it works, but if I use the mainScript.py it gives me the error code.
'script1.py' is not recognized as an internal or external command,
operable program or batch file.
'script2.py' is not recognized as an internal or external command,
operable program or batch file.
I know I can just move all the code into mainScript but wonder if there is anything I can do to have.
You need to specify wich program will run the script.
Try this :
os.system('python script1.py')
Related
I am a newbie to python and would like to seek some advice. I having a script now where the function of this script can only be executed after I run a command i.e. python run trial.py. However, I would like to automate this process by using a subprocess function in a new python file called 'run.py' to run the trial.py script.
After that, I would wish to convert run.py file into an exe file to ease the execution for other users.
I have tried to perform the below steps.
1. saved the scripts (trial.py & run.py) in same directory.
2. Execute both of the files in same conda virtual environment.
3. Successfully execute run.py by using ```subprocess.run('xxx run trial.py')```
4. Converted the run.py into an exe file
5. Tried to execute the exe file and it is running, but **failed** to show the output that suppose to be appeared after running trial.py.
Would like to seek advice is any steps on above did wrongly or need to be improvised? I need to deal with confidential data hence the easiest way I can do is by using pyinstaller to allow another user to execute.
Hope to hear some advice
UPDATE
I had tried to use the codes below,
import subprocess
import sys
from subprocess import PIPE, STDOUT
command ='python run trial.py'
run = subprocess.Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
Now the exe file is able to be generated and able to run, but it doesn't appear the expected output. It ended without any error message. It works well when I run the script in python..
Wish to hear advice from all of you..
With subprocess, you can try it:
command = 'Your command' # Example: command = 'python trial.py'
process = subprocess.Popen(command, shell=True, stdout=sys.stdout, stderr=sys.stderr)
I would wish ... to ease the execution for other users.
As I've understood your case, using subprocess and making an exe file out of your python file (which is not an easy task) is not a good fit for you.
Instead, I recommend you to have a look at make files as they are well-known for simplifying your commands.
for example you can have a make file like this:
run:
python trial.py
And users can simply run make run, and python trial.py will run instead.
The possibilities are endless.
You can also make a bash file that is an executable,
# !/bin/bash
python trial.py
And it will simply run like exe files.
I want to use google API using python by Anaconda prompt.
I have followed these steps but it is giving me the error 'GOOGLE_APPLICATION_CREDENTIALS' is not recognized as an internal or external command,
operable program or batch file.
I have created the APIKEY.json file perfectly but in anaconda prompt GOOGLE_APPLICATION_CREDENTIALS=APIKEY.json is not working.
I have followed these steps.
Created a Service account and run all the steps on anaconda prompt.
changed the path for the APIKEY.json file.
run GOOGLE_APPLICATION_CREDENTIALS=APIKEY.json this line.
The last command is giving me the error 'GOOGLE_APPLICATION_CREDENTIALS' is not recognized as an internal or external command, operable program, or batch file.
You need to set the environment variable
from terminal like this
export GOOGLE_APPLICATION_CREDENTIALS='/path/to/your/APIKEY.json'
or from your code like this
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/your/APIKEY.json"
I have a powershell script that I'd like to run as a SQL Agent Job.
It contains 3 lines:
Invoke-SqlCmd...
Copy-Item...
python...
The first 2 lines work flawlessly, however the job fails on the third line in which I call some python.
The powershell script works fine when run manually from the powershell ISE. I assume, and this appears to be the case from some googling that the SQL Agent doesn't like or can't run python on its own, however I would have assumed that since its all part of a powershell script I would get around that problem.
The SQL job error is:
Executed as user: DOMAIN\SQL_ReportServer. A job step received an error at line 3 in a PowerShell script. The corresponding line is 'python StripQuotes.py "E:\DW_Exports\Pearsonvue\CDD.txt"'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. '. Process Exit Code -1. The step failed.
Is there anyway to get this process working?
The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program.
python is not visible in the PATH for the context of the SQL Server Agent or powershell proxy account. Try invoking python using the fully qualified pathname (e.g. C:\Python37\python.exe). You may encounter further errors which you should then debug as new and separate problems.
In a directory I have three files.
test.py, containing print "Hello World"
python_runner.bat, containing
python test.py
PAUSE
jython_runner.bat, containing
jython test.py
PAUSE
The python_runner.bat works as expected, but running Jython_runner.bat causes the PAUSE command to be skipped!
Why is Jython causing the batch script to be prematurely terminated?
(NOTE: I am using Jython2.7b4, I haven't tried with Jython 2.5)
If the jython command is a batch script then the pause and any thing after this will not be executed.
try call
call jython test.py
What is the outcome?
I am trying to execute/call a python script that resides in another directory.
My Problem: When I attempt to open/call the file I get the error
'..' is not recognised as an internal or external command, operable
program or batch file
My python code to execute the python file is:
os.system("../test.py abc")
I have also tried this but I get the same error on a DIFFERENT part of the string:
os.system(os.getcwd()+"/../test.py abc")
# results in "c:/users/jim/work products/python/testdir/../test.py abc"
Error:
'c:/users/jim/work ' is not recognised as an internal or external command, operable
program or batch file
In windows you should use '..\\executeablename' to run a program or script in the parent directory, not '../' the unix style.
And, to ensure the script can run property, a 'python' is better to be added before the command.
So I think this situation should be:
os.system("python ..\\test.py abc")
It has not been tested since I am using linux, you can just try.
BTW, 'os.system' is kinda deprecated and the subprocess module is recommend to use when execute system level command.
A .py file isn't an executable, so that won't work on Windows. You have to run it with python.exe:
import subprocess
import sys
subprocess.call([sys.executable, '../test.py', 'abc'])
You could do this with system, but I figure this is easier because you don't have to quote the filename.