杰瑞科技汇

Mac下Python2.7如何安装PyQt?

Of course. Here is a comprehensive guide to setting up and using Python 2.7 with PyQt on a Mac.

Mac下Python2.7如何安装PyQt?-图1
(图片来源网络,侵删)

This is a legacy setup, as Python 2.7 reached its official end-of-life in 2025, and PyQt5/6 are the modern standards. However, this is necessary for maintaining or running very old applications.

⚠️ Important Warning: Why You Should Avoid This

Before we begin, it's crucial to understand the context:

  • Python 2.7 is End-of-Life: It no longer receives security updates, bug fixes, or improvements. Using it in a production environment is a security risk.
  • Qt5/6 are Modern: Modern versions of Qt (the framework behind PyQt) are no longer compatible with Python 2.7.
  • PyQt4 is the Target: For Python 2.7, you must use PyQt4, not PyQt5 or PyQt6. PyQt4 is also ancient and unsupported.

This guide is for legacy maintenance only. For any new project, you should absolutely use a modern version of Python (3.8+) and PyQt5 or PyQt6.


Step 1: Install Python 2.7

The easiest and recommended way to manage multiple Python versions on a Mac is with a version manager like pyenv.

Mac下Python2.7如何安装PyQt?-图2
(图片来源网络,侵删)

Method 1: Using pyenv (Recommended)

  1. Install Homebrew (if you don't have it): Open Terminal and run:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Install pyenv using Homebrew:

    brew install pyenv
  3. Configure your Shell: You need to add pyenv to your shell's path. The commands depend on your shell (usually Zsh for modern macOS or Bash for older versions).

    For Zsh (macOS Catalina and later):

    Mac下Python2.7如何安装PyQt?-图3
    (图片来源网络,侵删)
    echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
    echo 'eval "$(pyenv init -)"' >> ~/.zshrc

    For Bash (macOS Mojave and earlier):

    echo 'eval "$(pyenv init --path)"' >> ~/.bash_profile
    echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
  4. Restart your Terminal or run source ~/.zshrc (or source ~/.bash_profile).

  5. Install Python 2.7: pyenv will build Python from source, which can take a few minutes.

    pyenv install 2.7.18
  6. Set Python 2.7 as your Global Version (Optional): If you want this to be the default Python for your user account, run:

    pyenv global 2.7.18

    You can verify the installation:

    python --version
    # Should output: Python 2.7.18
    pip --version
    # Should output: pip X.X.X from ...

Method 2: Using Official Installer

  1. Go to the official Python 2.7 download page: Python 2.7.18 Release
  2. Download the macOS 64-bit installer (e.g., python-2.7.18-macosx10.9.pkg).
  3. Run the installer. It will install Python 2.7 to /Library/Frameworks/Python.framework/Versions/2.7/.
  4. The installer usually adds the correct paths to your shell configuration files automatically. You may need to restart your Terminal.

Step 2: Install PyQt4 for Python 2.7

Now that you have Python 2.7, you need to install PyQt4. The best tool for this is pip.

  1. Ensure pip is up-to-date for Python 2.7:

    pip install --upgrade pip
  2. Install PyQt4: This command will download and compile PyQt4 against your Python 2.7 installation.

    pip install PyQt4

    Note: The compilation can sometimes fail due to missing Xcode command-line tools. If you encounter errors, run:

    xcode-select --install

    Then try the pip install command again.

  3. Verify the Installation: Run a simple Python script to check if the imports work.

    Create a file named test_qt.py:

    import sys
    from PyQt4 import QtGui, QtCore
    print("PyQt4 version:", QtCore.PYQT_VERSION_STR)
    print("Qt version:", QtCore.QT_VERSION_STR)
    app = QtGui.QApplication(sys.argv)
    window = QtGui.QMainWindow()
    window.setWindowTitle("PyQt4 on macOS Test")
    window.setGeometry(100, 100, 400, 300)
    window.show()
    sys.exit(app.exec_())

    Run it from your Terminal:

    python test_qt.py

    If a small window titled "PyQt4 on macOS Test" appears, your installation is successful!


Step 3: Running Your PyQt4 Application

You have two primary ways to run your scripts.

A. Using the Terminal (Command Line)

This is the most straightforward method. Navigate to your project directory in the Terminal and run:

python your_script.py

Example:

cd /Users/yourname/Desktop/my_pyqt_app
python my_app.py

B. Creating a Standalone .app Bundle (for Distribution)

To share your application with others who might not have Python/PyQt installed, you can package it into a standard macOS .app bundle. The best tool for this is py2app.

  1. Install py2app:

    pip install py2app
  2. Create a setup.py file: In the same directory as your main script, create a file named setup.py. This file tells py2app how to package your application.

    For our test_qt.py script, the setup.py would look like this:

    from setuptools import setup
    APP = ['test_qt.py']
    DATA_FILES = []
    OPTIONS = {
        'argv_emulation': False,
        'iconfile': None, # Optional: path to your .icns icon file
    }
    setup(
        app=APP,
        data_files=DATA_FILES,
        options={'py2app': OPTIONS},
        setup_requires=['py2app'],
    )
  3. Build the Application: From your Terminal, in the same directory, run the build command:

    python setup.py py2app
  4. Find Your Application: After the build process completes, a new dist folder will be created. Inside, you will find your application, named test_qt.app.

    You can now double-click test_qt.app to run it, or share it with others.


Troubleshooting Common Issues

  • "ImportError: No module named PyQt4":

    • Cause: You are likely running the wrong Python interpreter (e.g., the system Python 2.7 or a different Python 3 version).
    • Solution: Check your active Python version with which python and python --version. If you used pyenv, ensure you are in the correct shell session where pyenv global 2.7.18 was set.
  • Build errors during pip install PyQt4 or py2app:

    • Cause: Missing build dependencies like Xcode Command Line Tools or other libraries.
    • Solution: Run xcode-select --install to get the necessary compilers and headers.
  • Application crashes on launch (from .app bundle):

    • Cause: Often related to missing Qt plugins (like the style plugins). The .app bundle might not be able to find them.
    • Solution: You can try using a tool like delocate to copy the necessary Qt frameworks into your bundle's Frameworks directory, or use a more advanced py2app configuration to specify plugin directories. This is a common challenge with legacy Qt packaging.
分享:
扫描分享到社交APP
上一篇
下一篇