简体   繁体   English

ImportError:没有名为PyQt4的模块

[英]ImportError: No module named PyQt4

I installed pyqt4 by using Homebrew. 我使用Homebrew安装了pyqt4。 But when I import PyQt4 in python interpreter, It said that "No module named PyQt4". 但是当我在python解释器中导入PyQt4时,它说“没有名为PyQt4的模块”。 Can somebody help me with that? 有人可以帮我吗?

After brew install pyqt , you can brew test pyqt which will use the python you have got in your PATH in oder to do the test (show a Qt window). brew install pyqt ,您可以brew test pyqt ,它将使用您在PATH路径中获得的python进行测试(显示Qt窗口)。

For non-brewed Python, you'll have to set your PYTHONPATH as brew info pyqt will tell. 对于未酿造的Python,您必须按照brew info pyqt会告诉您的那样设置PYTHONPATH。

Sometimes it is necessary to open a new shell or tap in order to use the freshly brewed binaries. 有时有必要打开一个新的外壳或点击以使用新鲜酿造的二进制文件。

I frequently check these issues by printing the sys.path from inside of python: python -c "import sys; print(sys.path)" The $(brew --prefix)/lib/pythonX.Y/site-packages have to be in the sys.path in order to be able to import stuff. 我经常通过从python内部打印sys.path来检查这些问题: python -c "import sys; print(sys.path)" $(brew --prefix)/lib/pythonX.Y/site-packages必须位于sys.path中,以便能够导入内容。 As said, for brewed python, this is default but for any other python, you will have to set the PYTHONPATH . 如前所述,对于brewed python,这是默认设置,但是对于其他任何python,您都必须设置PYTHONPATH

You have to check which Python you are using. 您必须检查使用的是哪个Python。 I had the same problem because the Python I was using was not the same one that brew was using. 我遇到了同样的问题,因为我使用的Python与brew使用的Python不同。 In your command line: 在您的命令行中:

  1. which python
    output: /usr/bin/python 输出:/ usr / bin / python
  2. which brew
    output: /usr/local/bin/brew //so they are different 输出:/ usr / local / bin / brew //因此它们是不同的
  3. cd /usr/local/lib/python2.7/site-packages
  4. ls //you can see PyQt4 and sip are here ls //您可以在此处看到PyQt4和sip
  5. Now you need to add usr/local/lib/python2.7/site-packages to your python path. 现在,您需要将usr/local/lib/python2.7/site-packages到您的python路径。
  6. open ~/.bash_profile //you will open your bash_profile file in your editor open ~/.bash_profile //您将在编辑器中打开bash_profile文件
  7. Add 'export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH' to your bash file and save it 'export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH'到您的bash文件中并保存
  8. Close your terminal and restart it to reload the shell 关闭终端,然后重新启动以重新加载外壳
  9. python
  10. import PyQt4 // it is ok now import PyQt4 //现在可以

If you're using Anaconda to manage Python on your system, you can install it with: 如果您正在使用Anaconda在系统上管理Python,则可以使用以下命令进行安装:

$ conda install pyqt=4

Omit the =4 to install the most current version. 忽略=4以安装最新版本。

Answer from How to install PyQt4 in anaconda? 如何在Anaconda中安装PyQt4的答案

I solved the same problem for my own program by installing python3-pyqt4 . 我通过安装python3-pyqt4为自己的程序解决了相同的问题。

I'm not using Python 3 but it still helped. 我没有使用Python 3,但仍然有帮助。

It is likely that you are running the python executable from /usr/bin (Apple version) instead of /usr/loca/bin (Brew version) 您可能是从/ usr / bin(Apple版本)而不是/ usr / loca / bin(Brew版本)运行python可执行文件

You can either 你可以

a) check your PATH variable a)检查您的PATH变量

or 要么

b) run brew doctor b) brew doctor

or 要么

c) run which python c)运行which python

to check if it is the case. 检查情况是否如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM