Python:来自导入错误
我在 Ubuntu 10.10 上运行 Python 2.6.6。
我知道我们可以导入一个模块并将该模块绑定到不同的名称,例如
import spam as eggs
,
from eggs import spam as foo
我的问题是在运行 PySide 示例,以下导入代码不会运行:
import PySide as PyQt4
from PyQt4 import QtCore, QtGui
它会生成导入错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PyQt4
显然,根据 Python 解释器,上述代码不正确,我的问题是为什么它不正确,或者为什么这不起作用?
I'm running Python 2.6.6 on Ubuntu 10.10.
I understand that we can import a module and bind that module to a different name, e.g.
import spam as eggs
also,
from eggs import spam as foo
My problem is that when running the PySide examples, the following import code does not run:
import PySide as PyQt4
from PyQt4 import QtCore, QtGui
It generates an import error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PyQt4
Clearly, according to the Python interpreter the above code is incorrect, my question is why is it incorrect or rather why doesn't this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
import
和from
是一种特殊的语法。他们寻找模块名称,这意味着 sys.path 中以模块名称开头的文件。
而且好像你没有安装PyQt4,所以会失败。
事实上,在运行
import PySide as PyQt4
后,您的命名空间中有一个名为PyQt4
的变量,这不会改变任何内容,Python 仍在寻找名为PyQt4 的实际模块
当您执行from PyQt4 import QtCore, QtGui
时。尝试做
或
那应该是等效的。
import
andfrom
are a special syntax.They look for a module name, which means a file in
sys.path
which starts with the module name.And it seems like you don't have PyQt4 installed, so it will fail.
The fact that you have a variable called
PyQt4
in your namespace after runningimport PySide as PyQt4
does not change anything, Python is still looking for an actual module calledPyQt4
when you dofrom PyQt4 import QtCore, QtGui
.Try doing
or
That should be equivalent.
我刚刚安装了 PySide 并正在做一个教程,其中所有示例都使用 PyQt4。我厌倦了将导入从 PyQt4 更改为 PySide,因此我只是使用以下步骤在我的站点包中创建了一个符号链接:
1)肯定有更好的方法,但我通过打开 shell 并运行找到了我的 python 包的安装位置python,然后在交互式解释器中输入:
2)然后我在其中一个目录中找到 PySide 并 cd 到它(注意它位于 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2 .7/site-packages(如果您在 Mac OSX Leopard 10.5.8 上使用适用于 python 2.7 的 macports PySide 安装)。
3)然后我用ln创建了一个符号链接,在我的例子中我必须使用sudo:
就是这样,现在我可以像平常一样使用:
快乐的日子!
显然,如果你想安装 PyQt4,你应该首先 rm PyQt4 符号链接。
另一个警告:我上面描述的内容在很多方面可能都是错误/不好的 - 我不是 Python 安装方面的专家,但到目前为止对我来说还可以。 YMMV,因此使用风险需您自担。希望很快有人会评论说“不,非常糟糕!”或者理想情况下“是的,别担心,我们很酷……”
I just installed PySide and was doing a tutorial where all the examples used PyQt4. I got tired of changing the imports from PyQt4 to PySide so I just made a symlink in my site-packages, using the following steps:
1) There's surely a better way but I found where my python packages were installed by opening a shell and running python, then at the interactive interpreter typed:
2) I then found PySide in one of the directories and cd'd to it (n.b. It's at /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages if you're using the macports PySide install for python 2.7 on Mac OSX Leopard 10.5.8).
3) Then I made a symlink with ln, in my case I had to use sudo:
That's it, now I can just use:
as normal - happy days!
Obviously, if you ever want to install PyQt4 you should rm the PyQt4 symlink first.
Another caveat: What I've described above may well be wrong/bad in many ways - I am no expert at Python installs but so far it's ok for me. YMMV so use at your own risk. Hopefully someone will comment soon to say "no, very bad!" or ideally "yeah don't sweat it, we cool.."