Python 如何在新的 virtualenv 上安装 PyQt5 并在 IDLE 上工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18042919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install PyQt5 on a new virtualenv and work on an IDLE
提问by Gouz
I installed PyQt5 globally on my win7 system (python 3.3), using the installer provided from the official riverbank website.
我使用官方 Riverbank 网站提供的安装程序在我的 win7 系统(python 3.3)上全局安装了 PyQt5。
Then i created a new –no-site-packages
virtualenv, where the only things i see listed after typing pip list
, are pip (1.4) and setuptools (0.9.7).
然后我创建了一个新的–no-site-packages
virtualenv,其中我在键入后看到的唯一内容pip list
是 pip (1.4) 和 setuptools (0.9.7)。
The problem now however, is that i need to install there the complete PyQt5 too and this seems impossible using the pip tool.
然而,现在的问题是,我也需要在那里安装完整的 PyQt5,而使用 pip 工具这似乎是不可能的。
Both pip install sip
and pip install PyQt5
inside the virtual enviroment are returning errors.
无论pip install sip
和pip install PyQt5
虚拟环境内部正在返回的错误。
Can someone provide a "how to" guide of what exactly should i do?
有人可以提供我应该做什么的“操作方法”指南吗?
I also want to be able to work with that PyQt5 (of the new virtualenv) from inside an IDLE, so
I copied the tcl folder from the global installation of my Python to thelocation of my virtual environment and also created a shortcut targeting: {location of my virtual enviroment}\Scripts\pythonw.exe C:\Python33\Lib\idlelib\idle.pyw
so i could open the virtualenv IDLE and not the global one. (Hope I did not do anything wrong there... correction maybe please.)
我还希望能够从 IDLE 内部使用 PyQt5(新的 virtualenv),所以我将 tcl 文件夹从我的 Python 的全局安装复制到我的虚拟环境的位置,并创建了一个快捷方式定位:{location of my virtual enviroment}\Scripts\pythonw.exe C:\Python33\Lib\idlelib\idle.pyw
所以我可以打开 virtualenv IDLE 而不是全局 IDLE。(希望我没有做错任何事情......请纠正。)
回答by boweeb
Both "pip install sip" and "pip install PyQt5" inside the virtual enviroment are returning errors.
虚拟环境中的“pip install sip”和“pip install PyQt5”都返回错误。
If the errors you're referring to are:
如果您所指的错误是:
Could not find any downloads that satisfy the requirement [pyqt5|sip]
Could not find any downloads that satisfy the requirement [pyqt5|sip]
and
和
No distributions at all found for [pyqt5|sip]
No distributions at all found for [pyqt5|sip]
Then this answershould shed light on this. Basically, PyPI is only providing a link to the homepage and/or source -- not a downloadable package or egg. Here are the links to PyQt5and SIPPyPI pages. I know it seems strange that a package manager wouldn't provide packages, but that's the way it is.
那么这个答案应该阐明这一点。基本上,PyPI 仅提供指向主页和/或源的链接——而不是可下载的包或鸡蛋。这是PyQt5和SIPPyPI 页面的链接。我知道包管理器不提供包似乎很奇怪,但事实就是这样。
You'll have to download, compile, and install sip and pyqt5 from source, inside your virtualenv. I wish I could provide details but I'm currently working through this myself.
您必须在 virtualenv 中从源代码下载、编译和安装 sip 和 pyqt5。我希望我能提供详细信息,但我目前正在自己解决这个问题。
回答by SpacemanSpiff
I faced the same issue installing PyQt5 and SIP. The solution for resolving this problem for PyQt4 as specified on Is it possible to add PyQt4/PySide packages on a Virtualenv sandbox?worked for me.
我在安装 PyQt5 和 SIP 时遇到了同样的问题。根据是否可以在 Virtualenv 沙箱上添加 PyQt4/PySide 包中指定的那样解决 PyQt4 的此问题的解决方案?为我工作。
I manually created symlinks to the PyQt5 and sip.so from within the virtualenv/site-packages directory. An ugly solution but gets things working.
我从 virtualenv/site-packages 目录中手动创建了指向 PyQt5 和 sip.so 的符号链接。一个丑陋的解决方案,但让事情发挥作用。
回答by Anon
How I got my PyQt5, Python33 Windows7x64 within a virtualenv working:
我如何在 virtualenv 中运行我的 PyQt5、Python33 Windows7x64:
Firstly, ensure you have matched 64 or 32 bit versions of everything. I am developing on a 64bit platform and am using 64bit versions. I did not fully test this with 32 bit. Please read thoroughly.
首先,确保您已匹配 64 位或 32 位版本的所有内容。我正在 64 位平台上开发并且使用 64 位版本。我没有用 32 位完全测试这个。请仔细阅读。
- Installed Python33 x64 using Windows installer binaries.
- Installed PyQt5 x64 from riverbank using the MSI installer into the defaultPython33. PIP won't work.
- Create a virtualenv using the Python33 as your base, no site packages.
- Copy the PyQt5 folder from Python33/Lib/site-packages/ into your virtualenv/Lib/site-packages.
- DO NOT DELETE THE PyQT5 folder!
- 使用 Windows 安装程序二进制文件安装了 Python33 x64。
- 使用 MSI 安装程序从河岸安装 PyQt5 x64 到默认的Python33。PIP 不起作用。
- 使用 Python33 作为基础创建一个 virtualenv,没有站点包。
- 将 PyQt5 文件夹从 Python33/Lib/site-packages/ 复制到您的 virtualenv/Lib/site-packages。
- 不要删除 PyQT5 文件夹!
Ensure PyQt5 is working on the base Python33 install:
确保 PyQt5 正在处理基本的 Python33 安装:
from PyQt5 import QtCore, QtGui, QtWidgets
Create a main window and launch it. If testing with PyQt4 code, a few classes have been swapped around. (Within QtGui and QtWidgets for example)
创建一个主窗口并启动它。如果使用 PyQt4 代码进行测试,则交换了一些类。(例如在 QtGui 和 QtWidgets 中)
from PyQt5 import QtGui, QtWidgets, QtCore
从 PyQt5 导入 QtGui、QtWidgets、QtCore
class Main(QtWidgets.QMainWindow):
def __init__(self):
print("Main __init__ fired")
QtWidgets.QMainWindow.__init__(self)
#Setup the UI
print("Setting up UI")
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def main():
print("fired main()")
#Setup the main application object
app = QtWidgets.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
#Run GUI
if __name__ == "__main__":
main()
If you get this error "..failed to start, could not find or load the Qt platform plugin "windows."(or similar), YOU MUST UPDATE your DirectX. Do this through Microsoft.
如果您收到此错误“..无法启动,无法找到或加载 Qt 平台插件“windows。”(或类似的),您必须更新您的 DirectX。通过 Microsoft 执行此操作。
Once you are entirely sure PyQt5 is working within Windows, then continue.
一旦您完全确定 PyQt5 可以在 Windows 中运行,请继续。
- Activate your virtualenv where you copied the PyQt5 folder to
Ensure PyQt5 can be imported from within your virtualenv. You may need to manipulate paths depending on what IDE you are using. Run an import in your virtualenv
from PyQt5 import QtCore, QtGui, QtWidgets
Try to run the same application again from inside this virtualenv
- 激活您将 PyQt5 文件夹复制到的 virtualenv
确保可以从您的 virtualenv 中导入 PyQt5。您可能需要根据您使用的 IDE 来操作路径。在您的 virtualenv 中运行导入
从 PyQt5 导入 QtCore、QtGui、QtWidgets
尝试从这个 virtualenv 内部再次运行相同的应用程序
OH NO! Another error with dlls!What gives? The paths to the QT dll libraries are wrong inside the virtualenv. It happens even if you tell PyQt5 to install directly to your virtualenv. You MUST add the paths to libraryPaths before creating the QApplication object. Here is a solution (Replace venvwith your virtualenv path):
不好了!dll 的另一个错误!是什么赋予了?在 virtualenv 中 QT dll 库的路径是错误的。即使您告诉 PyQt5 直接安装到您的 virtualenv,它也会发生。在创建 QApplication 对象之前,您必须将路径添加到 libraryPaths。这是一个解决方案(用您的 virtualenv 路径替换venv):
def main():
print("fired main()")
#ADD THE FOLLOWING BEFORE CREATING A QApplication()
QtCore.QCoreApplication.setLibraryPaths(['C:/venv/Lib/site-packages/PyQt5/plugins'])
#Ensure path was added and correct
print(QtCore.QCoreApplication.libraryPaths())
#Setup the main application object
app = QtWidgets.QApplication(sys.argv)
#See what the paths were
print(app.libraryPaths())
window = Main()
window.show()
sys.exit(app.exec_())
Now the code should run from all inside your virtualenv. You can delete the PyQt5 directory from your base install, or move it to another place if you need it in the future.
现在代码应该从你的 virtualenv 中运行。你可以从你的基础安装中删除 PyQt5 目录,或者如果你将来需要它,将它移到另一个地方。
There is most likely a way to boilerplate the path in a relative way, but this could create an issue with cx-freeze. Speaking of which.
很可能有一种方法可以以相对方式对路径进行样板化,但这可能会导致 cx-freeze 出现问题。说到这。
Now you're all done and you go to cx-freeze your PyQt5 app into a nice exe. You go to run it and OH NO!more dll troubles.
现在你已经完成了,你去 cx-freeze 你的 PyQt5 应用程序成一个不错的 exe。你去运行它,哦不!更多的dll麻烦。
You can manually copy libEGL.dllfrom site-packages/PyQt5 to the root build folder of your application, or add the following lines to your setup.py file for cx-freeze:
您可以手动将libEGL.dll从 site-packages/PyQt5 复制到应用程序的根构建文件夹,或将以下行添加到您的 setup.py 文件中以进行 cx-freeze:
import sys
from cx_Freeze import setup, Executable
#ADD THIS MUST INCLUDE FOR Windows 7.
include_files = [('c:/venv/Lib/site-packages/PyQt5/libEGL.dll', 'libEGL.dll')]
setup(
name="My App",
version="0.1",
description="PyQt5 App.",
#ADD THIS
options={'build_exe': {'include_files': include_files}},
executables=[Executable("c:/venv/src/myApp_main.py",
base="Win32GUI",
targetName="MyApp.exe")])
回答by Six
Assuming you have PyQt5 installed globally, there's a way you can give your virtualenv access to it without using --system-site-packages
. A package called vextmakes it possible. It works with other packages as well, but in this case we'll just be setting up the PyQt5 support.
假设您在全球范围内安装了 PyQt5,有一种方法可以让您的 virtualenv 访问它,而无需使用--system-site-packages
. 一个名为vext的包使之成为可能。它也适用于其他包,但在这种情况下,我们将只设置 PyQt5 支持。
Run the following command from within your virtualenv and you should be good to go:
从您的 virtualenv 中运行以下命令,您应该很高兴:
pip install vext.pyqt5
To check the status, run vext -l
. You should see something like the following:
要检查状态,请运行vext -l
。您应该会看到类似以下内容:
Running in virtualenv [enabled]
pyqt5.vext
Or, run vext -c pyqt5.vext
. You should see something like the following:
或者,运行vext -c pyqt5.vext
。您应该会看到类似以下内容:
import sip: [success]
import PyQt5: [success]
Alternatively you can just try to import PyQt5, e.g. python -c 'import PyQt5'
.
或者,您可以尝试导入 PyQt5,例如python -c 'import PyQt5'
.
回答by Richard Ayling
Anon's solution of adding a Qt libraryPath worked for me. I am using Anaconda3 on Windows. But I found an alternative.
Anon 添加 Qt libraryPath 的解决方案对我有用。我在 Windows 上使用 Anaconda3。但我找到了一个替代方案。
Copy the file …\Anaconda3\qt.conf
to the Scripts
folder in the virtual environment. Now I don't need to change any Python code.
将文件复制…\Anaconda3\qt.conf
到Scripts
虚拟环境中的文件夹中。现在我不需要更改任何 Python 代码。
The conf file seems to have been created by …\Anaconda3\Scripts\.qt-post-link.bat
.
conf 文件似乎是由…\Anaconda3\Scripts\.qt-post-link.bat
.