473,413 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

How to: get list of modules in a package

Is there a direct way to get a list of all the modules in a package, other
than using an os.listdir() for the directory of the package?

To be more specific, I would like to inspect every module in a package and
check if it contains the definition of a certain class. At the same time, I
would like to be able to add modules to this package in a dynamic way, so
__init__.py would not be of much help because I do not want to change it for
every new module.

Thanks,

Dan
Jul 18 '05 #1
8 6804
Dan Perl wrote:
Is there a direct way to get a list of all the modules in a package, other
than using an os.listdir() for the directory of the package?


Given the dynamic nature of Python, I doubt that's easy to do.
I'm not sure even os.listdir(), for example, can be relied on,
given that you can import modules from zip files, or if the
import hook is used, even from remote locations or, presumably
(though I haven't seen it done yet), from "thin air" where
the module is generated dynamically.

-Peter
Jul 18 '05 #2
Peter L Hansen wrote:
....
Given the dynamic nature of Python, I doubt that's easy to do.
I'm not sure even os.listdir(), for example, can be relied on,
given that you can import modules from zip files, or if the
import hook is used, even from remote locations or, presumably
(though I haven't seen it done yet), from "thin air" where
the module is generated dynamically.


I used to do this somewhere in mcf.vrml (old VRML97 processing
package). The idea was to generate a module to hold classes which were
also generated on-the-fly (from VRML97 prototypes). You could then
import the classes from the non-existent module as long as the module
itself (which roughly corresponded to a VRML97 file) was part of your
pickle. The package holding those modules was empty until such time as
you either generated one of those modules or loaded one from a pickle,
and there was just no reasonable way to know what would eventually show
up there.

Advances in Python (and my moving away from my perverse love of such
tricks ;) ) made that particular usage obsolete, but it definitely has
been done in the past...

Enjoy,
Mike

________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Jul 18 '05 #3
Thanks, Peter. So the fact I didn't see any solution was not due to my lack
of knowledge. I thought I should ask and maybe someone would point out an
elegant solution that was just beyond my own grasp of Python.

os.listdir() would not work in the general case, but it's good enough in my
limited case. The package is well-determined and it's always a directory.
Even if I will change that some day, there will always be another equivalent
for the os.listdir.

So thanks again, I can confidently go and implement a solution based on
os.listdir.

Dan

"Peter L Hansen" <pe***@engcorp.com> wrote in message
news:df********************@powergate.ca...
Dan Perl wrote:
Is there a direct way to get a list of all the modules in a package,
other than using an os.listdir() for the directory of the package?


Given the dynamic nature of Python, I doubt that's easy to do.
I'm not sure even os.listdir(), for example, can be relied on,
given that you can import modules from zip files, or if the
import hook is used, even from remote locations or, presumably
(though I haven't seen it done yet), from "thin air" where
the module is generated dynamically.

-Peter

Jul 18 '05 #4
What a tease! "It definitely has been done in the past", so there must be a
way, but you're not telling us how you did that! :-)

Oh well, I have another solution (based on os.listdir) and even if it is not
the most elegant one that could exist, at least it's going to be clear to
understand.

Dan

"Mike C. Fletcher" <mc******@rogers.com> wrote in message
news:ma**************************************@pyth on.org...
Peter L Hansen wrote:
...
Given the dynamic nature of Python, I doubt that's easy to do.
I'm not sure even os.listdir(), for example, can be relied on,
given that you can import modules from zip files, or if the
import hook is used, even from remote locations or, presumably
(though I haven't seen it done yet), from "thin air" where
the module is generated dynamically.


I used to do this somewhere in mcf.vrml (old VRML97 processing package).
The idea was to generate a module to hold classes which were also
generated on-the-fly (from VRML97 prototypes). You could then import the
classes from the non-existent module as long as the module itself (which
roughly corresponded to a VRML97 file) was part of your pickle. The
package holding those modules was empty until such time as you either
generated one of those modules or loaded one from a pickle, and there was
just no reasonable way to know what would eventually show up there.

Advances in Python (and my moving away from my perverse love of such
tricks ;) ) made that particular usage obsolete, but it definitely has
been done in the past...

Enjoy,
Mike

________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Jul 18 '05 #5
Dan Perl wrote:
What a tease! "It definitely has been done in the past", so there must be a
way, but you're not telling us how you did that! :-)

Ah, apparently I wasn't clear enough in my quoted antecedent :) , I was
referring to Peter's comment that he'd not seen anyone produce modules
'from "thin air"', rather than to whether this could be solved in the
general case. I was backing him up in his assertion that there's no
*general* way to solve the problem, rather than attempting to contradict
him :) (those Canucks get dangerous when contradicted).

If you mean you wanted to know how to do the module-from-thin-air thing,
the code for mcf.vrml is available on sourceforge, so it's not like I'm
hiding it ;) , but I doubt you'd want to use that particular pattern
anymore :) .

Have fun,
Mike
Peter L Hansen wrote:
...

....
import hook is used, even from remote locations or, presumably
(though I haven't seen it done yet), from "thin air" where
the module is generated dynamically.

....
________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Jul 18 '05 #6
"Mike C. Fletcher" <mc******@rogers.com> wrote in message
news:ma**************************************@pyth on.org...
Dan Perl wrote:
What a tease! "It definitely has been done in the past", so there must be
a way, but you're not telling us how you did that! :-)
Ah, apparently I wasn't clear enough in my quoted antecedent :) , I was
referring to Peter's comment that he'd not seen anyone produce modules
'from "thin air"', rather than to whether this could be solved in the
general case. I was backing him up in his assertion that there's no
*general* way to solve the problem, rather than attempting to contradict
him :) (those Canucks get dangerous when contradicted).


Got it now! And what do you mean "those" Canucks? From the email address
you seem to be Canadian too! Not to mention that I am Canadian!
If you mean you wanted to know how to do the module-from-thin-air thing,
the code for mcf.vrml is available on sourceforge, so it's not like I'm
hiding it ;) , but I doubt you'd want to use that particular pattern
anymore :) .
No, thanks, that's not what I wanted, and thanks for confirming Peter's
reply. I can move on with my own solution.

Dan
Have fun,
Mike

Jul 18 '05 #7
Dan Perl wrote:
....
Got it now! And what do you mean "those" Canucks?
Igloo-dwellers from the North... very short tempers... I blame the
strong beer...
From the email address
you seem to be Canadian too!
Scurrilous rumours those. I was born in Montreal, so I'm obviously not
Canadian ;) :) .
Not to mention that I am Canadian!

Really, had no idea ;) :D ... guess I shouldn't contradict you then...

Have fun,
Mike

________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Jul 18 '05 #8
Mike C. Fletcher wrote:
referring to Peter's comment that he'd not seen anyone produce modules
'from "thin air"', rather than to whether this could be solved in the
general case. I was backing him up in his assertion that there's no
*general* way to solve the problem, rather than attempting to contradict
him :) (those Canucks get dangerous when contradicted).

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
No they don't!

-Peter

P.S.: See you at the PyGTA meeting in two weeks, Mike...
Jul 18 '05 #9

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

Similar topics

15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
0
by: ischenko | last post by:
Hi, I'm trying to find all modules that contain doctests and execute them (using DocTestSuite). The problem is how to iterate (programmatically) through the package's modules. >>> import M...
2
by: Benjamin Rutt | last post by:
I note that the help() function of interactive python can determine all available modules: $ python Python 2.4 (#1, Mar 31 2005, 15:26:02) on linux2 Type "help", "copyright", "credits" or...
10
by: Noah | last post by:
I would like to package my main script and all the modules it imports into a single script that will run just as the collection would. It should not need to package standard Python lib modules --...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
4
by: Fabian Braennstroem | last post by:
Hi, I am pretty new to python and will use it mainly in combination with scientific packages. I am running ubuntu breezy right now and see that some packages are out of date. Do you have any...
7
by: alito | last post by:
Hi all, I am new to using packages to group my modules. I can't figure out how to run a module that uses relative imports without writing a wrapper that imports that module. Everything I try...
0
by: Gabriel Genellina | last post by:
En Sun, 24 Aug 2008 07:34:41 -0300, Mohamed Yousef <harrrrpo@gmail.comescribió: Yes. That way, when you see a name "foo" used in a module, you can look at the imports to see where it comes from....
16
by: ssecorp | last post by:
Is there a way to view all the modules I have available for import from within Python? Like writing in the interpreter: import.modules Also, is there anything like Cpan for Python?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.