AttributeError: module 'pandora' has no attribute 'util' - python

I have a github repo named pandora:
https://github.com/akshkr/pandora-python
In the test_case_refactor branch, I can't use submodules of pandora such as pn.util.misc.seed_everything() gives me the following error.
AttributeError: module 'pandora' has no attribute 'util'
I doubted circular imports so moved every import inside function wherever possible but didn't seem to work. And the test-cases written inside the module are passing.
Can someone tell me what exact issue is causing this AttributeError?

Since misc is a python script and not a module in itself, you need to either import the function itself
from pandora.util.misc import seed_everything
seed_everything()
OR, you can import the misc script from util module
from pandora.util import misc
misc.seed_everything()

Related

Form A import B and import A causes troubles

I have a following problem. I have this folder structure:
folder
├──script.py
├──utils.py
└──venv
I would like to import functions from utils.py into script.py. When I try this from utils import function_a, function_b everything works. But when I try import utils I got an error NameError: name 'function_a' is not defined.
Whats the difference between from utils import function_a, function_b and import utils in this case? And how can I fix it, please?
import module : Nice when you are using many bits from the module. Drawback is that you'll need to qualify each reference with the module name.
from module import ... : Nice that imported items are usable directly without module name prefix. The drawback is that you must list each thing you use, and that it's not clear in code where something came from.
If you are importing the module itself, then you have to specify what you want to use from the module with the . operator:
import utils
utils.function_a()
utils.function_b()

Can't import classes from local files in Python

Python local import from files stored in at the same level of directory is often confusing to me. I have the following directory structure,
/distrib
__init__.py # empty
bases.py # contains classes MainBase etc.
extension.py # contains classes MainExtension etc
/errors
__init__.py
base_error.py
In file bases.py importing classes from extension.py is successful. For example, simply using from .extensions import MainExtension would work. On the other hand, in extensions.py, importing a class from bases.py faces challenges.
Attempt 1
If I from bases import MainBase, it complains ModuleNotFoundError: No module named 'bases'.
Attempt 2
If I specify it as local import by from .bases import MainBase, it complains ImportError: cannot import name 'MainBase'.
Attempt 3
If I import it using from . import bases, there is no error. But, consequently using bases.MainBase triggers error module distrib.bases has no attribute 'MainBase' and
it seem that all classes defined in the bases.py file are 'missing'.
However, in a different folder such as errors, I can import classes from distrib.bases normally. What exactly is happening here? Doesn't Python allow cyclical import?
You have a circular import. In your base module you try to import from extension. But from extension you import base. But base is not finished importing yet so that results in an ImportError when you try to import anything from the extensions module.

How to import an entire module from within the same directory

Sorry if this is a duplicate, but I couldn't find anything for this specific topic. So here's the question:
In python 3 when you want to import an object from some module you would write something like
from module import object
If you want to specify that the module is in the same diretory as the file you're currently working on, you'd precede the module name with a dot like
from .module import object
What do I have to write in order to import an entire module from within the same directory? Intuitively, I tried
import .module
But this gives me a syntax error.
The correct way is to import the module from the current package, i.e. ..
from . import module
This makes the module available with the name module.

About import modules in python

import nibabel
nibabel.processing.resample_to_output(input_img, voxel_size)
AttributeError: module 'nibabel' has no attribute 'processing'
import nibabel
import nibabel.processing
nibabel.processing.resample_to_output(input_img, voxel_size)
Why does the first code fail but the second code work?
Expanding on #juanpa answer in the comments, you can simply consider these as two different modules.
For this
import nibabel
You get the error which suggests that this module does not have an attribute named processing
But for this
import nibabel.processing
It works fine since itself can be considered a module and thus means that processing is not an attribute of nibabel.
So it looks the code that you are trying to run only requires the 2nd import and not the first.

Addressing python objects

I'm trying to use ncclient for Python.
If I do this it works:
from ncclient import manager
m = manager.connect()
If I do this it fails:
import ncclient
m = ncclient.manager.connect()
The error is AttributeError: 'module' object has no attribute 'manager'.
I don't understand what the difference is. Shouldn't that be the same method either way? Why isn't it?
Importing a module (package) does not automatically import submodule. (Some modules do. For example, importing os module also import os.path)
Replace following line:
import ncclient
with:
import ncclient.manager
to load the submodule manager.

Categories