unum-cloud / usearch

Fast Open-Source Search & Clustering engine × for Vectors & 🔜 Strings × in C++, C, Python, JavaScript, Rust, Java, Objective-C, Swift, C#, GoLang, and Wolfram 🔍
https://unum-cloud.github.io/usearch/
Apache License 2.0
1.93k stars 109 forks source link

Bug: AttributeError: module 'importlib' has no attribute 'util' #360

Closed Omkar-Ranadive closed 4 months ago

Omkar-Ranadive commented 4 months ago

Describe the bug

I was unable to import usearch in Python after installing it using pip install usearch.

When I tried to run the example code given in the README, the following error gets thrown:

Traceback (most recent call last):
line 2, in <module> from usearch.index import Index

 File "C:\Users\omkar\miniconda3\envs\memdnn\Lib\site-packages\usearch\__init__.py", line 13, in <module>
    sqlite = importlib.util.find_spec("usearch.compiled").origin
             ^^^^^^^^^^^^^^

AttributeError: module 'importlib' has no attribute 'util'

I am using a conda environment created using miniconda. I tried various Python versions (3.9 - 3.12), the error existed in all of them.

The Fix: I was able to fix the error by manually adding import importlib.util to the __init__.py file of usearch package as follows:

import importlib
import importlib.util

from usearch.compiled import (
    VERSION_MAJOR,
    VERSION_MINOR,
    VERSION_PATCH,
)

__version__ = f"{VERSION_MAJOR}.{VERSION_MINOR}.{VERSION_PATCH}"

# The same binary file (.so, .dll, or .dylib) that contains the pre-compiled
# USearch code also contains the SQLite3 binding
sqlite = importlib.util.find_spec("usearch.compiled").origin

Steps to reproduce

index = Index(ndim=3)

vector = np.array([0.2, 0.6, 0.4]) index.add(42, vector)

matches = index.search(vector, 10)

assert matches[0].key == 42 assert matches[0].distance <= 0.001 assert np.allclose(index[42], vector)


* Importlib attribute error gets thrown for the line `from usearch.index import Index`

### Expected behavior

usearch package gets imported correctly without the attribute error 

### USearch version

v2.9.1

### Operating System

Windows 11 Home

### Hardware architecture

x86

### Which interface are you using?

Python bindings

### Contact Details

omkar.ranadive@u.northwestern.edu

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Code of Conduct

- [X] I agree to follow this project's Code of Conduct
ashvardanian commented 4 months ago

Oh, thank you, @Omkar-Ranadive! Can you please open a patch with that fix?

Omkar-Ranadive commented 4 months ago

Oh, thank you, @Omkar-Ranadive! Can you please open a patch with that fix?

Done! https://github.com/unum-cloud/usearch/pull/361