Edinburgh-Genome-Foundry/GoldenHinges

Python3.10 breaks GoldenHinges

mcrone opened this issue · 7 comments

There seems to be an implicit call of from collections import Sequence in the code. This works up until Python3.9, but then returns the error ImportError: cannot import name 'Sequence' from 'collections'.

This is because collections has been renamed to collections.abc and so this can be fixed with import collections.abc as collections in Python3.10.

Found reference to this issue here: https://stackoverflow.com/questions/69596494/unable-to-import-freegames-python-package-attributeerror-module-collections

As I mentioned this seems to be an implicit import as there is no reference to collections in the code of either GoldenHinges or DnaFeaturesViewer.

veghp commented

Thank you for the notification, I'll look into this. Looks like it can be solved with a simple try except, to keep compatibility with Python 3.6

veghp commented

@mcrone can you please provide the command or a reproducible example that causes the error?

The tests pass in Python 3.10, so I was not able to reproduce the error. I confirm that in v3.10, your example call to collections causes an error, but it's not used in GoldenHinges. Its requirements, proglog, dnachisel and geneblocks, have different imports from collections, but those work too.

@veghp I just ran the example code from the repo:

import collections.abc as collections

from goldenhinges import OverhangsSelector
selector = OverhangsSelector(gc_min=0.25, gc_max=0.5, differences=2,
                             forbidden_overhangs=['ATGC', 'CCGA'])
overhangs = selector.generate_overhangs_set()
print (overhangs)

This is the error:

AttributeError                            Traceback (most recent call last)
/var/folders/zv/39j4sx5x2t3g9m8zb4z0nx740000gp/T/ipykernel_91285/1884757783.py in <module>
      1 import collections.abc as collections
      2 
----> 3 from goldenhinges import OverhangsSelector
      4 selector = OverhangsSelector(gc_min=0.25, gc_max=0.5, differences=2,
      5                              forbidden_overhangs=['ATGC', 'CCGA'])

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/goldenhinges/__init__.py in <module>
      8 from .biotools import (list_overhangs, gc_content, sequences_differences,
      9                        reverse_complement, load_record)
---> 10 from .reports import (write_report_for_cutting_solution,
     11                       new_sequence_from_cutting_solution)
     12 from .version import __version__

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/goldenhinges/reports.py in <module>
----> 1 from dna_features_viewer import BiopythonTranslator
      2 import numpy as np
      3 from copy import deepcopy
      4 from Bio import SeqIO
      5 import flametree

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/dna_features_viewer/__init__.py in <module>
      1 """ dna_features_viewer/__init__.py """
...
-> 1182         return ((isinstance(value, collections.Sequence) or cls._is_seq_like(value)) and
   1183                 not isinstance(value, string_types))
   1184 

AttributeError: module 'collections' has no attribute 'Sequence'
veghp commented

Thank you. The provided code runs fine for me. I use

Python 3.10.9 (main, Jan 11 2023, 15:21:40) [GCC 11.2.0] on linux
biopython           1.81
dna-features-viewer 3.1.2
goldenhinges        0.2.3

We previously agreed that neither pkg makes a call to collections, and the last error lines are not in either pkg.
Is there any more error trace available? I'd like to find out the origin of line 1182.

Biopython has recently made a possibly related change: https://github.com/biopython/biopython/blob/master/NEWS.rst#18-november-2022-biopython-180
but the last error lines are not in the latest codebase.

I suspect there may be a mismatch between some of the installed packages or Python version. The above installations that worked for me are in a new conda environment. Would updating the packages solve the problem?

Turns out that it was a bokeh problem, I still had an older version that was a dependency of another library.

However, I now have an arm64 problem. I can't install Numberjack because I don't believe Mistral can be compiled for arm64.

ImportError: dlopen(/Users/michaelcrone/Library/Python/3.10/lib/python/site-packages/Numberjack-1.2.1-py3.10-macosx-10.9-universal2.egg/Numberjack/solvers/_Mistral.cpython-310-darwin.so, 0x0002): tried: '/Users/michaelcrone/Library/Python/3.10/lib/python/site-packages/Numberjack-1.2.1-py3.10-macosx-10.9-universal2.egg/Numberjack/solvers/_Mistral.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/michaelcrone/Library/Python/3.10/lib/python/site-packages/Numberjack-1.2.1-py3.10-macosx-10.9-universal2.egg/Numberjack/solvers/_Mistral.cpython-310-darwin.so' (no such file), '/Users/michaelcrone/Library/Python/3.10/lib/python/site-packages/Numberjack-1.2.1-py3.10-macosx-10.9-universal2.egg/Numberjack/solvers/_Mistral.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))

Confirmed working on python3.11 on an x86_64 (my old laptop).

veghp commented

I'm glad that updating the packages solved the problem. Yes unfortunately Numberjack is an old dependency, and we also test and use for x86 architecture and Ubuntu only. I'll close this one as the original problem is solved, and this is also a separate issue which I see you have filed here: eomahony/Numberjack#62 .

Zulko commented

Yeah I'm not sure Numberjack is still maintained unfortunately. There is Google ORTools that looks still active and could do the job (see the end of this page)