Multiple python version installs
Jump to navigation
Jump to search
同一電腦有兩種版本的 python
安裝兩種版本的 Python
Windows
Windows 電腦已經安裝 Python2, 想要安裝 Python3
- 直接從 Python 網站 下載安裝 Python3 即可
- 安裝完 Windows 版本 Python3,已經內建 pip,無需額外安裝 pip
Mac
因為 Mac 內建 Python2, 所以需要額外安裝 Python3
- 請參考 Installing Python 3 on Mac OS X — The Hitchhiker's Guide to Python & Mac OSX 正確地同時安裝 Python 2.7 和 Python3 – 字串小豬
- 安裝完 Mac 版本 Python3,已經內建 pip,無需額外安裝 pip
Linux
已經安裝 Python2, 需要額外安裝 Python3
確認安裝的 python 版本
文章中行首的 $ 符號代表 Windows 作業系統輸入指令時,要先開啟「命令提示字元」輸入 cmd [1]、Mac 或 Linux 輸入指令時,要先開啟「終端機」、terminal 或 console。
Windows
確認安裝版本[2]
$ python -V $ python -version $ py -2 -V 預期看到 Python 2.7.8 或其他 2.x 版本 $ py -V $ py -3 -V 預期看到 Python 3.6.4 或其他 3.x 版本
Mac & Linux
$ python -V $ python -version 預期看到 Python 2.7.10 或其他 2.x 版本 $ python3 -V $ python3 -version 預期看到 Python 3.6.4 或其他 3.x 版本
如果不小心打到小寫 v 也就是 python -v,會看到一長串指令,最後游標停在 >>> 符號前面。有兩種方式就可以離開 >>> 世界 (python 互動模式[3]): (1) 輸入 exit() 或按組合鍵 ctrl+d (2) 再按 Enter 或 return 按鍵
確認 pip (package_manager) 版本
Windows
確認 pip 版本
$ c:\Python27\Scripts\pip.exe -V pip 9.0.1 from c:\python27\lib\site-packages (python 2.7) 代表是可以用 c:\Python27\Scripts\pip.exe 安裝 python2 的套件 $ pip -V $ pip3 -V pip 9.0.1 from c:\users\user\appdata\local\programs\python\python36\lib\site-packages (python 3.6) 代表是可以用 pip 或 pip3 安裝 python3 的套件
Mac & Linux
確認 pip 版本
$ pip -V $ pip2 -V pip 9.0.1 from /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7) 代表是可以用 pip 或 pip2 安裝 python2 的套件 $ pip3 -V pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6) 代表是可以用 pip3 安裝 python3 的套件
更新 pip (package_manager) 版本[4]
使用 pip 安裝套件時遇到訊息
You are using pip version 9.0.3, however version 18.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
Windows
已知 py -2 -V 版本是 python2 $ py -2 -m pip install --upgrade pip 已知 py -3 -V 版本是 python2 $ py -3 -m pip install --upgrade pip
Mac & Linux
確認 pip 版本
已知 pip -V 版本是 python2 $ pip install --upgrade pip 已知 pip3 -V 版本是 python3 $ pip3 install --upgrade pip setuptools wheel
安裝或移除 python 套件
Windows
安裝套件
手動切換到 python 2.x 版本的目錄 $ c:\Python27\Scripts\pip.exe install <package> $ c:\Python27\Scripts\pip.exe install -r requirements.txt 已知 pip、pip3 支援 python3 的套件 $ pip3 install <package>
移除套件
已知 pip3 支援 Python3 的套件 $ pip remove <package>
Mac & Linux
安裝套件
已知 pip 或 pip2 支援安裝 python2 的套件 $ pip install <package> $ pip2 install <package> 已知 pip3 支援安裝 python3 的套件 $ pip3 install <package>
移除套件
已知 pip3 支援 Python3 的套件 $ pip3 remove <package>
執行 python 程式
python 程式碼需要指定要執行的版本
Windows
已經確認 python -V 是 2.x 版本
python script_for_version_2.py
3. Using Python on Windows — Python 3.6.5 documentation
# python 2.x 版本 py -2 script_for_version_2.py py -2.7 script_for_version_2.py # python 3.x 版本 py -3 script_for_version_3.py # 已經確認 py -V 是 3.x 版本 py script_for_version_3.py
Mac
已經確認 python -V 是 2.x 版本
python script_for_version_2.py
已經確認 python3 -V 是 3.x 版本
py script_for_version_3.py
尋找命令對應的完整路徑
Windows
$ where python C:\Python27\python.exe $ where py C:\Windows\py.exe
$ where pip $ where pip3 C:\Users\User\AppData\Local\Programs\Python\Python36\Scripts\pip.exe 代表是可以用 pip 或 pip3 安裝 python3 的套件
如果遇到「'xxx' 不是內部或外部命令、可執行的程式或批次檔。」 代表安裝時沒有設定 PATH 變數,詳 How to setup my system path
Mac & Linux
$ where python $ which python /usr/bin/python $ where python3 $ which python3 /usr/local/bin/python3
$ where pip $ which pip /usr/local/bin/pip $ where pip3 $ which pip3 /usr/local/bin/pip3
Python version on docker images
- library/docker - Docker Hub python v. 2.7.15 [Last visited: 2018-08-30]
# install python3 on ubuntu:latest image
image: ubuntu:latest
before_script:
- apt-get update -qy
- apt-get install -y build-essential libpq-dev libssl-dev openssl libffi-dev zlib1g-dev
- apt-get install -y python3
- apt-get install -y python3-pip python3-dev
# - apt-get install -y python-dev python-pip
- python3 -V
- pip3 -V
- pip3 install -r requirements.txt
References
References
Related articles