etetoolkit / ete

Python package for building, comparing, annotating, manipulating and visualising trees. It provides a comprehensive API and a collection of command line tools, including utilities to work with the NCBI taxonomy tree.
http://etetoolkit.org
GNU General Public License v3.0
795 stars 214 forks source link

Cannot import name `TreeStyle` from `ete3` #678

Open kamurani opened 1 year ago

kamurani commented 1 year ago

Hi,

not sure what I'm doing wrong, but I am trying to use a minimal example of rendering trees using ete3 version 3.1.2.

from ete3 import Tree
t = Tree( "((a,b),c);" )
t.show()

I get:

ImportError                               Traceback (most recent call last)
Cell In[36], line 2
      1 t = Tree( "((a,b),c);" )
----> 2 t.show()

File ~/opt/anaconda3/envs/analysis/lib/python3.10/site-packages/ete3/coretype/tree.py:1362, in TreeNode.show(self, layout, tree_style, name)
   1356 def show(self, layout=None, tree_style=None, name="ETE"):
   1357     """
   1358     Starts an interactive session to visualize current node
   1359     structure using provided layout and TreeStyle.
   1360 
   1361     """
-> 1362     from ..treeview import drawer
   1363     drawer.show_tree(self, layout=layout,
   1364                      tree_style=tree_style, win_name=name)

File ~/opt/anaconda3/envs/analysis/lib/python3.10/site-packages/ete3/treeview/drawer.py:45
     41 import signal
     43 from .qt import *
---> 45 from .qt4_gui import _GUI, _PropertiesDialog, _BasicNodeActions
     47 from . import layouts
     48 from .main import save

File ~/opt/anaconda3/envs/analysis/lib/python3.10/site-packages/ete3/treeview/qt4_gui.py:61
...
---> 61 from .. import Tree, TreeStyle
     62 import time
     64 class _SelectorItem(QGraphicsRectItem):

ImportError: cannot import name 'TreeStyle' from 'ete3' (~/opt/anaconda3/envs/analysis/lib/python3.10/site-packages/ete3/__init__.py)

Other instances of this issue mention the need to install pyqt5 which I did successfully, but did not change the error.

Any ideas would be greatly appreciated!

heskad commented 1 year ago
`from Bio import AlignIO, Phylo
from Bio.Phylo.TreeConstruction import DistanceCalculator, DistanceTreeConstructor
from ete3 import Tree, TreeStyle, TextFace
import matplotlib.pyplot as plt
# Load the alignment from a file in fasta format
alignment_file = "20cd-hit-clust.fa"
alignment = AlignIO.read(alignment_file, "fasta")
# Построение дерева методом Neighbor Joining
calculator = DistanceCalculator('blosum62')
dm = calculator.get_distance(alignment)
nj_constructor = DistanceTreeConstructor(calculator, 'nj')
tree = nj_constructor.build_tree(alignment)
# Сохранение дерева в файл в формате newick
Phylo.write(tree, "my_tree.newick", "newick")
# Создать объект для вывода дерева в формате newick
ete_tree = Tree(tree.to_newick())
# Создать некорневое дерево и настроить его стиль
ts = TreeStyle()
ts.show_leaf_name = False
ts.mode = "c"
ts.arc_start = -180 # установить начальный угол на -180 градусов, чтобы дерево было некорневым
ts.arc_span = 360
ts.scale = 120 # установить масштаб дерева
ts.branch_vertical_margin = 10 # установить отступ между ветвями
ts.title.add_face(TextFace("Unrooted Tree", fsize=20), column=0) # добавить заголовок дерева

# Вывести некорневое дерево в виде изображения
ete_tree.render("my_tree.png", w=1200, h=800, tree_style=ts)

Traceback (most recent call last): File "C:\Users\rusla\AppData\Roaming\JetBrains\PyCharmCE2021.2\scratches\unrooted_tree.py", line 97, in from ete3 import Tree, TreeStyle, TextFace ImportError: cannot import name 'TreeStyle' from 'ete3' (C:\Users\rusla\PycharmProjects\pythonProject\venv\lib\site-packages\ete3__init__.py)

Process finished with exit code 1

phyden commented 1 year ago

Hi,

I ran into the same issue (caused by PyQT5) today. It turned out, that it's not enough to install pyqt with pip. To show what error you actually have you should try to print the error where it's caused and silenced:

#/path/to/lib/pythonx.y/site-packages/ete3/__init__.py
try:
    from .treeview.svg_colors import *
    from .treeview.main import *
    from .treeview.faces import *
    from .treeview import faces
    from .treeview import layouts
except ImportError as e:
    pass
    #add e.g.
    print(e)

in my case the error message was: libGL.so.1: cannot open shared object file: No such file or directory meaning, that I was missing the openGL system library. Maybe you should check wheter this is available on your system: ldconfig -p | grep libGL or locate libGL

Bad news is: I did not find a way to install libGL.so via pip or conda, so if you don't have root access to your machine, users might not be able to solve this on their own.