python local modules -


i have several project directories , want have libraries/modules specific them. instance, might have directory structure such:

myproject/   mymodules/     __init__.py     myfunctions.py   myreports/     mycode.py 

assuming there function called add in myfunctions.py, can call mycode.py naive routine:

execfile('../mymodules/myfunctions.py') add(1,2) 

but more sophisticated it, can do

import sys sys.path.append('../mymodules') import myfunctions  myfunctions.add(1,2) 

is idiomatic way this? there mention modifying pythonpath (os.environ['pythonpath']?), or other things should into?

also, have seen import statements contained within class statements, , in other instances, defined @ top of python file contains class definition. there right/preferred way this?

don't mess around execfile or sys.path.append unless there reason it. rather, arrange code proper python packages , importing other library.

if mymodules in fact part of 1 large project, set package so:

myproject/     __init__.py     mymodules/         __init__.py         myfunctions.py     myreports/         __init__.py         myreportscode.py 

and can import mymodules anywhere in code this:

from myproject.mymodules import myfunctions myfunctions.add(1, 2) 

if mymodules code used number of separate , distinct projects, make package in own right , install whatever environment needs used in.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -