473,499 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\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:\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

Mar 19 '07 #1
9 1409
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

Mar 19 '07 #2
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.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.
Mar 19 '07 #3
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

Mar 19 '07 #4
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!

Mar 20 '07 #5
En Tue, 20 Mar 2007 07:40:53 -0300, abcd <co*******@gmail.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_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.

--
Gabriel Genellina

Mar 20 '07 #6
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_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
--
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

Mar 20 '07 #7
"abcd" <co*******@gmail.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

Mar 20 '07 #8
thanks for the help.

Mar 20 '07 #9
Gabriel Genellina <ga*******@yahoo.com.arwrote:
...
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.
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
Mar 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4667
by: Julia Bell | last post by:
I would like to run the same script on two different platforms. The directory in which the script(s) will be stored is common to the two platforms. (I see the same directory contents regardless...
1
3428
by: glaserp | last post by:
Hi, I am developing a Windows application with C# in Visual Stuio .NET. My application references three assemblies that are developed in another project. I have added these assemblies as...
12
6161
by: Sharon | last post by:
I’m wrote a small DLL that used the FreeImage.DLL (that can be found at http://www.codeproject.com/bitmap/graphicsuite.asp). I also wrote a small console application in C++ (unmanaged) that uses...
2
4585
by: Sascha | last post by:
Hi there, I searched carefully through the web before finally deciding to post this message, because I could not find a solution for my problem. Hopefully someone will have a hint or explanation...
7
5776
by: George Copeland | last post by:
This is a request for assistance analyzing a problem we are experiencing in our VB6 development environment. All our code is developed in VB6, and our persistance layer is SQL Server. We are...
4
1166
by: Stu | last post by:
Hi, I'm looking at making a piece of software which will have a central executable file, and a list of DLLs (kind of like Office "add-ins") that will provide different functionalities - the idea...
6
44468
by: Steve | last post by:
I'm playing with late binding and trying a very simple test to load an assembly In my "Host" application I have this code: <code> string modulePath =...
4
2198
by: Ravi Ambros Wallau | last post by:
Hi: We developed a set of ASP.NET Web Applications that never runs in stand-alone mode, but always inside a portal (Rainbow Portal). All modules are copied on that portal. My question is: load...
4
3342
by: cantatahost | last post by:
Hello, Likely this has been asked before... We have a library (in DLL form) that we distribute. The interface to the library is all C, but within the library it uses C++ in many places. ...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.