Load three different modules which have the same name

A

abcd

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\playground.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:\ok> python
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
 
A

abcd

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
 
C

Carsten Haese

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.Person()
beta_person = beta.Person.Person()
gamma_person = gamma.Person.Person()

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.
 
S

Steve Holden

abcd said:
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
 
A

abcd

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_Alpha"
or something like that in sys.modules? not sure how I might do that.

Thanks!
 
G

Gabriel Genellina

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_Alpha"
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.
 
S

Steve Holden

abcd said:
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_Alpha"
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
 
B

Ben Finney

abcd said:
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()
 
A

Alex Martelli

Gabriel Genellina said:
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,importing sys and assigning to the entry:

sys.modules['veryweird'] = extremely_tricky_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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top