Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package.module import

Tags:

python

scipy

so this is a collection of questions that are more to clarify things and help better understand rather than an issue I am having.

I apologise now if I got things wrong or if these questions have been answered before. I wasn't able to find them.

First clarification I want to ask is:

Let us assume:

import scipy

First, I have noticed that you cannot in general access a module in a package by doing import package and then trying to access package.module.

For example scipy.io

You often have to do import package.module or even import astropy.io.fits, or you can do from package import module.

My question is why is this the case, and why is it so random -dependent on the package? I can't seem to identify any stable pattern. Is it due to the fact that some of these libraries (packages) are very big and in order to not have memory problems it only imports the core attributes/modules?

The second question:

It relates to actually checking the size of these packages. Is there any way to see how big they are when imported? Any way of knowing what will work and what won't other than trying it? I guess I could check with sys.modules and try to obtain it from there?

The third and final question:

In the scenario that I am not running my code on a Raspberry Pi and I don't necessarily have to worry about the memory issue (if that is the reason why they don't allow direct access), is there any way of actually importing package, such that it also loads all the sub packages?

I am just being lazy and wondering if it is possible. I am aware that it isn't good practice, but curiosity killed the cat.


Just to update and make it accessible to people to see related questions I have looked at:

This answer gives good advice on good general practice: What are good rules of thumb for Python imports?

Why can't I use the scipy.io? just like the documentation explains why the subpackage isn't necessarily imported

Then there is obviously the documentation: https://docs.python.org/3/reference/import.html#packages Section 5.2.1 is the reason why import scipy doesn't also import scipy.io, but I was wondering why would developers not make it an automated process.

This question is actually similar to part of my question but doesn`t seem to have a clear answer Python complex subpackage importing

Status of Questions:

Question 1: Good reason in answers

Question 2: Pending

Question 3: Pending

like image 820
nzicher Avatar asked Oct 19 '25 13:10

nzicher


1 Answers

Answer Q1

When you import a package, especially large ones like SciPy, it uses the init.py module intialisation module which prevents all subpackages/modules from being imported automatically to save space. I won't go into this further as this is already mentioned in this question, documented here, and talked about in other answers.

Additionally, if you have questions about scripts vs. modules, this post is incredibly descriptive.

Answer Q2

To find the size of a package I would point you towards this post about finding package directories, and then this post about reporting the size of a particular directory. You could create some combined code to do both for you.

Answer Q3

Update: Unsure on how to do this as the normal from package import * works as explained in the documentation (similar to Q1):

if a package’s __init__.py code defines a list named __all__, it is taken to be the list of module names that should be imported when from package import * is encountered

like image 62
Nebbles Avatar answered Oct 27 '25 06:10

Nebbles