Importing different versions of a module

Fredrik Lundh fredrik at pythonware.com
Mon Jul 21 17:55:41 EDT 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.

if you already have two different versions of the script, what stops you 
from making changes to them?

> For example, suppose the development script is in ~/dev/rss.py, and the 
> production script is in ~/prod/rss.py.  I want the dev version to import 
> /usr/lib/python2.5/site-packages/lib_dev/parse.py, and the prod version 
> to import usr/lib/python2.5/site-packages/lib_prod/parse.py.

cannot you just insert the appropriate directory in sys.path the first 
thing you do in the scripts?  e.g.

     import os, sys

     lib = "lib_dev" # change this for prod/rss.py

     sys.path.insert(0,
         os.path.join(
             os.path.dirname(os.__file__), "site-packages", lib
         ))

     import parse # picks the right one

</F>




More information about the Python-list mailing list