No, no design. If you want to avoid big overhead when importing submodules, you simply use the empty __init__.py to define the packages. Thus, the overhead costs of importing the package are almost zero.
If pandas does not do this, you have no way to import pandas.util.clipboard without importing pandas and util . What you can do, however, is a huge hack and the equivalent is not to import the clipboard module as a regular module, and not as a submodule. You just need to find the place where pandas installed (e.g. /usr/lib/pythonX.Y/dist-packages/ ) and insert the path of the parent package in sys.path ( /usr/lib/pythonX.Y/dist-packages/pandas/util in your case). Then you can import the clipboard package by doing:
import clipboard
Note that:
import clipboard from pandas.util import clipboard as clipboard2 print(clipboard == clipboard2)
False will be printed. In fact, this can break a lot of code, since you fundamentally violate some of the invariants that the import mechanism suggests.
In particular, if a submodule refers to other submodules using relative imports, the import will fail, and there are other situations where it will behave incorrectly. Another example when this fails is if you are dealing with pickled objects. If you have any objects pickled using a module imported as pandas.util.clipboard , you can not break them using the imported clipboard module, as described above.
So not ! I suggest either:
- Live with him if the time taken to import the package is not a real problem.
- Or: try finding a replacement. If you only need
pandas.util.clipboard and not the rest of pandas , you should probably not use pandas in the first place, and you should use a smaller package that only implements clipboard functionality.
If you look at the pandas.util.clipboard source code , you will find that it is actually just a pyperclip version of module 1.3. You can simply add this module to your site-packages and use it instead of the one provided by pandas . In fact, the pandas command only added the following to the end of the source code:
#
Expand a bit about why python imports work this way.
As you know, there are objects in python modules. And it also happens that packages are modules, although not every module is a package. When you import a package like in:
import pandas.util.clipboard
Python should:
- Create an instance of
module pandas - Create an instance of
module util and add it as an attribute in pandas - Create an instance of
module clipboard and add it as an attribute in util .
To create an instance of module , python must execute the code in the module.
Import form:
from pandas.util import clipboard
Only syntactic sugar for:
import pandas.util.clipboard clipboard = pandas.util.clipboard del pandas.util
Note that in the case of from clipboard could be either module / package, or just something specific inside util . To verify this, the interpreter must also import util , and for this it must also import pandas .