Python language basics 20: running a Python file from the command line
June 13, 2015 Leave a comment
Introduction
In the previous post we looked at how for-loops are constructed in Python using the range function. They are in fact practically the same as foreach-loops as we iterate through a range of integers and we perform something with each integer. However, that is the closes approximation of the more traditional integer-based for-loops of other programming languages like Java:
for (int i = 0; i < 10; i++) { }
In this post we’ll stop looking at language features for a moment and instead check how to start a Python program from the command line.
Python executable
We’ve been working in the PyCharm environment to write our Python code. However, you could just as well open Notepad and write Python code there. The main thing to note is that Python source files have the file extension ‘.py’, like “helloworld.py”, “customers.py” etc. If you save your Python source code as a ‘py’ file then you can instruct Python to execute it for you.
Open a command line window, type “python” and press Enter. If Python is configured correctly then you should see the following response:
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (In
tel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
The date and version may differ but that’s not important. If, however, you get the following error message…
‘python’ is not recognized as an internal or external command, operable program or batch file.
…then it means that the operating system cannot find any executable (.exe) file called Python. That’s easy to fix.
In the very first post in the series we installed Python on Windows. In my case it was installed in the c:\Python34 folder. This folder contains an executable file called python:
When we typed “python” in the command window then it was this executable we wanted to run but Windows didn’t find it. We need to modify the environment variables for Windows. I have Windows 8.1 so all the screenshots are from that OS but the goal is in any case to open the Environment Variables window. Open the Control Panel and search for “environment”:
Open the “Edit the system environment variables” link and click the “Environment Variables…” button towards the bottom of the System Properties window:
The Environment Variables window will open. You’ll see the System variables in the bottom section. Scroll down to the variable called Path and click “Edit…”:
Assuming that you have installed Python in the c:\python34 folder put the following string in the very beginning of the Path value:
C:\Python34\;C:\Python34\Scripts;
Make sure you don’t miss the closing semi-colon. Click OK on every window until all of them disappear.
Now Windows will check those folders and look for a file called python when you type it in the command prompt. In fact the command will open the Python command line where you can directly type Python code:
Press Ctrl+C to exit the command line.
If you followed along the number guessing code demo in this post then you should have that source file somewhere. I have mine in a file called NumberGuessing.py in the c:\PythonProjects\HelloWorld folder. I then have to navigate to that folder in the command prompt and issue the following command:
python NumberGuessing.py
…and the game can begin:
That was fairly easy right? We didn’t have to perform any steps before running our Python source code.
In the next post we’ll look at how to write functions in Python.
Read all Python-related posts on this blog here.