Importing different versions of a module

norseman norseman at
Fri Jul 18 23:51:32 CEST 2008

mercado mercado wrote:
> I have two versions of a script on my machine. One version is for new
> development and the other version is a production version.  This script
> imports a module from a different directory, and this module again has two
> versions (a development version and a production version).  What I want is
> for the development script to import the development module, and the
> production script to import the production module, without making any
> changes to the code in the script.
> For example, suppose the development script is in ~/dev/, and the
> production script is in ~/prod/  I want the dev version to import
> /usr/lib/python2.5/site-packages/lib_dev/, and the prod version to
> import usr/lib/python2.5/site-packages/lib_prod/
> My first instinct was to place a .pth file in ~/dev that points to
> /usr/lib/python2.5/site-packages/lib_dev, and a .pth file in ~/prod that
> points to /usr/lib/python2.5/site-packages/lib_prod, but it seems that
> doesn't look at .pth files in the current working directory.  My
> next attempt was to create a symbolic link in ~/dev called,
> pointing to /usr/lib/python2.5/site-packages/lib_dev/, and a
> symbolic link in ~/prod called, pointing to
> /usr/lib/python2.5/site-packages/lib_prod/, but that didn't work
> either.
> Can anybody suggest a way to achieve my goal?  Thanks in advance.
> ------------------------------------------------------------------------
> --

Yes, but you may not like it.

I do what you do. Prod and devel subdirs.
I store the paths in a var at the top of my programs.
I change it(them) when the program graduates.


DvlpPATH=......  #usually a full hardcoded path ending with a '/'
PATH2USE=DvlpPATH   #which gets changed to FnlPATH when graduating
                     #  I keep the DvlpPATH until I'm positive I'm not
                     #  going to send it back to school

x="pgm_or_file.ext", 'r+b')

and so forth

If the there is a need, I'll hardcode to a branch point and create sub 
branch vars as needed and chain as needed.
company_one + accounting + payables + vendors
             + inventory + shelf + vendors
                         + real_property + locations

company_two + (same as above)

you get the idea

YES! You do have to change the code. But in one place only once you are 
correctly set.

norseman at hughesnet

More information about the Python-list mailing list