miguelgrinberg / flasky

Companion code to my O'Reilly book "Flask Web Development", second edition.
MIT License
8.52k stars 4.2k forks source link

Breaks in python 3.10.6 #542

Closed truthadjuster closed 1 year ago

truthadjuster commented 1 year ago

flask deploy does not work in python 3.10.6. Error log below:

Traceback (most recent call last):
  File "/home/dx/src/py/grinberg/flasky/venv/bin/flask", line 5, in <module>
    from flask.cli import main
  File "/home/dx/src/py/grinberg/flasky/venv/lib/python3.10/site-packages/flask/__init__.py", line 17, in <module>
    from werkzeug.exceptions import abort
  File "/home/dx/src/py/grinberg/flasky/venv/lib/python3.10/site-packages/werkzeug/__init__.py", line 151, in <module>
    __import__('werkzeug.exceptions')
  File "/home/dx/src/py/grinberg/flasky/venv/lib/python3.10/site-packages/werkzeug/exceptions.py", line 71, in <module>
    from werkzeug.wrappers import Response
  File "/home/dx/src/py/grinberg/flasky/venv/lib/python3.10/site-packages/werkzeug/wrappers.py", line 26, in <module>
    from werkzeug.http import HTTP_STATUS_CODES, \
  File "/home/dx/src/py/grinberg/flasky/venv/lib/python3.10/site-packages/werkzeug/http.py", line 1044, in <module>
    from werkzeug.datastructures import Accept, HeaderSet, ETags, Authorization, \
  File "/home/dx/src/py/grinberg/flasky/venv/lib/python3.10/site-packages/werkzeug/datastructures.py", line 16, in <module>
    from collections import Container, Iterable, MutableSet
ImportError: cannot import name 'Container' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

It seems that in python 3.10.6 we can no longer do: from collections import Container, Iterable, MutableSet. But we can in python 3.6.9.

What's a quick way to be able to have any python version (and its corresponding pip) at your disposal? It can be a handy technique to make work old stuff.

miguelgrinberg commented 1 year ago

The offending import is made by werkzeug, not by this project. Have you tried upgrading this package to the latest version?

truthadjuster commented 1 year ago

The offending import is made by werkzeug, not by this project. Have you tried upgrading this package to the latest version?

Yes, I did tried to upgrade to latest Werkzeug 2.2.2, 2.2.1 and it dominoed to other error in MarkupSafe. As this breaking change is in the python library itself, I'm checking on the best practice with pyenv to overcome issues such as this.

miguelgrinberg commented 1 year ago

The logical thing to do would be to ensure the release dates of all your dependencies (both direct and transitive) are after the release date of the Python interpreter that you are using, but Python doesn't make this easy. In practice the best solution is to upgrade everything to latest and hope that all the packages have addressed the problem.

truthadjuster commented 1 year ago

I've played on pyenv and trying on python 3.6.9 it seg faults if I do pip list. I then next tried 3.7.0 and it still seg faults. I suspect is due to my GCC 11.2.0 I am on latest Ubuntu jammy. In my older Ubuntu with GCC 9 there is no seg fault. The flasky project now works in my Ubuntu jammy (its default python version is 3.10.6) and using pyenv to switch to python 3.8.0. In my older Ubuntu with GCC 9, flasky also worked there using python 3.6.9.

I would think that requirements.txt should also specify the python version.

miguelgrinberg commented 1 year ago

it seg faults if I do pip list

If I understand you correctly, this is pip segfaulting, so this isn't something I can help with, you may need to take it up with the Python org.

truthadjuster commented 1 year ago

True. It is outside the flasky project. The most reproducible step that I came up with is here