0

I have a package structure like this:

A/__init__.py
|
 --B/__init__.py
|
 --C/__init__.py

In each package and sub packages, it contains a few classes, e.g. A/__init__.py contains class A1 and A2, B/__init__.py contains class B1 and B2, C/__init__.py contains class C1 and C2. In A/__init__.py, it adds all the class names of A and its sub packages into __all__.

Now, I want to print out all package names as well as their contained class names, e.g.

A contains A1, A2, B contains B1, B2, C contains C1, C2

So given the absolute path of package A, e.g. 'xx/yy/A', how can I print the above line?

My question is not about how to retrieve file path of a package.

james
  • 1,107
  • 14
  • 29

2 Answers2

2
print(os.path.dirname(os.path.abspath(A.__file__)))

Or did you have something different in mind?

Update:

if package A has not been imported, how can I refer it using A.__file__?

Arguably, there's no easy way to know. You could figure it out by looking at what the import mechanism does and following that to find a folder/file named A.

I only know the absolute folder path of this package.

But that makes it sound like you already know the location of the package on disk and you want to import it, even though it's not installed as a package. You can do something like that, but you shouldn't.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • It answered your *original* question. You probably should have asked *another* question, since it's fundamentally different than the one that you asked. – Wayne Werner Jul 28 '16 at 16:04
  • sorry, your answer came too quickly for me to edit it properly. but the question title did tell what i am looking for. it's to print all class names under a package. And I'd like to import this package via its file path. – james Jul 28 '16 at 16:08
  • Do you have a *really good* reason for that? Because that's introducing all kinds of potential problems to your code. – Wayne Werner Jul 28 '16 at 16:10
  • we have developers working on all those modules (e.g. A1, A2, B1, B2... referred above) and we'd like to build a tool to connect these modules to each other. the first step to do is to list all the available modules and present them in some way, maybe via a GUI app so that people can pick the modules they want to use in their project. That's why this question... – james Jul 28 '16 at 16:13
  • You have enough rep - you should [join us in the python chat](http://chat.stackoverflow.com/rooms/6/python) and maybe we can help you figure out a better approach. – Wayne Werner Jul 28 '16 at 16:50
0

I will try to dig some code out, but in general :

  1. Walk through the package folders and files using 'os.walk'
  2. For every Python file you find use importlib to import it.
  3. For every imported module use the functions in the 'inspect' standard library module to identify the classes within each imported module.

I don't there is any nice way to import a module based on it's absolute path. The import process works by importing paths which are either relative to the existing module, or relative to an entry in sys.path. An option might be to force add the root of your package into sys.path - and for every file you find work out the dotted module name for the module.

Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33