Load three different modules which have the same name

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

    #1

    Load three different modules which have the same name

    I have the following directory structure setup...

    c:\alpha\Person .py
    ------------------
    class Person(IPerson) :
    def __init__(self):
    print "Alpha person here"

    c:\beta\Person. py
    ------------------
    class Person(IPerson) :
    def __init__(self):
    print "Beta person here"

    c:\gamma\Person .py
    ------------------
    class Person(IPerson) :
    def __init__(self):
    print "Gamma person here"

    c:\ok\playgroun d.py
    -------------------
    def tryAllThree():
    a = "c:\\alpha"
    b = "c:\\beta"
    g = "c:\\gamma"

    sys.path.append (a)
    from Person import Person
    alpha = Person()

    sys.path.remove (a)
    sys.path.append (b)
    from Person import Person
    beta = Person()

    sys.path.remove (b)
    sys.path.append (g)
    from Person import Person
    gamma = Person()

    Notice that my three different projects (alpha, beta, gamma) have a
    Person.py which contains a Person class.

    When I execute the following (on windows):

    c:\okpython
    >>from playground import *
    >>tryAllThree ()
    I see...
    Alpha person here
    Alpha person here
    Alpha person here

    What I want to see is
    Alpha person here
    Beta person here
    Gamma person here

    .....how can I get this to work?

    Thanks

  • abcd

    #2
    Re: Load three different modules which have the same name

    nevermind this took care of it:

    import sys

    def tryAllThree():
    a = "c:\\alpha"
    b = "c:\\beta"
    g = "c:\\gamma"

    sys.path.append (a)
    import Person
    alpha = Person.Person()

    sys.path.remove (a)
    sys.path.append (b)
    reload(Person)
    beta = Person.Person()

    sys.path.remove (b)
    sys.path.append (g)
    reload(Person)
    gamma = Person.Person()

    thanks

    Comment

    • Carsten Haese

      #3
      Re: Load three different modules which have the same name

      On Mon, 2007-03-19 at 11:42 -0700, abcd wrote:
      nevermind this took care of it:
      >
      import sys
      >
      def tryAllThree():
      a = "c:\\alpha"
      b = "c:\\beta"
      g = "c:\\gamma"
      >
      sys.path.append (a)
      import Person
      alpha = Person.Person()
      >
      sys.path.remove (a)
      sys.path.append (b)
      reload(Person)
      beta = Person.Person()
      >
      sys.path.remove (b)
      sys.path.append (g)
      reload(Person)
      gamma = Person.Person()
      That sort of works, but it's really unclean.

      I suggest you turn each directory that contains an implementation of
      Person into a package. That can be done by simply putting an __init__.py
      file into each of those directories. This __init__.py file can be empty
      or only contain a "pass" statement, but as long as it's there, the
      directory containing it becomes a package.

      Then, add the directory that contains your packages (C:\ in your
      example) to your path, and you can import and use your Person modules
      like this:

      import alpha.Person, beta.Person, gamma.Person
      alpha_person = alpha.Person.Pe rson()
      beta_person = beta.Person.Per son()
      gamma_person = gamma.Person.Pe rson()

      The main advantages are that the different implementations of Person
      don't shadow each other in your name space, and you don't gratuitously
      force module reloads.

      Hope this helps,

      Carsten.


      Comment

      • Steve Holden

        #4
        Re: Load three different modules which have the same name

        abcd wrote:
        nevermind this took care of it:
        >
        import sys
        >
        def tryAllThree():
        a = "c:\\alpha"
        b = "c:\\beta"
        g = "c:\\gamma"
        >
        sys.path.append (a)
        import Person
        alpha = Person.Person()
        >
        sys.path.remove (a)
        sys.path.append (b)
        reload(Person)
        beta = Person.Person()
        >
        sys.path.remove (b)
        sys.path.append (g)
        reload(Person)
        gamma = Person.Person()
        >
        thanks
        >
        Blerch! Why not just call the modules by the right names in the first
        place? Then each will have its own sys.modules entry for a start ...

        regards
        Steve
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC/Ltd http://www.holdenweb.com
        Skype: holdenweb http://del.icio.us/steve.holden
        Recent Ramblings http://holdenweb.blogspot.com

        Comment

        • abcd

          #5
          Re: Load three different modules which have the same name

          Blerch! Why not just call the modules by the right names in the first
          place? Then each will have its own sys.modules entry for a start ...
          >
          regards
          Steve
          what do i need to do? also, is there a way I can load each one as I
          have but each one have its own unique entry in sys.modules? For
          example i could load Person from Person (in alpha) as, "Person_Alp ha"
          or something like that in sys.modules? not sure how I might do that.

          Thanks!

          Comment

          • Gabriel Genellina

            #6
            Re: Load three different modules which have the same name

            En Tue, 20 Mar 2007 07:40:53 -0300, abcd <codecraig@gmai l.comescribió:
            >Blerch! Why not just call the modules by the right names in the first
            >place? Then each will have its own sys.modules entry for a start ...
            >
            what do i need to do? also, is there a way I can load each one as I
            have but each one have its own unique entry in sys.modules? For
            example i could load Person from Person (in alpha) as, "Person_Alp ha"
            or something like that in sys.modules? not sure how I might do that.
            Use the "as" clause when importing; it's almost the same phrase you wrote
            above:
            from alpha.Person import Person as Person_Alpha
            or something like that.
            alpha should be a package, as someone already said.

            --
            Gabriel Genellina

            Comment

            • Steve Holden

              #7
              Re: Load three different modules which have the same name

              abcd wrote:
              >Blerch! Why not just call the modules by the right names in the first
              >place? Then each will have its own sys.modules entry for a start ...
              >>
              >regards
              > Steve
              >
              what do i need to do? also, is there a way I can load each one as I
              have but each one have its own unique entry in sys.modules? For
              example i could load Person from Person (in alpha) as, "Person_Alp ha"
              or something like that in sys.modules? not sure how I might do that.
              >
              Thanks!
              >
              The easiest way to proceed is simply to call the .py files by different
              names - then they automatically get theor own sys.modules entry.

              [sys.modules is a dictionary where you can look modules up by name. it's
              helpful to ensure each module gets its own entry, and the presence of
              the entry is how the system avoids unnecessary reloading of modules].

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC/Ltd http://www.holdenweb.com
              Skype: holdenweb http://del.icio.us/steve.holden
              Recent Ramblings http://holdenweb.blogspot.com

              Comment

              • Ben Finney

                #8
                Re: Load three different modules which have the same name

                "abcd" <codecraig@gmai l.comwrites:
                nevermind this took care of it:
                >
                import sys
                >
                def tryAllThree():
                a = "c:\\alpha"
                b = "c:\\beta"
                g = "c:\\gamma"
                >
                sys.path.append (a)
                import Person
                alpha = Person.Person()
                To avoid this confusion, follow PEP 8
                <URL:http://www.python.org/dev/peps/pep-0008/>; in particular, name
                your classes with TitleCase and name your modules (i.e., name the
                files that contain the code) with lowercase.

                import person
                alpha_person = person.Person()

                As to the original question, you've already seen the answer:

                from alpha import person
                alpha_person = person.Person()

                from beta import person
                beta = person.Person()

                from gamma import person
                gamma_person = person.Person()

                --
                \ "If I ever get real rich, I hope I'm not real mean to poor |
                `\ people, like I am now." -- Jack Handey |
                _o__) |
                Ben Finney

                Comment

                • abcd

                  #9
                  Re: Load three different modules which have the same name

                  thanks for the help.

                  Comment

                  • Alex Martelli

                    #10
                    Re: Load three different modules which have the same name

                    Gabriel Genellina <gagsl-py2@yahoo.com.a rwrote:
                    ...
                    example i could load Person from Person (in alpha) as, "Person_Alp ha"
                    or something like that in sys.modules? not sure how I might do that.
                    >
                    Use the "as" clause when importing; it's almost the same phrase you wrote
                    above:
                    from alpha.Person import Person as Person_Alpha
                    or something like that.
                    alpha should be a package, as someone already said.
                    Right, but the as clause does NOT affect sys.modules -- the entry in
                    sys.modules will still be sys.modules['alpha.Person']. I doubt this
                    matters, but since the OP had very specifically askd about "in
                    sys.modules" I thought it better to clarify.

                    If you want to make fake entries in sys.modules you need to do that
                    explicitly,impo rting sys and assigning to the entry:

                    sys.modules['veryweird'] = extremely_trick y_stuph

                    The only purpose of that is to "fool" future "import veryweird"
                    statements (executed under any circumstances) to use said extremely
                    tricky stuph. NOT recommended unless you know what you're doing.


                    Alex

                    Comment

                    Working...