[Tutor] Where is sys.py?

Alan Gauld alan.gauld at btinternet.com
Mon Aug 22 18:31:41 CEST 2011

On 22/08/11 14:45, Lisi wrote:

>> I don't understand what you mean by "modules within it".
> The problem may be that I have again failed to understand.
> As I understand it, I can, should I wish to do so, write and save a subroutine
> that I wish to reuse rather than write the same subroutine out multiple
> times.

That's correct

> The book also says that one can import sys and then run the various commands
> within it.  It also appears to say that sys is a file, and I thought that it
> said sys.py existed.  Clearly, it didn't since it doesn't.

I hope that's not my tutorial you are reading? :-)
If so, you missed the bit in parentheses:

It is almost like making a copy the contents of sys.py into our program, 
like a cut n' paste operation. (it's not really like that but the 
concept is similar. In fact sys is a special kind of module that doesn't 
actually have a sys.py file, but we will ignore that for now!)

> Anyhow, I therefore thought - erroneously - that if there could be commands in
> sys, then it must be possible to store subroutines in some sort of "envelope"
> so that I could import the "envelope" and call the subroutines as I wanted
> them.  Otherwise, storing short subroutines would appear to me to be a waste
> of time and effort.  By the time one has imported the subroutine and then run
> it, and done this for every one of a number of short subroutines, it would be
> quicker and easier just to run them when needed.

Now I'm confused.
There can be functions inside a module but not commands, they are 
technically something different and only exist within the Python 
interpreter itself. And you can access those functions from the module 
after you have imported it.

I'm not sure what you mean by an envelope though?
The module is the envelope...

And I don't understand the bit about the time taken for short routines?
Do you mean the time taken to access them in terms of typing time? Or in 
terms of execution time? It is faster to import than the time it takes 
to retype a function unless the function is only a simple expression.

def foo(x): return x+1

is 22 characters. If saved in a module called foo.py it can be used with

import foo

Which is 10 characters.

and used with


which is only 4 extra characters, that's a lot less than retyping the 
definition. And if you expect to type foo. a lot then use:

from foo import foo

which is still only 19 characters, less than the definition.
So even for this trivially simple case its still faster to put foo into 
a module...

> So I wanted to look at how Python itself does it, so that I could copy it.
> a) I am clearly barking up a non-existent tree and b) the sooner I forget
> completely about the text that I was working from (I am still turning over in
> my mind the exercise I got hung up on) and focus only on Alan's, the sooner I
> am going to make some kind of progress!!

There seems to be some strange terminology creeping in but whether 
that's down to your text (looks like its not mine! :-) or just your own 
preconceptions I'm not sure.

One advantage of using mine is that you know where to find me if you 
don't understand something! :-)

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list