pertpy icon indicating copy to clipboard operation
pertpy copied to clipboard

`pertpy` Installation Fails on Google Colab Due to `numpy` AttributeError

Open LCGaoZzz opened this issue 10 months ago • 1 comments

Description

I tried installing pertpy on Google Colab using the following commands:

!pip install pertpy
!pip install pertpy[coda]
import pertpy as pt

However, when attempting to import pertpy, I encountered the following error:


AttributeError                            Traceback (most recent call last)  
<ipython-input-3-5d616454759d> in <cell line: 0>()  
      2 get_ipython().system('pip install pertpy[coda]')  
      3   
----> 4 import pertpy as pt  
      5 get_ipython().system('pip install scanpy')  
      6 get_ipython().system('pip install plotnine')  

24 frames  
/usr/local/lib/python3.11/dist-packages/numpy/__init__.py in __getattr__(attr)  
    322     warnings.filterwarnings("ignore", message="numpy.dtype size changed")  
    323     warnings.filterwarnings("ignore", message="numpy.ufunc size changed")  
--> 324     warnings.filterwarnings("ignore", message="numpy.ndarray size changed")  
    325   
    326     def __getattr__(attr):  

AttributeError: module 'numpy' has no attribute 'bool'.  
np.bool was a deprecated alias for the builtin bool. To avoid this error in existing code, use bool by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.bool_ here.  
The alias was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:  
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations  

⚠️ THIS ISSUE PREVENTS PERTPY FROM BEING USED IN GOOGLE COLAB. ⚠️

Steps to Reproduce

1.Open a new Google Colab notebook, start a v2-8 TPU runtime. 2.Run the following commands:

!pip install pertpy
!pip install pertpy[coda]
import pertpy as pt

3.Observe the AttributeError related to numpy.bool.

LCGaoZzz avatar Mar 08 '25 04:03 LCGaoZzz

Dear @LCGaoZzz ,

I'm sorry for the inconveniences. The issue is that the scipy version on google colab is not recent enough if you install pertpy. If you run pip install -U scipy after having installed pertpy, this issue is mitigated. You might run into another compatibiliy issue related to import hooks. See https://github.com/googlecolab/colabtools/issues/5000. This out of my control.

This is how I got it to work:

!pip install pertpy[coda]
!pip install -U scipy
!pip install -U torch

import sys
import types
from importlib.machinery import ModuleSpec

# Find and fix ANY import hook missing find_spec
hooked_imports = []
for i, finder in enumerate(sys.meta_path):
    # Skip built-in finders that already have find_spec
    if hasattr(finder, 'find_spec'):
        continue
        
    # Only fix hooks that have find_module
    if hasattr(finder, 'find_module'):
        hook_name = finder.__class__.__name__ if hasattr(finder, '__class__') else str(finder)
        hooked_imports.append(hook_name)
        
        def find_spec_wrapper(self, fullname, path, target=None):
            loader = self.find_module(fullname, path)
            if loader is not None:
                return ModuleSpec(name=fullname, loader=loader)
            return None
        
        # Bind the method to the instance
        finder.find_spec = types.MethodType(find_spec_wrapper, finder)

print(f"Fixed {len(hooked_imports)} import hooks: {', '.join(hooked_imports)}")

# Now try importing sympy directly 
try:
    import sympy
    print("Successfully imported sympy")
    
    # Then try pertpy
    import pertpy as pt
    print("Successfully imported pertpy")
except Exception as e:
    print(f"Error: {e}")

Unfortunately, colab is a mess. I'll try to set better minimum boundaries soon to help mitigate such issues but I also partially blame Colab for this.

Hope this helps!

Zethson avatar Mar 08 '25 20:03 Zethson

Closing this for now as I think this is out of my control.

Zethson avatar Sep 05 '25 13:09 Zethson