在Linux上安装PyQt5 5.14.1

36
pip3 install PyQt5
Collecting PyQt5
  Using cached https://files.pythonhosted.org/packages/3a/fb/eb51731f2dc7c22d8e1a63ba88fb702727b324c6352183a32f27f73b8116/PyQt5-5.14.1.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/lib/python3.6/tokenize.py", line 452, in open
        buffer = _builtin_open(filename, 'rb')
    FileNotFoundError: [Errno 2] No such file or directory: '/tmp/pip-build-b2zw891b/PyQt5/setup.py'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-b2zw891b/PyQt5/

然后我从https://www.riverbankcomputing.com/software/pyqt/download5下载了zip文件夹并运行:

python3 configure.py --qmake /home/oo/Qt/5.14.0/gcc_64/bin/qmake
make
sudo make install

成功

>>> import PyQt5
>>> import PyQt5.QtCore
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'PyQt5.sip'
>>> 

于是我安装了

pip3 install PyQt5.sip
pip3 install sip

成功

但仍然出现相同的错误 No module named 'PyQt5.sip',用于 import PyQt5.QtCore

还尝试了PyQtChart,但仍有错误

pip3 install PyQtChart
Collecting PyQtChart
  Using cached https://files.pythonhosted.org/packages/83/35/4f6328db9a31e2776cdcd82ef7688994c11e265649f503858f1913444ba9/PyQtChart-5.14.0-5.14.0-cp35.cp36.cp37.cp38-abi3-manylinux1_x86_64.whl
Collecting PyQt5>=5.14 (from PyQtChart)
  Using cached https://files.pythonhosted.org/packages/3a/fb/eb51731f2dc7c22d8e1a63ba88fb702727b324c6352183a32f27f73b8116/PyQt5-5.14.1.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/lib/python3.6/tokenize.py", line 452, in open
        buffer = _builtin_open(filename, 'rb')
    FileNotFoundError: [Errno 2] No such file or directory: '/tmp/pip-build-gzep4mr7/PyQt5/setup.py'

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-gzep4mr7/PyQt5/

我还从https://www.riverbankcomputing.com/software/pyqtchart/download下载了压缩文件,然后运行:

python3 configure.py --qmake /home/oo/Qt/5.14.0/gcc_64/bin/qmake
Error: Unable to import PyQt5.QtCore. Make sure PyQt5 is installed.

QT 截图: 在此输入图片描述

我的最终目标是使用 pyqt5 运行蜡烛图。

sudo python3 -m pip install pyqt5 pyqtchart
[sudo] password for oo:  
The directory '/home/oo/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/oo/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: pyqt5 in /usr/lib/python3/dist-packages
Requirement already satisfied: pyqtchart in /usr/local/lib/python3.6/dist-packages
Requirement already satisfied: PyQt5-sip<13,>=12.7 in /home/oo/.local/lib/python3.6/site-packages (from pyqtchart)

但仍然收到相同的错误:

Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyQt5
>>> import PyQt5.QtCore
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'PyQt5.sip'
>>> 

elementary OS 5.1 Hera - ooo
更新问题,我仍然得到相同的错误。 - ooo
我也将 /usr/local/lib/python3.6/dist-packages/ 中的所有PYQT包都删除了,然后安装了 pyqt5 pyqtchart pyqt5.sip sip 但是在导入 PyQt5.QtCore 时仍然出现错误 ModuleNotFoundError: No module named 'PyQt5.sip' - ooo
8个回答

70
我认为最初的pip安装问题是由于PyQt5切换到manylinux2014平台标记,用于最新版本(请参见PyPI上5.14.15.14.0的wheels)。只有pip版本>=19.3才能识别此平台标记(ref),因此,如果您使用较旧版本的pip,则会尝试从源代码安装。

避免源代码安装的两个简单选项:

  • 通过pip3 install --upgrade pip将pip更新到最新版本
  • 安装先前的版本,该版本使用manylinux1(pip3 install pyqt5==5.14.0

安装5.14.0版本后,问题得到了解决。 - Steven
在运行Pengwin Enterprise Scientific Linux的WSL上,pip3升级到21.0.1版本成功。 - skytaker
是的,升级一些东西可以解决90%的问题! - TheEagle
升级pip成功了,但是我错过了libxcb-util.so.1(即使找到了,也无法在“”中加载Qt平台插件“xcb”)。安装之前的版本(5.14.0)让我能够运行应用程序。谢谢。 - Ale

20

最新版的 PyQt5 在 pypi 上似乎存在一个 bug,因此我安装了 5.14 版本。

sudo apt-get update && \
    sudo apt-get autoclean

sudo apt-get update && sudo apt-get install \
    -y --no-install-recommends \
    python3-pip \
    python3-setuptools

sudo python3 -m pip install pyqt5==5.14 pyqtchart==5.14

复制我之前的回答中的示例到main.py,然后运行:

python3 main.py

我建议您搜索由失败尝试生成的文件夹和文件,并将其删除。

为了我的测试,我使用了以下Dockerfile


你为什么要使用sudo来运行python3 -m pip呢? - WesR

10

我也遇到了同样的问题,尝试安装ReText时无法安装PyQt5

Ubuntu 18.04上,使用Python 3.6.9Pip 9.0.1,我成功通过以下步骤pip安装了PyQt5

python3 -m venv env
source env/bin/activate
pip3 install pyqt5 --only-binary pyqt5

这已足以使pip下载PyQt5-5.14.0-5.14.0-cp35.cp36.cp37.cp38-abi3-manylinux1_x86_64.whl二进制轮(不需要/使用setup.py),而不是从源tarball构建。

根据https://pypi.org/project/PyQt5/#files,还有其他二进制轮,因此希望它可以覆盖大多数平台需求。


2
有时在Debian或Ubuntu Linux发行版上安装PyQt5非常困难。 我成功地在我的Debian 10 buster (stable)系统上为python3安装了它。 我使用apt软件包管理器进行了安装。
sudo apt-get update 
sudo apt-get install python3-pyqt5

对我来说,唯一有效的选项是,所有5.14.x和5.15版本的pip安装都因要求中的语法错误或缺少的文件而失败。 - Michael H

1
我也遇到了这个问题。这是因为你的 pip 版本过旧,你需要使用以下命令进行更新:
pip3 install setuptools wheel
pip3 install --upgrade pip
pip3 install --user pyqt5

0
我不得不从源代码构建正确版本的sip,以便从源代码构建所需版本的PyQt5,然后我还遇到了以下错误:
No module named 'PyQt5.sip'

然后

python3 -m pip install PyQt5.sip

问题已解决


0
如问题所示,您正在下载的是tar.zip文件而不是wheel。使用pip安装QT只允许使用wheel。请选择在PyPI主页上有wheel文件版本并与您的Python版本匹配的版本。

0
对于那些不急需5.14版本的人来说,5.12版本可以是一个解决方案。这对我有用:
sudo python3 -m pip install pyqt5==5.12 pyqtchart==5.12

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接