I am trying to run a batch file that will open the CMD and start a python script. I nee Windows 10 to start this at 4 am in the morning and know I have to use the Task Scheduler.
My problems seems to be that when I start the batch file the CMD opens up and goes to the correct directory where the python script is but nothing happens.
I am calling the python script from my batch files as "python file.py". when I manual type this into the CMD everything works perfectly but I can not figure out how to start the python scrip from within the batch file to be executed within the CMD.
starts cmd /k "c: && cd\users\ME\Documents"
python file.py
Any help is appreciated.
Thanks
Whenever I run python scripts using batch files I format it like this:
"C:\Users\...\python.exe" "C:\Users...\file.py"
The first command is the location of your python executionable, and the second is the location of the script you want to run.
An optional extra is to include
pause
at the end of your batch file to ensure the cli remains open.
Related
I have been searching the internet up and down for a solution to this. I understand there is SublimeREPL which I can easily use to properly run python code in sublime text 3. However, it's not sufficient for me. I want to run my python file on the cmd from sublime text 3.
There are many reasons for me. The biggest reason is that i need to test my python files often and I don't want to cramp my sublime text workspace with thousands of tabs. There are several other reasons but at the moment I just want a solution. Almost everywhere on the internet it only talks about SublimeREPL, so I have been unable to find a solution.
I also understand I can use the cmd manually by going to the file directly and running it there, but its a pain in the back to keep switching to cmd and then to sublime text over and over again.
Therefore, I am looking for a neat solution where I can run my python file from sublime text 3 in cmd. Any help is deeply appreciated.
I am using python 3.7.3
The rule of thumb for build systems in Sublime Text is that if you can craft a command line that, when executed manually in the terminal/console, will give you the effect that you want (and that command doesn't require interactive input), then you can turn it into a build system.
In this case, what you want to do is spawn a new cmd window and do something inside of it; the fact that you're using Sublime is thus not interesting in the grand scheme of knowing how to do that, which might be why your search didn't turn up any results.
In Windows, you can use a terminal command like cmd /s /c something to tell the Windows cmd.exe that it should execute the command something. In your case you want to use Python to execute a program, so that might look something like the following to get Python to execute my_file.py.
cmd /s /c python my_file.py
However if you do that in an existing command prompt, the result is just to run the program in the current window; i.e. you're telling cmd to run a command, but it's still running in the current window, which is not what you want.
In order to run the program in a new window, you need to use the start command, which launches a new program. The general format of that would be something like this:
start "" cmd /s /c python my_file.py
That tells start to launch a new instance of cmd, which you're telling to run the program, so now the Python program is running in its own window.
There are some general issues with this; the biggest one is that as soon as cmd finishes executing the command you give it, it quits. Similarly, Python also quits when the program is finished. The result of that is that as soon as your program is finished, the window immediately vanishes before you can see what it did.
You could add -i to the python command to get it to go interactive, but then you'd have to interact with the internal python REPL in order to get it to quit and close the window, which it sounds like you don't want to have to take the step to do.
In that case you need to modify the command you give to cmd to get it to also wait until you press a key.
All told, an example of that might look something like the following as a complete sublime-build file:
{
"shell_cmd": "start \"\" cmd /s /c \"python -u \"$file\" & pause\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
}
This is a version of the internal Python.sublime-build modified to extend the command as outlined above. In order for this to work, you need to be able to enter python at a terminal and have it launch the Python interpreter. If that's not the case you need to adjust your PATH environment variable as appropriate.
If you're using Python 3, you may need to replace python with python3 or similar in the command so that cmd knows what to execute.
I am currently using crontab to run a SH script at boot which navigates to the path of my python script, switches to a different python environment and runs my python script, although it works perfectly fine it runs hidden without a terminal for me to monitor whatever the python interpreter prints like errors, how could I make it so the python interpreter points at a newly opened terminal window?
Here is my SH script (runs with the bash interpreter, not sh):
#!/bin/sh
cd /
cd /home/pi/Desktop/Juvia-py
source defenv/bin/activate
python3 juvia.py &
and my crontab entry:
#reboot bash /home/pi/launcher.sh
Thank you
If you just want to record errors, you could pipe STDOUT and STDERR to files, something like
python3 juvia.py >stdout.log 2>stderr.log &
But if you wanted to open it in a separate window so you could interact you would need to manage STDIN more creatively.
I was hoping for some advice on using the subprocess module.
I'm trying to run a bash job within a python script so my bash command (in the right directory) is: ./program myjob.inp
This is just running the executable "program" with myjob.inp being the input file (and my python script constantly updates myjob.inp).
I know that if I just wanted to run "program", I could do something like:
with open("tmp.dat","w") as fstore_tmp:
subprocess.call(["./program"], stdout = fstore_tmp)
However, I can't figure out how to run the job taking in the input file myjob.inp such that it's doing the equivalent of ./program myjob.inp. I tried:
with open("tmp.dat","w") as fstore_tmp:
subprocess.call(["./program", "myjob.inp"], stdout = fstore_tmp)
However, that doesn't seem to be working. Does anyone have any suggestions? Thanks!
there is a process running that will write some output to a set of files. I wrote a python script that will make a copy of these output files in another directory. Right now I can simply run the python script when I notice the other process is done. How can I get this script to be run automatically when the other process is done?
I don't have control over the other process's source code. Messing with the source code might make the results file inadmissible, so I'd rather not touch it at all.
I am running Python 2.7.1 in an Ubuntu 11.x machine.
You don't tell much about what is the program running before the Python script, but if it is or you can convert to a shell script, you can use this syntax:
$ first-script.sh && python-script.sh
The && operator means that if the first script finished successfully, run the second afterwards.
Of course, you could invoke the python interpreter with your script directly as the 2nd script. Here I assume that it is wrapped in a Bash script.
To get the script to run in terminal, I have to select the option to open in terminal and write sh script name.shIs there a way I can reduce that to a single step, i.e. a launcher that automatically opens the script in a terminal after logging as a root ? I've tried to look it up on Google, but I haven't found any useful advice (perhaps I'm not executing the search properly).
I think what you mean is running your script as start up script. In that case place the script you want to run in the /etc/init.d directory and make the script executable with command chmod 755 scriptname.sh.
See the below related threads for more information
https://askubuntu.com/questions/290099/how-to-run-a-script-during-boot-as-root
How to run a shell script at startup
EDIT:
if you want to run your script after your login is successful then you need to place your script in ~/.bash_profile. See this related post
How do you run a script on login in *nix?