Re: import multiple modules with same name



Christian Bird wrote:
Is it possible to import multiple modules with the same name from
different locations? I'm using two different pieces of software, both
of which have a module named util.py. I know that I can modify
sys.path to fix which util gets imported when I do:

import util

I'd like to be able to do something like:

import sys
sys.path.append("/somedir1/")
import util as util1
sys.path.insert(0, "/somedir2/")
import util as util2

But it appears that once python imports a module, it will never look
for a module with the same name again. Is there any way to get around
this? I'd rather not rename either of the util modules as other
pieces of software use them (but the others never use both of them of
course) and I don't want to break them or have multiple copies of the
code in different named files. I'm appreciative of anyone's ideas.

-- Chris

If your two "pieces of software" are in fact Python packages (i.e., the directories have files named __init__.py),
then you can import each this way:

from package1 import util as util1
from package2 import util as util2

If they are not packages, then they should be, and you can make them so by creating empty files named __init__.py in each directory.
.



Relevant Pages

  • Re: MD5 Checker where to get?
    ... sort. ... Does MS make such util? ... "Microsoft File Checksum Integrity Verifier" ... packages like this, is sometimes they need libiconv and libintl, ...
    (microsoft.public.windowsxp.general)
  • Re: Several packages in one file, no Export possible?
    ... I have several packages together in one file and now I would like "use" ... and does not search in the executed file, ... package Util; ...
    (perl.beginners)