[Tutor] command prompt HOWTO (long)

Stephen L Arnold sarnold@earthling.net
Mon, 4 Jun 2001 20:18:11 -0700


Howdy:

Judging from the lone response I got, I guess I've been expunged 
from the list, consigned to the bit-bucket, redirected to /dev/null 
8-o

Here it is, but it needs some work (some of the longer lines may 
get wrapped in transit).  I'm open to suggestions...

Python Command Prompt mini-HOWTO

Steve Arnold  06-03-2001
sarnold@earthling.net

Introduction

This is an introduction to running Python programs from the command
prompt in both DOS/Windows and X-Windows environments. You should be
familiar with using a basic text editor, creating and saving files,
knowing where they go when you save them, etc. Detailed info on
these topics can be found elsewhere on the web (and is beyond the
scope of this document). However, we will discuss some editing tips,
setting up your environment, and various ways to run Python
programs and capture the output.

The DOS Command Prompt

Most of my win32 experience is with Windows 9x and a little bit of NT4.
The main difference is that NT has a more useful NT command prompt,
as well as the DOS box, where you can actually set the size of the
scroll buffer (among other things), but they are very similar as
well. NT allows setting user-specific environment stuff, but 9x
requires a login profile on a server for this. We'll assume the
Windows box is not connected to a network in this case (except for a
dial-up Internet connection).

Setting up Your Environment

All programs run inside an environment of some kind, and in this
case there are both global settings and some that are specific to
the DOS-shortcut you start from. The easiest place to put your
global environment settings is in your autoexec.bat file. A DOS
batch file is just a text file (with a .bat extension) that contains
a sequence of DOS commands to run, and autoexec.bat is a special
batch file that runs when DOS starts up (and before Windows starts).
You should find autoexec.bat in the root of your C:\ drive (this
file was more widely used back in the Windows 3x days, so you may
not even have one). If you don't have one, just create one with a
text editor.

Tip: The basic Windows text editor is called Notepad.exe and is
really not good for much at all (it can't even load or work with
files bigger than 64k). Although you can use something like Word or
Wordpad to edit text files, I wouldn't recommend it. If you don't
already have some kind of programmer's editor, then here's the best
free one (and it's way better than Notepad):

http://www.winsite.com/info/pc/win95/misc/pfe101i.zip

It's called Programmer's File Editor, and although it's no longer
under development, the file above can be found on numerous ftp
archives. The website for PFE used to be here:

http://www.lancs.ac.uk/people/cpaap/pfe/

If you already have an autoexec.bat file, then just add some of the
settings below. Here is an example file, with each line explained
below (most non-Python stuff has already been removed):

************************  begin C:\autoexec.bat  ***********************

@C:\UTILS\NAV\NAVDX.EXE /Startup
SET PATH=%PATH%;C:\DOS_STUF;C:\USR\GNAT\BIN;C:\PYTHON20
SET ADA_INCLUDE_PATH=c:\usr\gnat\asis;c:\usr\gnat\POSIX\src;c:\usr\gnat\Booch\components
SET ADA_OBJECTS_PATH=c:\usr\gnat\asis;c:\usr\gnat\POSIX\src;c:\usr\gnat\Booch\components
SET HOME=C:\HOME\STEVE
c:\windows\command\doskey.com dird=dir /ad $*

************************  end C:\autoexec.bat  ***********************

Line 1: @C:\UTILS\NAV\NAVDX.EXE /Startup

This line is for Norton Antivirus for Win95. If you don't have it,
don't worry (but you should probably have some form of virus
protection).

Line 2: SET PATH=%PATH%;C:\DOS_STUF;C:\USR\GNAT\BIN;C:\PYTHON20

This line sets the path, which is where Windows looks for programs
to run when you type the program name at a command prompt. The
%PATH% is a variable that means "whatever the path was already set
to". The default path for Windows 9x is usually
"C:\windows;C:\windows\command" (notice that semicolons are used to
separate entries in the path) so my complete path would be:

PATH=C:\WINDOWS;C:\WINDOWS\COMMAND;C:\DOS_STUF;C:\USR\GNAT\BIN;C:\PYTHON20

To see what your path is, open a DOS prompt and type "set", which
will show all your DOS environment variables. The first two entries
above are where Windows and DOS programs reside, the DOS_STUF
directory is where I put some additional DOS programs, next is where
the GNU Ada compiler programs reside, and finally the DOS Python
tools. You need this if you want to call the Python interpreter from
anywhere on your hard drive.

Lines 3 & 4:

SET ADA_INCLUDE_PATH=c:\usr\gnat\asis;c:\usr\gnat\POSIX\src;c:\usr\gnat\Booch\components
SET ADA_OBJECTS_PATH=c:\usr\gnat\asis;c:\usr\gnat\POSIX\src;c:\usr\gnat\Booch\components

Extra junk telling the Ada compiler where to find things (this
should be phased out soon and stored in the registry instead).

Line 5: SET HOME=C:\HOME\STEVE

This is a handy way to set a home directory (where you can keep your
personal Python files, etc). It also helps with some programs that
look for this variable.

Line 6: c:\windows\command\doskey.com dird=dir /ad $*

If you want to have a command history in your DOS box, i.e., where
you can use the Up-arrow key to recall previous commands, then you
need to load this program (it comes with Windows).

So, unless you have other settings already in your autoexec.bat
file, the basic settings for running Python programs would be:

SET PATH=%PATH%;C:\PYTHON20
SET HOME=C:\HOME\STEVE
c:\windows\command\doskey.com dird=dir /ad $*

And don't forget to download the PFE editor above. If you installed
Python to a different directory than the default shown above, then
change it accordingly.

The default DOS prompt shortcut is on your Start Menu under Start ->
Programs -> MS-DOS Prompt. This is a special type of Windows
shortcut that points to command.com, and allows you to set several
other custom parameters for a given DOS session. I'd recommend
making your own shortcut for Python; you might want to put it with
the other Python menus (right-click on the Start menu and click
Explore). Once you've started your Python-DOS box, select the
Properties button on the toolbar at the top of the window. You
should see several tabs of configuration options, some of which can
make things more convenient. You may have noticed that your DOS box
opened in the C:\Windows directory (if it takes over your whole
screen, then press Alt-Tab to make a window). You can change this to
point to wherever you keep your working Python files. For example, I
would make a directory under my home directory above, such as:

C:\HOME\STEVE\SRC\PYTHON

Then on the Program tab in the MS-DOS Prompt Properties, I would
enter the above path for the "Working" directory. That way, this
particular DOS shortcut will always open in my Python source
directory. Feel free to use the Change Icon button to give your DOS
box a more appealing Python-esque icon (there are several .ico files
in the main Python directory). You don't really need to change
anything else, however, you may want to increase the Initial
Environment size on the Memory tab to at least 1024. You can also
play with the options on the Font tab to change the size of the
window/fonts. However, you can't increase the size of your scroll
buffer, so if the text output of your program scrolls off the top of
your screen, about the only options you have are to pipe the output
to the More command, or redirect it to a file (which we'll cover in
just a minute).

Running Python Programs

Start your customized DPS prompt and type "set" to check your
environment. You should see something like the following:

TMP=C:\WINDOWS\TEMP
TEMP=C:\WINDOWS\TEMP
PROMPT=$p$g
winbootdir=C:\WINDOWS
COMSPEC=C:\COMMAND.COM
ADA_INCLUDE_PATH=c:\usr\gnat\asis;c:\usr\gnat\POSIX\src;c:\usr\gnat\Booch\components
ADA_OBJECTS_PATH=c:\usr\gnat\asis;c:\usr\gnat\POSIX\src;c:\usr\gnat\Booch\components
HOME=C:\HOME\STEVE
PGPPATH=C:\INTERNET\PGP
TZ=PST8PDT
DIRCMD=/O
BLASTER=A220 I5 D0 P300
windir=C:\WINDOWS
PATH=C:\USR\GNAT\BIN;C:\WINDOWS;C:\WINDOWS\COMMAND;C:\DOS_STUF;C:\PYTHON20

Including the C:\PYTHON20 directory in your path allows Windows to
find both the DOS and win Python interpreters. For text-mode
programs, you would call the PYTHON.EXE interpreter like this:

C:\home\steve\src>python which.py pythonw.exe
\PYTHON20\pythonw.exe

The which.py program implements a version of the 'which' command
that looks for a program in your PATH (in this case PYTHONW.EXE). If
your program has more output than will fit on one screen, then you
can pipe it (the pipe symbol is above your backslash key) to the
More command, which will let you page down through the output one
screen at a time:

python which.py pythonw.exe|more

The other option is to redirect the output to a file (which will end
up in the current directory unless you specify a path):

python which.py pythonw.exe > output.txt

Starting PYTHON.EXE with no arguments will launch the Python
interpreter. If you double click on PYTHONW.EXE, you will see that
nothing (apparently) happens. PYTHON.EXE is the console version of
Python, and is useful when you want to see what Python writes to the
standard output. PYTHONW.EXE is the non-console version of Python,
and does not show a console window. You would normally use it to run
Python programs which create their own windows, programs running as
server processes, etc.

If you want to run a GUI program, then you would use PYTHONW.EXE
(you would probably want to write a short DOS batch file to start
your program). Most Python GUI programs you'll find floating around
require the TCL/Tk package (Tkinter under Windows) for creating the
windows, buttons, etc, however, more people are starting to use the
GTK-based WXPython package. Both TCL/Tk and WXPython programs should
run under both Linux/X-Windows and MS-Windows, provided you have all
required packages installed. More info on this as available on the
Python and ActiveState web sites:

http://www.python.org/

http://www.ActiveState.com/index.html

The Linux Command Prompt

One of the major differences between DOS/NT and Linux command
prompts (or shells) is that Linux/UNIX shells provide full-featured
scripting languages to use within the shell environment. There are
also several different shells to choose from such as the Bash shell,
C shell, and Korn shell. Most Linux distributions seem to default to
Bash, but you can always choose another. The syntax varies from one
shell to the next (although they seem to fall into two main camps)
so we will use the Bash shell syntax in the following examples.

Setting up Your Environment

Your Linux or UNIX environment should already be setup for you (for
the most part). You can see your environment variables as before, by
typing the 'set' command. You should see something like this:

[sarnold@ptolemy sarnold]$ set
BASH=/bin/bash
BASH_ENV=/home/sarnold/.bashrc
BASH_VERSINFO=([0]="2" [1]="04" [2]="11" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='2.04.11(1)-release'
COLORS=/etc/DIR_COLORS
COLUMNS=103
DIRSTACK=()
DISPLAY=:2.0
EUID=501
GROUPS=()
HISTFILE=/home/sarnold/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/sarnold
HOSTNAME=ptolemy
HOSTTYPE=i386
IFS=' 
'
INPUTRC=/etc/inputrc
KDEDIR=/usr
LANG=en_US
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=62
LOGNAME=sarnold
LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37...
MACHTYPE=i386-redhat-linux-gnu
MAIL=/var/spool/mail/sarnold
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/home/sarnold/bin
PIPESTATUS=([0]="0")
PPID=13536
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/home/sarnold
QTDIR=/usr/lib/qt-2.2.0
REMOTE_HOSTNAME=rama.arnolds.bogus
REMOTE_IPADDRESS=192.168.0.7
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:histexpand:monitor:history:interactive-comments:emacs
SHLVL=4
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
TERM=xterm
TZ=PST8PDT
UID=501
USER=sarnold
USERNAME=sarnold
VNCDESKTOP=X
WINDOWID=8388622
_=PATH
i=/etc/profile.d/which-2.sh
langfile=/home/sarnold/.i18n
sourced=1
mc=() 
{ 
    mkdir -p ~/.mc/tmp 2>/dev/null;
    chmod 700 ~/.mc/tmp;
    MC=~/.mc/tmp/mc-$$;
    /usr/bin/mc -P "$@" >"$MC";
    cd "`cat $MC`";
    /bin/rm "$MC";
    unset MC
}

As you can see, there's much more information here than in the 
previous example. Don't worry if you don't know what it all means 
(I certainly don't) but you can check your path to make sure it has 
the right directories in it. One slight difference though: DOS will 
look in the current directory for programs first, then the path, 
whereas Linux will not look in the current directory unless you 
explicitly add it to your path using the '.' notation (which is not 
a very good idea). If you want a place to put your working Python 
scripts (as well as shell scripts) then you should make a bin 
directory under your home directory (as above) and then add it to 
your path. To modify your personal path setting, as well as other 
shell settings, then you should edit your .bash_profile in your 
home directory (it starts with a '.' so it's hidden, like the DOS 
Hidden file attribute). Here is my modified .bash_profile:  

[sarnold@ptolemy sarnold]$ cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin
BASH_ENV=$HOME/.bashrc
USERNAME="sarnold"

export USERNAME BASH_ENV PATH

Notice that in Bash syntax, you must first SET the variables, then
EXPORT them. Your Linux distribution should have come with Python
already installed, so you would probably find the Python interpreter
in /usr/bin. Use the 'which' command to locate it (you could also
try the 'locate' command, but you might have to create the locatedb
first if your machine doesn't run all night) like so:

[sarnold@ptolemy sarnold]$ which python
/usr/bin/python

You can run your python programs exactly the same way as under
Windows, except there's no distinction between running a text-mode
program or a GUI program. Of course, you need to be running X to run
most GUI programs, but that's beyond the scope of this HOWTO.