Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Automatic Module Discovery (More to python than to django) [RSS Feed]

#1 March 23, 2008 21:53:55

Chris H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Automatic Module Discovery (More to python than to django)


Hey there!

This has really nothing to do at all with django, beside the fact that
many of it's developers should be able to answer from the top of their
head. I couldn't think right now of any nice python community, and
signing up somewhere else just to fire a single question and probably
never ever return didn't seem like something I'd do on a sunday
evening.

For an app I'm writing, I'd love to know if it is at all possible to
do the following workflow:

* Given a list of paths, à la PYTHONPATH, go through those paths and
it's subpaths and "discover" (aka, import) all python modules found
* Loop through all of those modules searching for classes that are
subclasses of a certain class, and add their objects to a list, or:
* make the modules "execute" (I can't recall if this really happens
upon import anyways, sorry), so that I could just use a decorator to
do the add-to-a-list thing

I'm a tad lost on this subject, and I'd really appreciate any pointers
you could give me.

~ Chris
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 23, 2008 22:27:49

Christian V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Automatic Module Discovery (More to python than to django)


There's the pkg_resources module and .eggs. That might be what you're
looking for.

Try typing this in a python repl:

>>> import pkg_resources
>>> help(pkg_resources)



On 3/23/08, Chris Hoeppner <> wrote:
>
> Hey there!
>
> This has really nothing to do at all with django, beside the fact that
> many of it's developers should be able to answer from the top of their
> head. I couldn't think right now of any nice python community, and
> signing up somewhere else just to fire a single question and probably
> never ever return didn't seem like something I'd do on a sunday
> evening.
>
> For an app I'm writing, I'd love to know if it is at all possible to
> do the following workflow:
>
> * Given a list of paths, à la PYTHONPATH, go through those paths and
> it's subpaths and "discover" (aka, import) all python modules found
> * Loop through all of those modules searching for classes that are
> subclasses of a certain class, and add their objects to a list, or:
> * make the modules "execute" (I can't recall if this really happens
> upon import anyways, sorry), so that I could just use a decorator to
> do the add-to-a-list thing
>
> I'm a tad lost on this subject, and I'd really appreciate any pointers
> you could give me.
>
> ~ Chris
> >
>


--
Venlig hilsen / Kind regards,
Christian Vest Hansen.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#3 March 24, 2008 10:57:08

Chris H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Automatic Module Discovery (More to python than to django)


Hey,

pkg_resources throws an ImportError (I'm googling it up as I type), and
as for eggs, I really never got my head around them, useful as they
might seem.

I might probably just go down the easy way and have the things I want to
import on the pythonpath (maybe even add the paths "dynamically" on the
fly) and using a filename pattern to set the ones appart I want to use.

~ Chris

El dom, 23-03-2008 a las 22:27 +0100, Christian Vest Hansen escribió:
> There's the pkg_resources module and .eggs. That might be what you're
> looking for.
>
> Try typing this in a python repl:
>
> >>> import pkg_resources
> >>> help(pkg_resources)
>
>
>
> On 3/23/08, Chris Hoeppner <> wrote:
> >
> > Hey there!
> >
> > This has really nothing to do at all with django, beside the fact that
> > many of it's developers should be able to answer from the top of their
> > head. I couldn't think right now of any nice python community, and
> > signing up somewhere else just to fire a single question and probably
> > never ever return didn't seem like something I'd do on a sunday
> > evening.
> >
> > For an app I'm writing, I'd love to know if it is at all possible to
> > do the following workflow:
> >
> > * Given a list of paths, à la PYTHONPATH, go through those paths and
> > it's subpaths and "discover" (aka, import) all python modules found
> > * Loop through all of those modules searching for classes that are
> > subclasses of a certain class, and add their objects to a list, or:
> > * make the modules "execute" (I can't recall if this really happens
> > upon import anyways, sorry), so that I could just use a decorator to
> > do the add-to-a-list thing
> >
> > I'm a tad lost on this subject, and I'd really appreciate any pointers
> > you could give me.
> >
> > ~ Chris
> > >
> >
>
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#4 March 24, 2008 13:50:48

Chris H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Automatic Module Discovery (More to python than to django)


Just thinking about this...

Is there really no way to import something not being on the pythonpath?
Maybe importing the file's contents as string and stuff it through some
parser function (just ranting here, don't even know if there is such a
thing)?

What I want is make a few files execute in a certain order, and if I can
do that without messing too much with sys.path, even better. Some of
these files might even have the same name, so `import module` might not
give me all the files I want either.

Maybe the following workflow would make it possible, even though it
feels *really* hacky:

pathbk = sys.path
paths =
for p in paths:
sys.path =
f = os.path.join(sys.path, '__init__.py')
file(, 'w')
import os.path.splitext(os.path.basename(p))
os.unlink(f)
sys.path = pathbk

Basically, I'm clearing the pythonpath for each iteration through the
files I'm wanting to import, make the pythonpath be solely the folder
containing the file, create a __init__.py inside it, and import the
module. Feels hacky, yeah. I hope there's some other way to acomplish
this.

I've been reading up about setuptools. It's something my head just
doesn't want to absorb! I'll keep trying it though.

Any tips on this?

~ Chris

El dom, 23-03-2008 a las 22:27 +0100, Christian Vest Hansen escribió:
> There's the pkg_resources module and .eggs. That might be what you're
> looking for.
>
> Try typing this in a python repl:
>
> >>> import pkg_resources
> >>> help(pkg_resources)
>
>
>
> On 3/23/08, Chris Hoeppner <> wrote:
> >
> > Hey there!
> >
> > This has really nothing to do at all with django, beside the fact that
> > many of it's developers should be able to answer from the top of their
> > head. I couldn't think right now of any nice python community, and
> > signing up somewhere else just to fire a single question and probably
> > never ever return didn't seem like something I'd do on a sunday
> > evening.
> >
> > For an app I'm writing, I'd love to know if it is at all possible to
> > do the following workflow:
> >
> > * Given a list of paths, à la PYTHONPATH, go through those paths and
> > it's subpaths and "discover" (aka, import) all python modules found
> > * Loop through all of those modules searching for classes that are
> > subclasses of a certain class, and add their objects to a list, or:
> > * make the modules "execute" (I can't recall if this really happens
> > upon import anyways, sorry), so that I could just use a decorator to
> > do the add-to-a-list thing
> >
> > I'm a tad lost on this subject, and I'd really appreciate any pointers
> > you could give me.
> >
> > ~ Chris
> > >
> >
>
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#5 March 24, 2008 14:50:55

Christian V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Automatic Module Discovery (More to python than to django)


For just running another python script from within python, there's a
number of options:
+ the exec statement: >>> help('exec')
+ the execfile() function (deprecated in py3k)
+ the imp module

On 3/24/08, Chris Hoeppner <> wrote:
>
> Just thinking about this...
>
> Is there really no way to import something not being on the pythonpath?
> Maybe importing the file's contents as string and stuff it through some
> parser function (just ranting here, don't even know if there is such a
> thing)?
>
> What I want is make a few files execute in a certain order, and if I can
> do that without messing too much with sys.path, even better. Some of
> these files might even have the same name, so `import module` might not
> give me all the files I want either.
>
> Maybe the following workflow would make it possible, even though it
> feels *really* hacky:
>
> pathbk = sys.path
> paths =
> for p in paths:
> sys.path =
> f = os.path.join(sys.path, '__init__.py')
> file(, 'w')
> import os.path.splitext(os.path.basename(p))
> os.unlink(f)
> sys.path = pathbk
>
> Basically, I'm clearing the pythonpath for each iteration through the
> files I'm wanting to import, make the pythonpath be solely the folder
> containing the file, create a __init__.py inside it, and import the
> module. Feels hacky, yeah. I hope there's some other way to acomplish
> this.
>
> I've been reading up about setuptools. It's something my head just
> doesn't want to absorb! I'll keep trying it though.
>
> Any tips on this?
>
>
> ~ Chris
>
> El dom, 23-03-2008 a las 22:27 +0100, Christian Vest Hansen escribió:
>
> > There's the pkg_resources module and .eggs. That might be what you're
> > looking for.
> >
> > Try typing this in a python repl:
> >
> > >>> import pkg_resources
> > >>> help(pkg_resources)
> >
> >
> >
> > On 3/23/08, Chris Hoeppner <> wrote:
> > >
> > > Hey there!
> > >
> > > This has really nothing to do at all with django, beside the fact that
> > > many of it's developers should be able to answer from the top of their
> > > head. I couldn't think right now of any nice python community, and
> > > signing up somewhere else just to fire a single question and probably
> > > never ever return didn't seem like something I'd do on a sunday
> > > evening.
> > >
> > > For an app I'm writing, I'd love to know if it is at all possible to
> > > do the following workflow:
> > >
> > > * Given a list of paths, à la PYTHONPATH, go through those paths and
> > > it's subpaths and "discover" (aka, import) all python modules found
> > > * Loop through all of those modules searching for classes that are
> > > subclasses of a certain class, and add their objects to a list, or:
> > > * make the modules "execute" (I can't recall if this really happens
> > > upon import anyways, sorry), so that I could just use a decorator to
> > > do the add-to-a-list thing
> > >
> > > I'm a tad lost on this subject, and I'd really appreciate any pointers
> > > you could give me.
> > >
> > > ~ Chris
> > > >
> > >
> >
> >
>
>
> >
>


--
Venlig hilsen / Kind regards,
Christian Vest Hansen.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#6 March 25, 2008 12:33:20

Chris H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Automatic Module Discovery (More to python than to django)


Right this one saved me, Christian =)

Thanks a bunch-o-lot!

~ Chris

El lun, 24-03-2008 a las 14:50 +0100, Christian Vest Hansen escribió:
> For just running another python script from within python, there's a
> number of options:
> + the exec statement: >>> help('exec')
> + the execfile() function (deprecated in py3k)
> + the imp module
>
> On 3/24/08, Chris Hoeppner <> wrote:
> >
> > Just thinking about this...
> >
> > Is there really no way to import something not being on the pythonpath?
> > Maybe importing the file's contents as string and stuff it through some
> > parser function (just ranting here, don't even know if there is such a
> > thing)?
> >
> > What I want is make a few files execute in a certain order, and if I can
> > do that without messing too much with sys.path, even better. Some of
> > these files might even have the same name, so `import module` might not
> > give me all the files I want either.
> >
> > Maybe the following workflow would make it possible, even though it
> > feels *really* hacky:
> >
> > pathbk = sys.path
> > paths =
> > for p in paths:
> > sys.path =
> > f = os.path.join(sys.path, '__init__.py')
> > file(, 'w')
> > import os.path.splitext(os.path.basename(p))
> > os.unlink(f)
> > sys.path = pathbk
> >
> > Basically, I'm clearing the pythonpath for each iteration through the
> > files I'm wanting to import, make the pythonpath be solely the folder
> > containing the file, create a __init__.py inside it, and import the
> > module. Feels hacky, yeah. I hope there's some other way to acomplish
> > this.
> >
> > I've been reading up about setuptools. It's something my head just
> > doesn't want to absorb! I'll keep trying it though.
> >
> > Any tips on this?
> >
> >
> > ~ Chris
> >
> > El dom, 23-03-2008 a las 22:27 +0100, Christian Vest Hansen escribió:
> >
> > > There's the pkg_resources module and .eggs. That might be what you're
> > > looking for.
> > >
> > > Try typing this in a python repl:
> > >
> > > >>> import pkg_resources
> > > >>> help(pkg_resources)
> > >
> > >
> > >
> > > On 3/23/08, Chris Hoeppner <> wrote:
> > > >
> > > > Hey there!
> > > >
> > > > This has really nothing to do at all with django, beside the fact that
> > > > many of it's developers should be able to answer from the top of their
> > > > head. I couldn't think right now of any nice python community, and
> > > > signing up somewhere else just to fire a single question and probably
> > > > never ever return didn't seem like something I'd do on a sunday
> > > > evening.
> > > >
> > > > For an app I'm writing, I'd love to know if it is at all possible to
> > > > do the following workflow:
> > > >
> > > > * Given a list of paths, à la PYTHONPATH, go through those paths and
> > > > it's subpaths and "discover" (aka, import) all python modules found
> > > > * Loop through all of those modules searching for classes that are
> > > > subclasses of a certain class, and add their objects to a list, or:
> > > > * make the modules "execute" (I can't recall if this really happens
> > > > upon import anyways, sorry), so that I could just use a decorator to
> > > > do the add-to-a-list thing
> > > >
> > > > I'm a tad lost on this subject, and I'd really appreciate any pointers
> > > > you could give me.
> > > >
> > > > ~ Chris
> > > > >
> > > >
> > >
> > >
> >
> >
> > >
> >
>
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

  • Root
  • » Django
  • » Automatic Module Discovery (More to python than to django) [RSS Feed]

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support