[Tutor] Modules and Python tutorial by S. Thurlow - opinions please

Steven D'Aprano steve at pearwood.info
Sat Aug 20 17:51:07 CEST 2011

Lisi wrote:
> I have got myself well and truly bogged down.  I need to change the angle from 
> which I am looking.  I only have a short while left for now in which to make 
> some sort of progress with Python.  What do people think of:
> http://www.sthurlow.com/python/
> Is it reasonably accurate and therefore useful?

As far as I can tell after a lightning fast read of it (about three 
minutes to skim a few of the lessons), it seems perfectly fine to me.

> To show you what I mean:
> How to save and run a bash script:
> Write your script
> save it in the normal manner
> chmod to x for everyone you want to be able to execute it.  (E.g. where owner 
> is root: perhaps 744)
> Either move the file into a directory on your path, or add the directory that 
> the file is in to your path.
> It will now run.
> Can anyone point me to a similar set of basic instructions for Python 
> modules??

More or less exactly the same, except you need a hash-bang line at the 
top of the script so that the shell knows that it is Python and not a 
shell script. So you can add a line like:


at the VERY TOP of your script (it MUST be in the first line for the 
shell to recognise it).

(This is exactly the same practice for all scripting languages, 
including Perl, Bash, Ruby, and many more... if the hash-bang line is 
missing, the shell will try to execute the file with "sh".)

As an alternative, you can call the script from the shell prompt like this:

$ python path/to/my/script.py

(naturally you don't type the $ that is just the shell prompt)

Finally, you can put your script somewhere in the PYTHONPATH and then 
call it like this:

$ python -m script

Note that this way you leave the .py off the end -- you're not telling 
Python to *run* a file, but to *import* a file, which happens to run it 
as a side-effect. I don't recommend you use this technique until you're 
more comfortable with Python. I mention it only for completeness.


More information about the Tutor mailing list