Python is easy?

Bengt Richter bokr at
Thu Aug 15 01:35:31 CEST 2002

On 12 Aug 2002 19:48:06 -0700, jdriller at (Jonathan Driller) wrote:

>A Python evangelist keeps telling me I need to try Python as it is so
>much quicker and easier to develop in then Java. Bottom line is I
>think the ease of learning is dependent on the quality of your
>teacher/support than wholly on the language.
>I keep getting this error using ActiveStates win2k install 2.2:
 ^^^-- this prompt means you are being prompted by Python for interactive
       input. You are not talking to a windows shell (which would look
       at the .py extension and check if that meant anything special to it).
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>NameError: name 'test1' is not defined
Python thought you were giving it the name of some object named 'test1' If you
had accidentally given it a name it knew about, e.g., 'abs' (which is a builtin
function) and you had typed "" then it would have found abs and looked
for a 'py' attribute, and you would have had a different error message ;-)

>I am trying to run which is in
>import urlopen
>page = urlopen('')
How you go about running depends on what context you are in.
Above you were in the interactive Python-prompted context, meaning python
is already running and reading what you type line by line and trying to
execute what you type as soon as it makes a complete chunk (single expression,
simple statement, or a multiline thing).

if you typed
in that context, python should read that line, go read the file, and act as if
you had typed the lines in the file one by one right there in that context.
(Notice the r prefix on the string, which preserves the backslashes in the string
instead of sometimes combining them with the next character to make a control or
special characters).

Another thing you could do is type
    import test1
which would also execute, but in a different context, resulting in a module
named test1, and 'page' would get defined inside the module's name space as an
attribute. In your immediate context, 'test1' would be defined, but not 'page'.
You could then access page as

For the import to succeed, python must be able to find the file. You can specify
places for it to look, and it will also look in the working directory that python
was given as default working directory when it was started. So if you start
a "DOS" window and cd to a directory of interest, and _then_ type python (or
D:\Python22\python or wherever you have python.exe if you haven't got PATH
set up with that directory in it), then you should be able to import any
in that directory by typing import xxx (without the .py).

>I figure it is my pythonpath but wrote this path.bat to set it:
>path d:\Python22;%PATH%
>set PYTHONPATH=.;d:\Python22\Lib;d:\Python22\Lib\lib-tk;d:\Python22\Lib\site-packages\jds;d:\Python22\Lib\site-packages
It might be ok already. You were already in Python.

But I would suggest you set up a convenient place to do your experiments etc, away from the
actual python install tree, so you don't accidentally go in there and change something without
realizing it. E.g., I would not recommend D:\Python22 as the place to accumulate various test
code snippets.

Instead, make a new tree for your projects, e.g., C:\mypy or d:\pystuff or such. Then make a new
shortcut to run "DOS" (cmd.exe probably) with that directory as default directory. You can just
copy the shortcut in the start menu that currently starts "DOS" and put the copy in a more convenient
place in the start menu under a name that reflects what you are doing, e.g., "mypy". Then edit
the properties of the shortcut to make the working directory C:\mypy or whatever you chose.

This will then get you conveniently to a "DOS" command prompt with your Python playpen directory
right there. Type "dir" and cmd.exe will show you the contents, etc. Type "sol" and get to solitaire
faster than all that mouse bs. Or type calc and get the calculator thing. You may get to like launching
things in one step by name ;-)

But the reason you are there is to type "python" ;-)
That _should_ start an interactive session and get you the ">>> " prompt. If it doesn't, there's
several ways to deal with it. If you want systemwide knowledge of python's whereabouts, change the
system path. See start>settings>control panel>system>environment or your equivalent, and add
;D:\python22 (or your equivalent) to the end of the path variable's definition string. On NT
you would have a choice of whether to do this for the system (if you're admin) or just for the current user.

Another way to deal with this is do it just for the particular short cut, by adding parameters
to the cmd.exe in the short cut target, e.g., I have

%SystemRoot%\system32\cmd.exe /x /k prompt [$T$H$H$H$H$H$H] $P$G& d:\vc98\bin\vcvars32.bat & home.cmd & title C++

To run the "DOS" window I most often use. It doesn't put python in the path (it's already there the other way),
but it does a bunch of setup (as an example which you could vary to set PATH etc.):
1) it sets the prompt, 2) it sets up the environment needed to run VC++60 compiler/linker from the
command line if I like, 3) home.cmd is something I use to remember my last working directory and/or
set it conveniently, so I can start up wherever I was last, and 4) it puts a title in the title bar.

The /x enables extensions, and the /k makes the window remain after it does 1-4. I could add
&& D:\python22\python.exe if I wanted to start python automatically also, but I have another shortcut
for that, with no C++ stuff. You can substitute whatever in the place of d:\vc98\bin\vcvars32.bat & home.cmd
to set the path and whatever else you like, as customization just for this particular shortcut.

If you change path via control panel, you should not have to reboot ;-) But the if you have a "DOS"
shell running you will have to restart that, most likely, before it will see the new PATH (type PATH to see).

>Can anyone assist here? My evangelist can't figure it out...

Once you have this set up, you can work from the "DOS" window to edit sources with whatever editor
suits you (type notepad or vim, or whatever. But if you use notepad, make sure you select
all as the file type or you may save Vim is less annoying. Emacs is also available for win32).

Anyway, in the "DOS" window, you can type
to "run" -- which is really running python to compile and run, then exiting back
to "DOS" if exits.

Typing python withou args gives you the interactive prompt.

If you have the file associations set up, you can also type
and the windows shell will wind up running python, with one important difference:
The stdin and stdout won't be set up to connect with redirected or piped i/o (this may have
been fixed for some new windows, but not NT and I don't think w2k). It's not a Python thing,
it's a general associated-interpreter problem. Perl will have the same problem (which monstrous
hacks have been invented to fight).

The easiest thing to do if you want a particular python script to act like an executable with
normally redirectable i/o is to create an xxx.cmd file with @/path1/python /path2/ in it
to run python explicitly. (Specify full path1& path2 for best context independence). Then you
can type xxx args <infile | another_prog >outfile or whatever.

IDLE is also nice, giving highlighted text etc. But running from a plain "DOS" window
avoids all that mousing stuff to exit and reenter. Of course, you could set up an idle.cmd
so you could just type idle to start it ;-)

HTH. I'm sure you will enjoy Python a lot once you get past these little preliminaries ;-)

Bengt Richter

More information about the Python-list mailing list