importing modules with the same name but from different directories

logistix logistix at cathoderaymission.net
Sun Apr 20 22:42:51 EDT 2003


> -----Original Message-----
> From: python-list-admin at python.org 
> [mailto:python-list-admin at python.org] On Behalf Of Mirko Koenig
> Sent: Sunday, April 20, 2003 9:26 PM
> To: python-list at python.org
> Subject: importing modules with the same name but from 
> different directories
> 
> 
> Hi
> 
> The subject is a little bit long but i didn't how how to 
> describe it shortly.
> 
> I hope my english is good enough to explain my problem.
> 
> I have a program consisting of many modules. Every modul has 
> its own directory in a directory called modul, eg.
> 
> .../modul/modul1
> .../modul/modul2
> 
> In every modul directory is a diretory for the language 
> files. These files are used to display the modules label in 
> different languages. eg.
> 
> .../modules/module1/language/german.py
> .../modules/module1/language/english.py
> .../modules/module2/language/german.py
> .../modules/module2/language/english.py
> 
> My problem is:
> If is start modul1 from within its modul directory ( 
> .../modules/modul1 ) And modul1 calls modul2, then modul2 
> doesn't load its own language file. Instead it loads the 
> langugae file of modul1.
> 
> In modul1 i wrote sys.path.append( "./languages" ) then 
> import german. In modul2 i wrote the same. I know in that way 
> it coulnd't work.
> 
> What can i do, that every modul knows its 'homedir', no 
> matter from where i start it ?
> 
> I don't want to set environment variables, because the 
> programm is for beginners, that are not used to pc's and want 
> an easy going programm and not to set env vars.
> 
> I'm developing under linux if that help solving my problem
> 
> thanx for comments
> 
> Mirko Koenig
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 

Add an __init__.py file to each directory in your project.  This makes
the directories packages.  Then you can use a fully qualified path such
as "import modules.modul1.language.english as module1_english".  Note
that you can't use the same name for different modules at runtime, so
use "as xxx" to give them a more distinct name.






More information about the Python-list mailing list