1

I have the following package called biographs:

biographs (folder)
 >biographs (package)
  >__init__.py
  >bspace.py
  >bpdb.py
  >bgraph.py

The __init__.py file imports the class Pmolecule from the module pmolecule.py only as follows:

from .pmolecule import Pmolecule

In turn, the pmolecule.py file imports the followin modules:

from __future__ import absolute_import
from . import bpdb
from . import bgraph
from . import bspace

Ipython has a completion feature that lets you see what are the available attributes of your object, e.g.,

In [1]: cd ~/biographs/
/Users/rdora/biographs

In [2]: import biographs

In [3]: biographs.
                   bgraph    Pmolecule
                   bpdb      pmolecule
                   bspace

As you can see after the biographs. the list of attributes is equivalent to the complete list of modules plus the class Pmolecule which is the only explicit import in __init__.py.

It seems logical because all those are necessary to use the class Pmolecule.

However, I would like to know if there is a way to import only the class without having access to the modules it uses.

Thanks

rodgdor
  • 2,530
  • 1
  • 19
  • 26
  • Just asking, why would you want to do that? – cs95 Jul 12 '17 at 12:37
  • @COLDSPEED I work a lot in interactive mode with `ipython`, and often I find myself *surfing* through the modules' attributes to explore the package and see if there's a better functionality for what I do. The thing with this package that I wrote is that all the functions have only one goal, work as a kind of backend for the class `Pmolecule` and I would rather not see those modules available in the interactive mode because they are practically useless outside the context of said class and therefore, distracting? – rodgdor Jul 12 '17 at 12:45
  • If you are worried about performance issues, then partial imports don't matter. Whether you do, `from foo import bar` or `import foo`, the entire `foo` module will be compiled and imported. The only difference is what is made available in your namespace. – cs95 Jul 12 '17 at 12:46
  • @COLDSPEED is indeed the namespace I am thinking about. For instance, I use the module `numpy` in this package in a module called `bspace` and I can access to it after importing the package `biographs` as `biographs.bspace.numpy`. I would like to know if there is a way to *hide* numpy. I know that you can hide certain objects from a namespace by starting its name with an underscore `_` I wonder if something similar has an effect for modules... – rodgdor Jul 12 '17 at 12:50
  • I would be interested in this as well. I'm testing deleting the imported modules or unnecessary functions (e.g. del np) but I'm not sure about the side effects. @rodgdor did you find an elegant solution? – stedes Jun 04 '20 at 16:10

0 Answers0