如何在Conda Python 2环境中安装wxPython 3并解决安装异常
Alright, let's break down your problem into two clear parts: installing wxPython 3 in your Conda Python 2 environment, and fixing the issue where Conda doesn't recognize the wxPython you installed via brew. Here's how to tackle each step:
First, make sure you're actively working in your Python 2 Conda environment. For newer Conda versions:
conda activate your_py2_environment_name
For older Conda versions (pre-4.6):
source activate your_py2_environment_name
Option 1: Use conda-forge (cleanest method)
Conda-forge maintains a library of legacy packages, including older wxPython builds compatible with Python 2. Try running this command:conda install -c conda-forge wxpython=3.0If this succeeds, you're all set! This installs wxPython 3.0 directly into your isolated Conda environment, avoiding conflicts with system or other Python setups.
Option 2: Install from SourceForge source code (if conda-forge fails)
The pre-built installers from SourceForge are tied to your system's default Python, which is why you saw those confusing errors. Instead, use the source package to install directly into your Conda environment:- Download the wxPython 3.0 source archive for Python 2 from SourceForge (pick the version matching your OS).
- Extract the archive, then navigate to the extracted folder in your terminal.
- With your Conda environment activated, run:
This compiles and installs wxPython 3.0 directly into your Conda environment'spython setup.py installsite-packagesdirectory, so it's only available to this environment.
The core issue here is that Conda environments are fully isolated from your system's Python (including brew-installed packages). Conda's Python only looks for packages in its own environment's site-packages folder, not the system-wide location where brew installs software.
You have two choices:
Option 1: Avoid this entirely (highly recommended)
Stick with the methods above to install wxPython 3.0 directly into your Conda environment. This keeps your environment clean and prevents dependency conflicts between system and Conda packages.Option 2: Force Conda to recognize the brew package (last resort)
If you absolutely need to use the brew-installed version, you can add the systemsite-packagespath to your Conda environment's Python path:- Activate your Conda environment.
- Run this command to find your system Python's
site-packageslocation:python -c "import sys; print([p for p in sys.path if 'site-packages' in p and not 'conda' in p][0])" - Navigate to your Conda environment's
site-packagesdirectory (find it withpython -c "import site; print(site.getsitepackages()[0])"). - Create a new file named
wx_brew.pth(the name doesn't matter as long as it ends with.pth), and paste the systemsite-packagespath you found into it. - Restart your Python session in the Conda environment—it should now detect the brew-installed wxPython.
Note: This can cause unexpected conflicts if system packages and Conda packages are incompatible, so only use this if you have no other option.
A quick side note: The reason pip isn't working is that wxPython 4.x dropped Python 2 support, and the 3.0 versions were removed from PyPI. So pip install wxpython defaults to the latest 4.x version, which won't work with your Python 2 environment—hence why you need the Conda or source installation methods above.
内容的提问来源于stack exchange,提问作者HappyFace




