已通过pip安装的模块在Python3可用,Python2不可用,如何解决?
I’ve run into this exact Python package isolation issue before, so let me walk you through what’s going on and how to fix it:
The Problem Scenario
- You successfully installed the
aptedmodule (version 1.0.3) using:pip install apted - Testing with Python 2.7 threw an error:
python -m apted -t {a{b}{c}} {a{b{d}}} -mv # Output: No module named apted - But the same test worked flawlessly with Python 3:
python3 -m apted -t {a{b}{c}} {a{b{d}}} -mv # Outputs distance, runtime, and tree transformation results as expected
Why This Happens
In environments with both Python 2.7 and Python 3 installed, the pip command usually defaults to linking with your system’s Python 3 installation. That means when you run pip install apted, the module gets placed in Python 3’s site-packages directory—completely unreachable for Python 2.7, even though the module claims compatibility with 2.7.
The Fix
The solution is simple: explicitly use Python 2.7’s dedicated pip executable to install the module. Run this command:
pip2 install apted
This ensures apted is installed directly into Python 2.7’s package directory, making it accessible whenever you use the python (Python 2.7) interpreter.
内容的提问来源于stack exchange,提问作者sten

