[Tutor] my own site-package

Branimir Petrovic BranimirP at cpas.com
Wed Oct 6 20:35:27 CEST 2004


> 
> In your case, as Kent said, all you need to do is move your 
> module1.py 
> out of the subfolder and put it directly into site-packages.  
> Then you 
> can simply 'import module1' and not worry about having an 
> __init__.py or 
> whether you're injecting unnecessary names into your namespace. :)
> 

To add another "angle" to this thread - I personally dislike disturbing
sacredness of Python's site-packages. Instead I tend to place my project
related modules in its own "Lib" folder right next to my "main script".
For instance say I have MyScript.py that is using (undisclosed) number
of custom (MyScript.py that is) related modules. I'd typically create 
structure like this:

	MyPyProject	(folder)
		|
		+- MyScript.py (this is the "main guy")
		|
		+- Lib (folder)
		    |
		    +- MyModule1.py
		    +- MyModule2.py
			...


# FileName: MyScript.py

import sys, os

def addLibPath():
    """Add current script path and its Lib folder to system path"""
    os.chdir(os.path.dirname(sys.argv[0]))
    scriptPath, libPath = os.path.dirname(sys.argv[0]),
os.path.abspath(r'..\Lib')
    sys.path.extend([scriptPath, libPath])


if __name__=='__main__':
    addLibPath()            # know where to find your own library modules

    # The rest of your code referencing (your own) project related modules 
    # comes here:

    # ...


Of course - you can put your modules right next to Python's own, but
to me at least this somehow does not "sound" right nor proper...


Branimir
                            


More information about the Tutor mailing list