Skip to content

Running Python script from a command prompt

December 9, 2009
tags:

Create the file in a text editor (I’m using Emacs).  For example, here’s a text file I called testfile.py:

#! /usr/bin/env python

print "Hello, world!"

Now we need to make the file executable.  In the command line, type:

$ chmod a+x testfile.py

Then you should be able to run it like this:

$ testfile.py

In order for this to work (running the script directly from the command line), the directory containing the script must be defined in the env PATH variable (to check path type echo $PATH).  Since this is *not* usually the case, ie, the directory you’re working in is defined as a PATH variable, the program can be run like this: ./testfile.py.  Doing this tells the interpreter to look in the current directory (./).

I used these links to help me with this stuff:

How do I make a Python script executable on unix?

Executable Python scripts

As well as Beginning Python.

Advertisements
No comments yet

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

%d bloggers like this: