Skip to content

pertpy Installation Fails on Google Colab Due to numpy AttributeError #724

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

Open
LCGaoZzz opened this issue Mar 8, 2025 · 1 comment
Open
Labels
bug Something isn't working

Comments

@LCGaoZzz
Copy link

LCGaoZzz commented Mar 8, 2025

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 LCGaoZzz added the bug Something isn't working label Mar 8, 2025
@Zethson
Copy link
Member

Zethson commented Mar 8, 2025

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 googlecolab/colabtools#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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants