Python and Windows
Finally figured out how to run a Python program as a batch file from Windows. Well, sort of.
I’ve been learning about algorithms and as part of that adventure I had to wade into Python programs so I’ve been learning Python as well.
The problem stems from the way I downloaded Python as per Algorithms for Dummies by John Paul Mueller and Luca Massaron. What I downloaded was a programming environment called Anaconda which included something called Jupyter notebook. Sometimes I used the interactive program iPython but when reviewing algorithms provided by the authors, I would go into Jupyter rather than the interactive program.
When going through the algorithms book and the programs that came with the book, I found that I really needed to learn Python to better understand how the programs worked, so I bought a book Automate the Boring Stuff by Al Sweigart. I’ve been learning Python through that book and developing a few practice programs using the Anaconda/Jupyter environment. Then I got to the part in the Automate book where the author discusses running your programs as a batch job through Windows rather than gearing up the developer environment every time you want to run a program.
That’s where I got stuck in trying to make it work. The Automate book doesn’t really utilize the Anaconda environment; I think it uses the Mu environment.
Right at that moment, the hurricane season really geared up – Laura was barreling down my way and then Sally threatened us and now we have various storm systems churning in the Atlantic. The one in the Gulf is making me nervous – I’ll have to watch that one over the weekend.
The anxiety over this climate change is making it hard to focus on the various things I’m trying to do, so I’m deploying, or trying to deploy, various strategies to help me maintain focus on certain things. The coronavirus and the climate change together is just making me so tired.
Anyway, yesterday, I finally figured parts of it out. I used two sources to help me figure it out: Techendo and the Automate book.
Here are a couple of things to do in order to run Python programs developed in Anaconda/Jupyter environment as a batch program in the Windows environment.
Cmd Environment and the Python Interpreter
First, I would suggest getting familiar with the cmd environment; this is the environment that has a black background. Type in the search box on the bottom left for “cmd” and you will see the app for “cmd”.
Click on that app and the cmd environment should open up.
Generally, according to either Techendo or the book, I forget which, the cmd environment will start off with C:\Users\username>. The above image is a good example. Type in “python” and see if you get the following:
If you end up with >>>, then you are now in the Python interpreter where you can run Python commands interactively.
For me, I either didn’t have the interpreter or it was not in the right spot, so Microsoft immediately sent me to its store to download a free version of the Python interpreter. I trusted Microsoft so I went ahead and downloaded it, even though there was a possibility of the interpreter residing somewhere else on my computer. My computer probably already did have it but maybe not in a way I needed it.
Once I got the Python interpreter saved by Microsoft in the right spot (or at least in the right spot for me), next I went searching for where Microsoft looks to run programs. As best as I can understand it, when Microsoft is asked to run a program, it goes looking in certain areas which the book and Techendo call the PATH environment. Apparently, all programs and batch files need to reside in the PATH environment before Microsoft will run the program.
Your PATH Environment
To figure out what your machine’s PATH environment is, type ‘echo %PATH%’ in the black cmd environment. I went ahead and copied my list of paths for future reference to make sure that wherever I store the programs, they reside in one of my listed PATH environments.
Saving Your Jupyter Notebook as .py Rather than .ipynb.
Next, the Jupyter environment apparently saves your programs as a notebook with extension ending as .ipynb rather than .py, but the program needs to be saved as .py in order for the interpreter to recognize it. If you are in the Jupyter environment and the screen looks like the following, then drill down further into your program before you can save it as a .py.
You want to be able to see:
Here you can do File – Download as – Python.
Once you’ve downloaded the program as a .py file, you then have two choices as to how you can run the program (you can also run it in Jupyter; I’m just describing alternate methods put out by the Automate book): 1) either in the cmd environment; or 2) in Windows as a batch file.
Running Python Programs in cmd Environment
For the cmd environment, you might have to first create a folder (here, I’m following instructions from Techendo) in one of conputer’s PATH environment. I created “pythonscripts” folder in the original path that cmd always open with when I start the cmd app: C:\Users\veron\. I placed my Python program called “testCommandLine.py” in this new folder.
Then in the cmd environment, I changed the directory path from C:\Users\veron\ to C:\Users\veron\pythonscripts using the command “cd pythonscripts”. At this point I am now ready to run the program by using the command “python testCommandLine.py”.
Running Python Programs as a Batch Job in Windows
Here’s where things kind of got screwy.
First create a batch file in Notepad with the following program instructions:
@python3 C:\Users\veron\pythonscripts\testCommandLine.py %*
@pause
Save this snippet as a batch file with the .bat extension. I named that batch file as pythonScript.
Note the path in the program includes pythonscripts folder; however, saving the batch file in that folder didn’t work, but when I saved the batch file outside of that folder, the program worked. So I’m a little confused. Obviously, more work on understanding what is going on is necessary. It could be that the Python program needs to be in the pythonscripts folder, as per the batch program, but the batch program itself cannot be in the pythonscripts folder. I have to play around some more.
I saved the batch file in C:\Users\veron and had the Python program in C:\Users\veron\pythonscripts.
Then finally, to kick things off, I keyed WIN-R to pull up the box for running programs out of Windows:
After keying in the batch filename pythonScript, the black cmd environment will open up with the results of running the program.
In Conclusion
Which is the best method? Running from Jupyter, running from the cmd environment or running as a batch file in Windows? I don’t know. I think each person is going to have to decide for himself but at least we know we have various options. By learning these options, I learn new things about running programs on my Windows computer and they may come in handy in the future.
You must be logged in to post a comment.