pymatviz: Incompatible with Google Colab (Python 3.7)

Open In Colab

!pip install ml-matrics
from ml_matrics.parity import density_hexbins
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
[<ipython-input-1-48d4b62e713f>](https://localhost:8080/#) in <module>()
----> 1 from ml_matrics.parity import density_hexbins

1 frames
[/usr/local/lib/python3.7/dist-packages/ml_matrics/__init__.py](https://localhost:8080/#) in <module>()
      1 from .correlation import marchenko_pastur, marchenko_pastur_pdf
      2 from .cumulative import add_dropdown, cum_err, cum_res
----> 3 from .elements import (
      4     count_elements,
      5     hist_elemental_prevalence,

[/usr/local/lib/python3.7/dist-packages/ml_matrics/elements.py](https://localhost:8080/#) in <module>()
      1 from __future__ import annotations
      2 
----> 3 from typing import TYPE_CHECKING, Any, Literal, Sequence
      4 
      5 import matplotlib.pyplot as plt

ImportError: cannot import name 'Literal' from 'typing' (/usr/lib/python3.7/typing.py)

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Not much pain i.t.o. workload. Should just take a few minutes. The pain is more psychological. 😄 I’m annoyed with Google for building Colab which is a great product but then completely abandoning it. My usual policy is not to put effort into supporting unmaintained platforms. But heck what are a few minutes…

Just in the process of splicing out your nice count_elements function

Glad if it can be useful. 😄

If it’s just a warning, I think it’s safe to ignore. But I’m still surprised. Pylance should know better.

from __future__ import annotations

backports the pipe operator for unions and other modern type-hint syntax all the way to py37.

@sgbaird That’s cool. Thanks for reporting this trick. I added a link to your comment to the readme in 65105e9.

A workaround for this might be to perform a local installation via cloning and modifying the self-documenting f-strings:

Clone

!git clone https://github.com/janosh/pymatviz.git
%cd pymatviz

Modify f-strings

import os, fnmatch
def findReplace(directory, find, replace, filePattern):
    """https://stackoverflow.com/a/6257321/13697228"""
    for path, dirs, files in os.walk(os.path.abspath(directory)):
        for filename in fnmatch.filter(files, filePattern):
            filepath = os.path.join(path, filename)
            with open(filepath) as f:
                s = f.read()
            s = s.replace(find, replace)
            with open(filepath, "w") as f:
                f.write(s)
# to make compatible with Python 3.7
findReplace("pymatviz", "=}", "}", "*.py")

This seemed to work OK for another use-case (CDVAE on Colab). Will probably try it out next time I’m surfacing an example of some package on Colab and want to use pymatviz.

I actually didn’t know about self-documenting f-strings until you mentioned it. Very convenient!

Yes, big fan here. 😄

There should be a version of ml_matrics from a while back that runs fine on Google Colab. Though unfortunately since I messed up the python_requires which you just fixed in #18, pip doesn’t know which version that is and tries to install the latest.

There are 2 long-standing issues on upgrading the OS on which Colab runs which would also update the Python version it offers:

Unfortunately, that endeavor seems to have stalled quite badly. Not sure why it’s so difficult. You may want to subscribe there for updates.