Overcoming the Hazm Compatibility Challenge with Python 3.12

Search for a command to run...

No comments yet. Be the first to comment.
Over the years, my journey through the innovation landscape has been nothing short of exhilarating. From working in cutting-edge innovation labs to collaborating closely with venture capitalists, venture builders, and factories, I’ve seen the power o...

After years of taking over the maintenance of pychartjs, I'm excited to finally have the time to bring some much-needed updates to the project. It's been a journey, but I'm proud to say that pychartjs is now framework-agnostic and fully compatible wi...

Introduction Recently, I worked on a project that needed many gRPC functions. If you've used gRPC, you know it helps different software programs talk to each other efficiently. However, making Proto files for every Python dataclass was taking a lot o...

I wanted to share some thoughts on the importance of code reviews, appreciating your teammates, and this cool tool I found called Git Add Co-Author that makes giving credit where it’s due super easy. A few weeks ago, I wrote a blog post titled "Contr...

Hello everyone, I hope you are doing well. I want to share some important news with you. I am nominating myself for the position of Director on the Board of the Python Software Foundation (PSF). My Statement Here is my official statement for the PSF ...

Today, my coworker Mahdi and I tackled a significant challenge with the Hazm Python package while working with Python 3.12. The issue arose when we discovered that Hazm was incompatible with the new version of Python. A collaborator from Hazm confirmed that the problem couldn't be resolved until Gensim released a new package. You can read the discussion here: Hazm Issue #322.
To solve the issue, we first needed to reproduce the bug. Here are the steps we took:
Install Hazm on Python 3.12: We started by trying to install Hazm in a Python 3.12 environment using the following command:
pip install hazm
Identify the Compatibility Issue: During installation, we encountered an error related to the deprecated triu function from SciPy, which Gensim used. The error message pointed us directly to the problem with the triu function.
Investigate Dependencies: We examined Hazm's dependencies and found that it relied on Gensim. To confirm that Gensim was the source of the problem, we tried installing Gensim separately and encountered the same error.
Review Gensim's Repository: Determined to find a solution, we checked the Gensim GitHub repository. We noticed that the development branch was 25 commits ahead of the latest release tag. The latest release had an issue with the deprecated triu function from SciPy, which Gensim used. Fortunately, we found that this problem was fixed in a commit on the development branch.
To address this issue, we used Poetry, a tool for dependency management and packaging in Python. Poetry allows installation from a specific commit, which was crucial because we needed the latest changes from the development branch of Gensim, not the stable release.
Use Poetry for Installation: We used Poetry to install Gensim from the specific commit where the issue was resolved. We add the package manually in pyproject.toml:
gensim={git = "https://github.com/piskvorky/gensim.git", rev ="commit-hash"}
Replace <commit-hash> with the actual commit hash where the issue was fixed.
Test the Solution: We tested the new setup, and it worked perfectly with Python 3.12.
Contribute Back: We made a pull request to the Hazm repository to share our solution with the community. You can see our commits here: Hazm Pull Request #338.
Until our pull request is merged into the main Hazm repository and published on PyPI, users can use the same approach we used for Gensim. This means installing Hazm from Mahdi's repository using either pip or Poetry.
You can follow these steps to install Hazm from the specific branch that includes our fix.
Open your terminal or command prompt.
Run the following command:
pip install git+https://github.com/mahdisharifloo/hazm.git@master
This command tells pip to install Hazm directly from Mahdi's GitHub repository from the master branch.
Open your terminal or command prompt.
Navigate to your project directory.
Add the Hazm package from Mahdi's repository with the following command:
poetry add git+https://github.com/mahdisharifloo/hazm.git
This command instructs Poetry to add Hazm to your project's dependencies, pulling it directly from the specified repository and branch.
By following these steps, we were able to fix the compatibility issue with Hazm and Python 3.12. We hope our solution will help others facing the same problem.
Our experience has taught us the importance of being proactive and contributing back to the open-source community. By sharing our solution, we aim to help others who might encounter similar issues.
Until our changes are merged into the official Hazm repository and a new version is released on PyPI, users can follow our example to use the fixed version of Hazm from Mahdi's repository.
We look forward to seeing our contribution help improve Hazm for everyone in the community.
For more details, you can see the comparison and commits related to our fix here: Comparison and Commits.
We hope this guide helps you navigate similar issues and contributes to smoother development experiences.