Editing
Multiple python version installs
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
如何在同一電腦,安裝兩種版本的 python == 安裝兩種版本的 Python == === Windows, Mac, Linux 安裝多種版本的 Python 法1 === * 安裝 [https://www.docker.com/get-started Docker Desktop] on {{Win}}, {{Mac}} or {{Linux}} & 安裝 container image *# 安裝 [https://hub.docker.com/_/python python] container image or *# [https://hub.docker.com/r/conda/miniconda3/ conda/miniconda3] or *# [https://hub.docker.com/r/condaforge/miniforge3/tags condaforge/miniforge3 Tags | Docker Hub] * [https://www.anaconda.com/products/individual Anaconda] on {{Win}}, {{Mac}} or {{Linux}}: [https://medium.com/python4u/%E7%94%A8conda%E5%BB%BA%E7%AB%8B%E5%8F%8A%E7%AE%A1%E7%90%86python%E8%99%9B%E6%93%AC%E7%92%B0%E5%A2%83-b61fd2a76566 用conda建立及管理python虛擬環境 - Python4U - Medium] * 安裝 [https://www.virtualbox.org/wiki/Downloads Downloads – Oracle VM VirtualBox] on {{Win}}, {{Mac}} or {{Linux}} & 安裝 [https://www.linuxvmimages.com/ Linux VM Images - Download VirtualBox and VMware Images] {{exclaim}} Not support Apple Silicon on {{Mac}}<ref>[https://forums.virtualbox.org/viewtopic.php?t=98742 virtualbox.org • View topic - ARM Macs: Will Virtualbox be ported ?]</ref> * 安裝 [[Podman]] === Windows 安裝兩種版本的 Python 法2 === Windows 電腦已經安裝 Python2, 想要安裝 Python3 * 直接從 [https://www.python.org/downloads/ Python 網站] 下載安裝 Python3 即可 * [https://www.microsoft.com/zh-tw/p/ubuntu/9nblggh4msv6?SilentAuth=1&wa=wsignin1.0#activetab=pivot:overviewtab 購買 Ubuntu on Windows - Microsoft Store zh-TW],已經內建 Python3,需額外安裝 pip3 * 安裝完 Windows 版本 Python3,已經內建 pip,無需額外安裝 pip {{Tips}} Python2 或 Python3 的安裝前後順序會造成的影響是 {{kbd | key=<nowiki>python -V </nowiki>}} 顯示的是先安裝的版本,可依照下文說明指定要執行的版本。 === Mac 安裝兩種版本的 Python 法2 === 因為 Mac 內建 Python2, 所以需要額外安裝 Python3 * 請參考 [http://docs.python-guide.org/en/latest/starting/install3/osx/ Installing Python 3 on Mac OS X — The Hitchhiker's Guide to Python] & [https://stringpiggy.hpd.io/mac-osx-python3-dual-install/ Mac OSX 正確地同時安裝 Python 2.7 和 Python3 – 字串小豬] * 安裝完 Mac 版本 Python3,已經內建 pip,無需額外安裝 pip === Linux 安裝兩種版本的 Python 法2 === 通常 Linux 已經內建安裝 Python2, 需要額外安裝 Python3 * [https://oranwind.org/python-python-an-zhuang-yu-ji-chu-jiao-xue/ CentOS 7 安裝 Python 3.6] * [https://www.centos.bz/2018/03/centos-7%E5%AE%89%E8%A3%85pip3/ CentOS 7 安装 pip3 - Linux运维日志] == 確認安裝的 python 版本 == {{Tips}} 文章中行首的 $ 符號代表 Windows 作業系統輸入指令時,要先開啟「命令提示字元」輸入 {{kbd | key=cmd}} <ref>[https://www.590103.idv.tw/blog/%E5%9C%A8windows-10%E4%B8%AD%E6%89%93%E9%96%8B%E5%91%BD%E4%BB%A4%E6%8F%90%E7%A4%BA%E5%AD%97%E5%85%83%E7%9A%8410%E7%A8%AE%E6%96%B9%E5%BC%8F 在Windows 10中打開命令提示字元的10種方式 - 笨笨包的天空]</ref>、Mac 或 Linux 輸入指令時,要先開啟「終端機」、terminal 或 console。 === Windows === 確認安裝版本<ref>[https://docs.python.org/3/using/windows.html#launcher 3. Using Python on Windows — Python 3.6.5 documentation]</ref> <pre> $ python -V $ python -version $ py -2 -V 預期看到 Python 2.7.8 或其他 2.x 版本 $ py -V $ py -3 -V 預期看到 Python 3.6.4 或其他 3.x 版本 </pre> === Mac & Linux === <pre> $ python -V $ python -version $ /usr/bin/env python -V 預期看到 Python 2.7.10 或其他 2.x 版本 $ python3 -V $ python3 -version $ /usr/bin/env python3 -V 預期看到 Python 3.6.4 或其他 3.x 版本 </pre> {{Tips}} 如果不小心打到小寫 {{kbd | key=<nowiki>v</nowiki>}} 也就是 {{kbd | key=<nowiki>python -v</nowiki>}},會看到一長串指令,最後游標停在 {{kbd | key=<nowiki>>>></nowiki>}} 符號前面。有兩種方式就可以離開 {{kbd | key=<nowiki>>>></nowiki>}} 世界 (python 互動模式<ref>[https://stackoverflow.com/questions/16420078/what-do-the-three-arrow-signs-mean-in-python What do the three arrow (">>>") signs mean in python? - Stack Overflow]</ref>): (1) 輸入 {{kbd | key=<nowiki>exit()</nowiki>}} 或按組合鍵 {{kbd | key=<nowiki>ctrl+d</nowiki>}} (2) 再按 {{kbd | key=<nowiki>Enter</nowiki>}} 或 {{kbd | key=<nowiki>return</nowiki>}} 按鍵 == 確認 pip (package_manager) 版本 == === Windows === 確認 pip 版本 <pre> $ 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 的套件 </pre> === Mac & Linux === 確認 pip 版本 <pre> $ 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 的套件 </pre> == 更新 pip (package_manager) 版本<ref>[http://softwaretester.info/install-and-upgrade-pip-on-mac-os-x/ Install and upgrade pip on Mac OS X | - Softwaretester -]</ref> == 使用 pip 安裝套件時遇到訊息 <pre> 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. </pre> === Windows === <pre> 已知 py -2 -V 版本是 python2 $ py -2 -m pip install --upgrade pip 已知 py -3 -V 版本是 python2 $ py -3 -m pip install --upgrade pip </pre> === Mac & Linux === 確認 pip 版本 <pre> 已知 pip -V 版本是 python2 $ pip install --upgrade pip 已知 pip3 -V 版本是 python3 $ pip3 install --upgrade pip setuptools wheel </pre> == 安裝或移除 python 套件 == === Windows === 安裝套件<ref>[https://stackoverflow.com/questions/48720833/could-not-find-a-version-that-satisfies-the-requirement-tensorflow python - Could not find a version that satisfies the requirement tensorflow - Stack Overflow]</ref> <pre> Python2: 以下以 Python 2.7 版本,安裝目錄在 c:\Python27\ 為例 $ c:\Python27\Scripts\pip.exe install <package> $ c:\Python27\Scripts\pip.exe install -r requirements.txt $ py -2 -m pip install <package> Python3: 已知 pip、pip3 支援 python3 的套件 $ pip3 install <package> $ py -m pip install <package> </pre> 移除套件 <pre> 已知 pip3 支援 Python3 的套件 $ pip3 remove <package> </pre> === Mac & Linux === 安裝套件 <pre> 已知 pip 或 pip2 支援安裝 python2 的套件 $ pip install <package> $ pip2 install <package> 已知 pip3 支援安裝 python3 的套件 $ pip3 install <package> </pre> 移除套件 <pre> 已知 pip3 支援 Python3 的套件 $ pip3 remove <package> </pre> == 執行 python 程式 == python 程式碼需要指定要執行的版本 === Windows === 已經確認 python -V 是 2.x 版本 <pre> python script_for_version_2.py </pre> [https://docs.python.org/3/using/windows.html#python-launcher-for-windows 3. Using Python on Windows — Python 3.6.5 documentation] <pre> # 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 </pre> === Mac === 已經確認 python -V 是 2.x 版本 <pre> python script_for_version_2.py </pre> 已經確認 python3 -V 是 3.x 版本 <pre> py script_for_version_3.py </pre> == 尋找命令對應的完整路徑 == === Windows === <pre> $ where python C:\Python27\python.exe $ where py C:\Windows\py.exe </pre> <pre> $ where pip $ where pip3 C:\Users\User\AppData\Local\Programs\Python\Python36\Scripts\pip.exe 代表是可以用 pip 或 pip3 安裝 python3 的套件 </pre> {{Tips}} 如果遇到「'xxx' 不是內部或外部命令、可執行的程式或批次檔。」 代表安裝時沒有設定 PATH 變數,詳 [[How to setup my system path]] === Mac & Linux === <pre> $ where python $ which python /usr/bin/python $ where python3 $ which python3 /usr/local/bin/python3 </pre> <pre> $ where pip $ which pip /usr/local/bin/pip $ where pip3 $ which pip3 /usr/local/bin/pip3 </pre> == Python version on docker images == === comparison of docker images === * [https://hub.docker.com/r/continuumio/anaconda3 continuumio/anaconda3]: Python 3.12.7 {{access | date=2025-01-22}} * continuumio/anaconda3:2022.10: Python 3.9.13 * continuumio/anaconda3:2022.05: Python 3.9.12 * continuumio/anaconda3:2020.02: Python 3.7.6 * continuumio/anaconda3:2019.10: Python 3.7.4 * [https://hub.docker.com/r/condaforge/miniforge3 condaforge/miniforge3:latest]: Python 3.9.10 {{access | date=2022-08-22}} * condaforge/miniforge3:4.9.2-5: Python 3.8.6 === docker image: python === [https://hub.docker.com/_/python/ library/python - Docker Hub] <pre> image: python:3.6 before_script: # show version of Linux, python and nginx - cat /etc/*-release - python -V - python3 -V - pip -V - pip3 -V - pwd - pip3 install -r requirements.txt </pre> === docker image: docker === * [https://hub.docker.com/_/docker/ library/docker - Docker Hub] python v. 2.7.15 {{access | date=2018-08-30}} === docker image: ubuntu === [https://hub.docker.com/_/ubuntu/ library/ubuntu - Docker Hub] <pre> # 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 </pre> == References == <references /> == Related articles == * [https://github.com/pypa/pipenv pypa/pipenv: Python Development Workflow for Humans.] * [https://hub.docker.com/_/python/ library/python - Docker Hub] * [https://www.anaconda.com/download/#macos Anaconda]: [https://docs.anaconda.com/anaconda/navigator/tutorials/use-multiple-python-versions/ Using multiple versions of Python with Navigator — Anaconda 2.0 documentation] [[Category: Python]] [[Category: Programming]] [[Category: Docker]] [[Category: Linux]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Access
(
view source
) (protected)
Template:Exclaim
(
edit
)
Template:Kbd
(
edit
)
Template:Linux
(
edit
)
Template:Mac
(
edit
)
Template:Tips
(
edit
)
Template:Win
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information