importing two modules with the same name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Francisco Borges

    #1

    importing two modules with the same name

    Hello,

    This is not stricly necessary but it would be nice if I could get it
    done. Here is what I want to do:

    There are 2 "foo" named modules, 'std foo' and 'my foo'. I want to be
    able to import 'my foo' and then from within my foo, import 'std
    foo'. Anyone can help??

    ---------

    Before you start calling me stupid... the reason I would like that is
    that:

    I have a module that produces shell completion code from optparse
    objects for zsh, tcsh and (sort of) bash. But to use it, I have to
    directly modify the python code. Which I don't want that since I want to
    be able to generate completion automatically for code which is not mine
    in an easy manner.

    So a shell script sets the PYTHONPATH in a way that "my optparse" is
    loaded, but to build the option parser I want to then import the
    original optparse module from my optparse.py file.

    I could simply copy optparse's code and hack it or I could simply import
    it and overload some methods, which is what I think would be a cleaner
    solution.

    Cheers,
    --
    Francisco.
    Groningen, Nederlands.
    __o
    `\<,
    _____(*)/(*)_____

  • Mike Meyer

    #2
    Re: importing two modules with the same name

    Francisco Borges <borges@let.rug .nl> writes:
    [color=blue]
    > I could simply copy optparse's code and hack it or I could simply import
    > it and overload some methods, which is what I think would be a cleaner
    > solution.[/color]

    So why aren't you willing to give your optparse module a different name?

    <mike
    --
    Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

    Comment

    • Tim Jarman

      #3
      Re: importing two modules with the same name

      Francisco Borges wrote:
      [color=blue]
      > Hello,
      >
      > This is not stricly necessary but it would be nice if I could get it
      > done. Here is what I want to do:
      >
      > There are 2 "foo" named modules, 'std foo' and 'my foo'. I want to be
      > able to import 'my foo' and then from within my foo, import 'std
      > foo'. Anyone can help??
      >[/color]

      If I had to do this, I'd use packages:

      import foo # the standard module - presumably already on sys.path
      import my.foo # my module which lives in its own little world

      Remember to qualify names correctly, and/or do something like:

      import foo as std_foo

      But if your foo is under your control, why not do everyone a favour and call
      it something else?

      --
      Website: www DOT jarmania FULLSTOP com

      Comment

      • El Pitonero

        #4
        Re: importing two modules with the same name

        Francisco Borges wrote:[color=blue]
        > There are 2 "foo" named modules, 'std foo' and 'my foo'. I want to be
        > able to import 'my foo' and then from within my foo, import 'std
        > foo'. Anyone can help??[/color]

        In other words, you would like to make a "patch" on third-party code.
        There are many ways to do it. Here is just one possible approach.

        #--------- A.py: third-party module
        x = 3
        def f():
        return 'A.f(): x=%d' % x
        #--------- B.py: your modifications to the module
        def f():
        return 'B.f(): x=%d' % x
        #--------- Main.py: your program
        import imp
        # load the third party module into sys.modules
        imp.load_source ('A', '', open('C:\\A.py' ))
        # load and execute your changes
        imp.load_source ('A', '', open('C:\\B.py' ))
        # import now from memory (sys.modules)
        import A
        print A.f()

        Comment

        • El Pitonero

          #5
          Re: importing two modules with the same name

          Tim Jarman wrote:[color=blue]
          > But if your foo is under your control, why not do everyone a favour[/color]
          and call[color=blue]
          > it something else?[/color]

          His case is a canonical example of a patch. Often you'd like to choose
          the "patch" approach because:

          (1) the third-party may eventually incorporate the changes themselves,
          hence you may want to minimize changes to the name of the module in
          your code, so one day in the future you may simply remove the patch, or

          (2) you are testing out several ideas, at any point you may want to
          change to a different patch, or simply roll back to the original
          module, or

          (3) your product is shipped to different clients, each one of them
          requires a different twist of the shared module, or

          (4) your program and your data files are versioned, and your program
          structure needs cumulative patches in order to be able to work with all
          previous data file versions.

          These types of needs are rather common.

          Comment

          • Francisco Borges

            #6
            Re: importing two modules with the same name

            El Pitonero wrote:[color=blue]
            > #--------- Main.py: your program
            > import imp
            > # load the third party module into sys.modules
            > imp.load_source ('A', '', open('C:\\A.py' ))
            > # load and execute your changes[/color]

            Thanks a bunch for the hint. The imp module did take care of the job,
            but just to mention load_source seems to be deprecated, so I used

            fp, mpath, desc = imp.find_module ('optparse',a)
            s_opt = imp.load_module ('std_optparse' , fp, mpath, desc)

            again, thanks for the help!

            Cheers,
            Francisco

            Comment

            Working...