已通过pip安装iPython,如何让系统区分iPython与iPython3并使其运行Python2?
Got it, let's work through this step by step so you can have ipython launch Python 2 by default, while keeping ipython3 tied to Python 3.
Step 1: Install IPython for Python 2
First, you need a dedicated IPython instance for Python 2. Depending on your system setup, use one of these commands:
- If your system has
pip2(the Python 2-specific pip):pip2 install ipython - If
pip2isn't available, use Python 2's interpreter to call pip directly:python2 -m pip install ipython
This installs the Python 2-compatible IPython, and in most cases, it will automatically set ipython to point to this version, leaving ipython3 linked to your Python 3 installation.
Step 2: Verify and Fix Command Associations
Next, check which Python version each ipython command uses:
# Check ipython's Python version ipython -c "import sys; print('IPython is using Python:', sys.version.split()[0])" # Check ipython3's Python version ipython3 -c "import sys; print('IPython3 is using Python:', sys.version.split()[0])"
If ipython still launches Python 3, your system's PATH is prioritizing the Python 3 version. Fix this with an alias:
- Open your shell config file (e.g.,
~/.bashrcfor Bash,~/.zshrcfor Zsh) in a text editor. - Add this line at the end:
alias ipython='python2 -m ipython' - Save the file and reload the config to apply changes right away:
source ~/.bashrc # Use ~/.zshrc if you're using Zsh
Now ipython will default to Python 2, and ipython3 will keep using Python 3.
Quick Alternative (No Global Changes)
If you don't want to modify your shell config, you can always launch Python 2's IPython directly with:
python2 -m ipython
This bypasses any PATH priorities and launches the correct version every time.
内容的提问来源于stack exchange,提问作者Daniel W.

