Breaking a single python .py file into multiple files with inter-dependencies -


i want split large python module wrote multiple files within directory, each file function may or may not have dependencies other functions within module. here's simple example of came with:

first, here's self contained .py module

#[/pie.py] def getpi():     return pi()  def pi():     return 3.1416 

obviously, works fine when importing , calling either function. split in different files init.py file wrap up:

#[/pie/__init__.py] getpi import * pi import * __all__=['getpi','pi']  #[/pie/getpi.py] def getpi():     return pi()  #[/pie/pi.py] def pi():     return 3.1416 

because getpi() has dependency pi(), calling structured raises exception:

>>> import pie >>> pie.getpi()  traceback (most recent call last):   file "<pyshell#7>", line 1, in <module>     pie.getpi()   file "c:\python\pie\getpi.py", line 2, in getpi     return pi() nameerror: global name 'pi' not defined 

and fix issue, current solution write init.py so:

#[/pie/__init__.py] import os _os  __all__ = [] _f in  _os.listdir(__path__[0]):     if not _f == '__init__.py' , _f.endswith('.py'):         execfile('%s\\%s'%(__path__[0],_f))         __all__.append(_os.path.splitext(_f)[0]) 

so works fine:

>>> import pie >>> pie.getpi() 3.1416 

so works if contained in single .py file. init.py can contain high level imports (numpy, os, sys, glob...) individual functions need.

structuring module way feels "right" me. new functions loaded automatically @ next import (no need append init.py each time). lets me see @ glance functions meant used looking @ what's within directory, plus keeps nicely sorted alphabetically.

the negative can see @ time init.py gets byte-compiled , not of sub .py files. loading speed hasn't been issue don't mind. also, realize might cause issue packaging, it's don't mind because our scripts distributed via our own revision control system.

is acceptable way of structuring python module? , if not, correct way achieve i've done properly.

the "correct" way import necessary modules needed:

# pi.py def pi(): return 3.1417  # getpi.py .pi import pi def getpi(): return pi()  # __init__.py .pi import * .getpi import * 

make sure don't have cyclic dependencies. these bad in case, can avoid them abstracting necessary level.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -