[Tutor] Problem with file()

Alan Gauld alan.gauld at blueyonder.co.uk
Sat Aug 21 23:46:20 CEST 2004


> > And naming a file __init__.py is probably a bad idea since
anything
> > with two underscores usually signifies something from the innards
> > of Python itself.

> There is no file called ClientsManager.py. There is however a folder
of that
> name, placed in the Lib folder. I added the folder path to the
Python paths.

OK, What you have done is create a package not a module.
The __init__.py is indeed a bit of Python magic that should really
only be used for doing initialisation of the package (setting global
values etc) and is rarely used in my experience (it just needs to
exist since that's what tells Python that it's dealing with a
package!)

Normally what you'd do is

a) Just create a file called ClientsManager.py located in a folder
in your sys.path list. This thus becomes a module that you can import.

Or,

b) If it's a significant project with multiple related modules you
might
create a folder with a descriptive name and place an empty __init__.py
file in the folder - thus making the folder a package. Then put all
your
modules (xxx.py, yyy.py etc) in there. You can then import your files
with:

import MyPackage.MyModule1, MyPackage.MyModule2

from MyPackage import MyModule3

etc.

What you have done works because you are importing the package name
which magically executes the __init__.py file. And by putting your
code in the __init__.py file it does appear to function like a module.
But that's extra work for you, more diskspace used, harder to
distribute
to others(coz they need to create a folder instead of just copy a
file)
and harder to maintain because all your code will wind up in files
called __init__.py and most text editors will list filenames only
not full paths...

But thanks for the remninder because its so rare for me to create
packages in Python that I'd forgotten all about __init__.py until
you mentioned you created a folder... :-)

HTH,

Alan G.



More information about the Tutor mailing list