Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError when trying to access submodules #4683

Closed
vinisalazar opened this issue Apr 6, 2022 · 3 comments
Closed

AttributeError when trying to access submodules #4683

vinisalazar opened this issue Apr 6, 2022 · 3 comments

Comments

@vinisalazar
Copy link

🐛 Bug Report

Hi,

I'm noticing what I think is unexpected behaviour when trying to load classes from iris. Basically, many of the submodules are unavailable when only importing the iris module and not running anything. My context is, I want to access specific iris classes (e.g. Cube, CubeList) to provide type hints in my package.

When I run or import the modules separately, they become available under the iris module. I'm using a fresh conda environment with iris 3.2.1 and Python 3.9 The command to create the environment is conda create -n iris python=3.9 iris -c conda-forge.

Here's what's happening:

>>> import iris
>>> iris.cube
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'iris' has no attribute 'cube'
>>> dir(iris)
['AttributeConstraint', 'Constraint', 'FUTURE', 'Future', 'IrisDeprecation', 'NameConstraint', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_constraints', '_deprecation', '_generate_cubes', '_load_collection', 'config', 'contextlib', 'exceptions', 'glob', 'io', 'iris', 'iris_sample_data', 'itertools', 'load', 'load_cube', 'load_cubes', 'load_raw', 'os', 'pathlib', 'sample_data_path', 'save', 'site_configuration', 'threading', 'warn_deprecated']

Strangely, this is solved if I import a module individually:

>>> import iris
>>> iris.cube
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'iris' has no attribute 'cube'
>>> from iris import cube
>>> iris.cube
<module 'iris.cube' from '/Users/vwsalazar/miniconda3/envs/erddapy/lib/python3.9/site-packages/iris/cube.py'>
>>> 

The same happens if I call any functions from the iris module, e.g. iris.load_raw().

This snippet shows exactly which submodules/attributes become available after importing a submodule individually or running a function:

>>> import iris
>>> before = dir(iris)
>>> from iris import cube
>>> after = dir(iris)
>>> [i for i in after if i not in before]
['_data_manager', '_lazy_data', '_merge', 'analysis', 'aux_factory', 'common', 'coord_systems', 'coords', 'cube', 'std_names', 'time', 'util']
>>> 

I was having problems only with the cube module, but after running this snippet, I realised it was happening with other attributes, which led me to believe this may be related to the problem reported in #3242.

How To Reproduce

Steps to reproduce the behaviour:

  1. Install iris with conda: conda create -n iris iris python=3.9 -c conda-forge && conda activate
  2. Start Python: python
  3. Import iris: import iris
  4. Try accessing one of the attributes listed above: iris.cube

Expected behaviour

Have all of the ['_data_manager', '_lazy_data', '_merge', 'analysis', 'aux_factory', 'common', 'coord_systems', 'coords', 'cube', 'std_names', 'time', 'util'] available in the iris module without having to import any submodules or run any functions before.

Environment

I tested this on a Mac and also on a Linux Docker container with iris 3.2 and 3.1.

  • OS & Version: ['Linux-5.10.76-linuxkit-x86_64-with-glibc2.31', 'macOS-10.16-x86_64-i386-64bit']
  • Iris Version: ['3.2.1', '3.1']

Please let me know if you need any more information and thank you for any assistance you can provide.

Best,
V

@trexfeathers
Copy link
Contributor

Thanks for getting in touch @vinisalazar, and with so much helpful detail 🙂. Sorry but this behaviour is intentional (part of #4174) in order to reduce Iris' import time. The previous import time of >1 second was causing problems in some of our users' specialist workflows.

The new behaviour that you have observed means users only import the parts of Iris they need for their script. But it does unfortunately need more granular import commands, as you've demonstrated.

@vinisalazar
Copy link
Author

Thanks @trexfeathers for clarifying, that makes sense. I would, however, suggest to add some sort of exception handling providing an informative message, or at least document this behaviour clearly in the documentation page (apologies if that's already the case and I missed it!).

@rcomer
Copy link
Member

rcomer commented Apr 6, 2022

Hi @vinisalazar, there is an entry on this in our "What's new" page for Iris v3.1:
https://scitools-iris.readthedocs.io/en/stable/whatsnew/3.1.html#incompatible-changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants