Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > on package import, have it conditionally import a subpackage

Reply
Thread Tools

on package import, have it conditionally import a subpackage

 
 
Gabriel Rossetti
Guest
Posts: n/a
 
      09-19-2009
Hello everyone,

I'd like to ba able to import a package and have it's __init__
conditionally import a subpackage. Suppose that you have this structure :

mybase/
mybase/__init__.py
mybase/mypkg
mybase/mypkg/__init__.py
mybase/mypkg/module0.py
mybase/mypkg/type1
mybase/mypkg/type1/__init__.py
mybase/mypkg/type1/module1.py
mybase/mypkg/type1/module2.py
mybase/mypkg/type2
mybase/ mypkg/type2/__init__.py
mybase/ mypkg/type2/module1.py
mybase/ mypkg/type2/module2.py

I'd like to do something like th following in my code

>>> import mybase
>>> mybase.setType("type1")
>>> from mybase.mypkg import module0, module1, module2
>>> module0.__file__

'mybase/mypkg/module0.pyc'
>>> module1.__file__

'mybase/mypkg/type1/module1.pyc'
>>> module2.__file__

'mybase/mypkg/type1/module2.pyc'

but I can't figure out how to do this.

Thank you,
Gabriel

 
Reply With Quote
 
 
 
 
ryles
Guest
Posts: n/a
 
      09-20-2009
On Sep 19, 4:06*pm, Gabriel Rossetti <(E-Mail Removed)>
wrote:
> Hello everyone,
>
> I'd like to ba able to import a package and have it's __init__
> conditionally import a subpackage. Suppose that you have this structure :
>
> mybase/
> mybase/__init__.py
> mybase/mypkg
> mybase/mypkg/__init__.py
> mybase/mypkg/module0.py
> mybase/mypkg/type1
> mybase/mypkg/type1/__init__.py
> mybase/mypkg/type1/module1.py
> mybase/mypkg/type1/module2.py
> mybase/mypkg/type2
> mybase/ mypkg/type2/__init__.py
> mybase/ mypkg/type2/module1.py
> mybase/ mypkg/type2/module2.py
>
> I'd like to do something like th following in my code
>
> *>>> import mybase
> *>>> mybase.setType("type1")
> *>>> from mybase.mypkg import module0, module1, module2
> *>>> module0.__file__
> 'mybase/mypkg/module0.pyc'
> *>>> module1.__file__
> 'mybase/mypkg/type1/module1.pyc'
> *>>> module2.__file__
> 'mybase/mypkg/type1/module2.pyc'
>
> but I can't figure out how to do this.
>
> Thank you,
> Gabriel


You might try something like the following:

$ export PYTHONPATH=/home/ryles


$ cat mybase_test.py

import mybase

mybase.type = "type2"

from mybase.mypkg import module0, module1, module2

print module0.__file__
print module1.__file__
print module2.__file__


$ python mybase_test.py
/home/ryles/mybase/mypkg/module0.pyc
/home/ryles/mybase/mypkg/type2/module1.pyc
/home/ryles/mybase/mypkg/type2/module2.pyc


$ ls -l mybase/
-rw-r--r-- 1 ryles None 44 Sep 20 16:59 __init__.py
drwxr-xr-x+ 4 ryles None 0 Sep 20 17:06 mypkg


$ cat mybase/__init__.py

default_type = "type1"

type = default_type


$ ls -l mybase/mypkg/
-rw-r--r-- 1 ryles None 307 Sep 20 17:06 __init__.py
-rw-r--r-- 1 ryles None 0 Sep 20 16:51 module0.py
drwxr-xr-x+ 2 ryles None 0 Sep 20 17:02 type1
drwxr-xr-x+ 2 ryles None 0 Sep 20 17:02 type2


$ ls -l mybase/mypkg/type1/
-rw-r--r-- 1 ryles None 0 Sep 20 16:49 __init__.py
-rw-r--r-- 1 ryles None 0 Sep 20 16:49 module1.py
-rw-r--r-- 1 ryles None 0 Sep 20 16:51 module2.py


$ ls -l mybase/mypkg/type2/
-rw-r--r-- 1 ryles None 0 Sep 20 16:48 __init__.py
-rw-r--r-- 1 ryles None 0 Sep 20 16:51 module1.py
-rw-r--r-- 1 ryles None 0 Sep 20 16:49 module2.py


$ cat mybase/mypkg/__init__.py

# These modules are hard-coded.
from mybase.mypkg import module0

# These modules are resolved dynamically.
from mybase import type
for module_name in ["module1", "module2"]:
full_name = "mybase.mypkg." + type + '.' + module_name
globals()[module_name] = __import__(full_name, {}, {},
[full_name])

$
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multiprocessing in subpackage on windows Darren Dale Python 0 04-20-2011 10:20 PM
Conditionally subclassing based on Import David Pratt Python 0 10-03-2008 06:45 AM
Creating a subpackage of a built-in module? Derek Peschel Python 0 08-23-2006 09:56 AM
Re: subpackage import problem Eric Huss Python 0 08-25-2005 02:51 AM
subpackage import problem Eric Huss Python 0 08-21-2005 07:07 AM



Advertisments