Python language basics 20: running a Python file from the command line

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:

Python executable in the Python installation folder

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 environment variables window

Open the “Edit the system environment variables” link and click the “Environment Variables…” button towards the bottom of the System Properties window:

Open environment variables with button

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…”:

Edit path

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:

Python command line

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:

Starting number guessing game from command line

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.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: