how to get the path of a module (myself) ?

Steven D'Aprano steven at
Fri Jun 5 06:28:26 CEST 2009

On Wed, 03 Jun 2009 01:00:15 +0200, Stef Mientki wrote:

> Sorry,
> but I realy don't understand the difference between the documents on my
> desk and a python file in a subpath.

Let's say you have a file called "parrot", containing some arbitrary data.

You read it with open('parrot').read(), and then you can do anything you 
want with the data inside:

If the data is a JPEG image, you can pass the data to a graphics program 
and display it.

If the data is plain text, you can manipulate the text.

If the data is Perl code, you can pass it to the Perl interpreter and run 

And if the data is Python code, you can call "exec data" and execute it.

execfile() does just that. It doesn't care that the file name you pass is 
"" instead of "parrot" -- it's just a file. You could call it 
"flooble.TIFF" and it wouldn't care:

>>> open('flooble.TIFF', 'w').write('print "FLOOBLE!!!"\n')
>>> execfile('flooble.TIFF')

On the other hand, modules are special. A module is an actual Python data 
type, like str, int, list and so forth, on more complex. All sorts of 
magic takes place when you say "import module_name". The most important 
is that, unlike execfile, an actual module object is created, complete 
with __file__ and __name__ attributes. (Usually -- built-in modules don't 
have a __file__ attribute.)

If you don't go through the import mechanism, you don't get a module.


More information about the Python-list mailing list