Multiple python version installs: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
 
(48 intermediate revisions by the same user not shown)
Line 1: Line 1:
同一電腦有兩種版本的 python
如何在同一電腦,安裝兩種版本的 python


== 安裝兩種版本的 Python ==
== 安裝兩種版本的 Python ==
=== Windows ===
=== 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
Windows 電腦已經安裝 Python2, 想要安裝 Python3
* 直接從 [https://www.python.org/downloads/ Python 網站] 下載安裝 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
* 安裝完 Windows 版本 Python3,已經內建 pip,無需額外安裝 pip


=== Mac ===
{{Tips}} Python2 或 Python3 的安裝前後順序會造成的影響是 {{kbd | key=<nowiki>python -V </nowiki>}} 顯示的是先安裝的版本,可依照下文說明指定要執行的版本。
 
=== Mac 安裝兩種版本的 Python 法2 ===
因為 Mac 內建 Python2, 所以需要額外安裝 Python3
因為 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 – 字串小豬]
* 請參考 [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
* 安裝完 Mac 版本 Python3,已經內建 pip,無需額外安裝 pip


=== Linux ===
=== Linux 安裝兩種版本的 Python 法2 ===
已經安裝 Python2, 需要額外安裝 Python3
通常 Linux 已經內建安裝 Python2, 需要額外安裝 Python3
* [https://oranwind.org/python-python-an-zhuang-yu-ji-chu-jiao-xue/ CentOS 7 安裝 Python 3.6]
* [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运维日志]
* [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。


: [[Image:Owl icon.jpg]] 文章中行首的 $ 符號代表 Windows 作業系統輸入指令時,要先開啟「命令提示字元」、Mac 或 Linux 輸入指令時,要先開啟「終端機」、terminal 或 console。


== 確認安裝的 python 版本 ==
=== Windows ===
=== Windows ===
確認安裝版本<ref>[https://docs.python.org/3/using/windows.html#launcher 3. Using Python on Windows — Python 3.6.5 documentation]</ref>
確認安裝版本<ref>[https://docs.python.org/3/using/windows.html#launcher 3. Using Python on Windows — Python 3.6.5 documentation]</ref>
<pre>
<pre>
$ python -V  
$ python -V  
$ python -version
$ py -2 -V
$ py -2 -V
預期看到 Python 2.7.8 或其他 2.x 版本
預期看到 Python 2.7.8 或其他 2.x 版本
Line 35: Line 52:
=== Mac & Linux ===
=== Mac & Linux ===
<pre>
<pre>
$ python -V  
$ python -V
$ python -version
$ /usr/bin/env python -V
預期看到 Python 2.7.10 或其他 2.x 版本
預期看到 Python 2.7.10 或其他 2.x 版本


$ python3 -V
$ python3 -V
$ python3 -version
$ /usr/bin/env python3 -V
預期看到 Python 3.6.4 或其他 3.x 版本
預期看到 Python 3.6.4 或其他 3.x 版本
</pre>
</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) 版本 ==
== 確認 pip (package_manager) 版本 ==
Line 63: Line 86:
$ pip3 -V
$ pip3 -V
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6) 代表是可以用 pip3 安裝 python3 的套件
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>
</pre>


== 安裝或移除 python 套件 ==
== 安裝或移除 python 套件 ==
=== Windows ===
=== 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>
<pre>
手動切換到 python 2.x 版本的目錄
Python2: 以下以 Python 2.7 版本,安裝目錄在 c:\Python27\ 為例
$ c:\Python27\Scripts\pip.exe install <package>
$ c:\Python27\Scripts\pip.exe install <package>
$ c:\Python27\Scripts\pip.exe install -r requirements.txt
$ c:\Python27\Scripts\pip.exe install -r requirements.txt
$ py -2 -m pip install <package>


已知 pip、pip3 支援 python3 的套件
Python3: 已知 pip、pip3 支援 python3 的套件
$ pip3 install <package>
$ pip3 install <package>
$ py -m pip install <package>
</pre>
</pre>


Line 80: Line 132:
<pre>
<pre>
已知 pip3 支援 Python3 的套件
已知 pip3 支援 Python3 的套件
$ pip remove <package>
$ pip3 remove <package>
</pre>
</pre>


Line 150: Line 202:
</pre>
</pre>


如果遇到「'xxx' 不是內部或外部命令、可執行的程式或批次檔。」 代表安裝時沒有設定 PATH 變數,詳 [[How to setup my system path]]
{{Tips}} 如果遇到「'xxx' 不是內部或外部命令、可執行的程式或批次檔。」 代表安裝時沒有設定 PATH 變數,詳 [[How to setup my system path]]


=== Mac & Linux ===
=== Mac & Linux ===
Line 171: Line 223:
$ which pip3
$ which pip3
/usr/local/bin/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>
</pre>


== References ==
== References ==
References


<references />
<references />


Related articles
== Related articles ==
* [https://github.com/pypa/pipenv pypa/pipenv: Python Development Workflow for Humans.]
* [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: Python]]  
[[Category: Programming]]
[[Category: Docker]]
[[Category: Linux]]

Latest revision as of 12:18, 22 January 2025

如何在同一電腦,安裝兩種版本的 python

安裝兩種版本的 Python[edit]

Windows, Mac, Linux 安裝多種版本的 Python 法1[edit]

Windows 安裝兩種版本的 Python 法2[edit]

Windows 電腦已經安裝 Python2, 想要安裝 Python3

Owl icon.jpg Python2 或 Python3 的安裝前後順序會造成的影響是 python -V 顯示的是先安裝的版本,可依照下文說明指定要執行的版本。

Mac 安裝兩種版本的 Python 法2[edit]

因為 Mac 內建 Python2, 所以需要額外安裝 Python3

Linux 安裝兩種版本的 Python 法2[edit]

通常 Linux 已經內建安裝 Python2, 需要額外安裝 Python3

確認安裝的 python 版本[edit]

Owl icon.jpg 文章中行首的 $ 符號代表 Windows 作業系統輸入指令時,要先開啟「命令提示字元」輸入 cmd [2]、Mac 或 Linux 輸入指令時,要先開啟「終端機」、terminal 或 console。


Windows[edit]

確認安裝版本[3]

$ 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[edit]

$ 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 版本
Owl icon.jpg 如果不小心打到小寫 v 也就是 python -v,會看到一長串指令,最後游標停在 >>> 符號前面。有兩種方式就可以離開 >>> 世界 (python 互動模式[4]): (1) 輸入 exit() 或按組合鍵 ctrl+d (2) 再按 Enterreturn 按鍵

確認 pip (package_manager) 版本[edit]

Windows[edit]

確認 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[edit]

確認 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) 版本[5][edit]

使用 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[edit]

已知 py -2 -V 版本是 python2
$ py -2 -m pip install --upgrade pip

已知 py -3 -V 版本是 python2
$ py -3 -m pip install --upgrade pip

Mac & Linux[edit]

確認 pip 版本

已知 pip -V 版本是 python2
$ pip install --upgrade pip

已知 pip3 -V 版本是 python3
$ pip3 install --upgrade pip setuptools wheel

安裝或移除 python 套件[edit]

Windows[edit]

安裝套件[6]

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>

移除套件

已知 pip3 支援 Python3 的套件
$ pip3 remove <package>

Mac & Linux[edit]

安裝套件

已知 pip 或 pip2 支援安裝 python2 的套件
$ pip install <package>
$ pip2 install <package>

已知 pip3 支援安裝 python3 的套件
$ pip3 install <package>

移除套件

已知 pip3 支援 Python3 的套件
$ pip3 remove <package>

執行 python 程式[edit]

python 程式碼需要指定要執行的版本

Windows[edit]

已經確認 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[edit]

已經確認 python -V 是 2.x 版本

python script_for_version_2.py


已經確認 python3 -V 是 3.x 版本

py script_for_version_3.py

尋找命令對應的完整路徑[edit]

Windows[edit]

$ 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 的套件
Owl icon.jpg 如果遇到「'xxx' 不是內部或外部命令、可執行的程式或批次檔。」 代表安裝時沒有設定 PATH 變數,詳 How to setup my system path

Mac & Linux[edit]

$ 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[edit]

comparison of docker images[edit]

  • continuumio/anaconda3: Python 3.12.7 [Last visited: 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
  • condaforge/miniforge3:latest: Python 3.9.10 [Last visited: 2022-08-22]
  • condaforge/miniforge3:4.9.2-5: Python 3.8.6

docker image: python[edit]

library/python - Docker Hub

  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

docker image: docker[edit]

docker image: ubuntu[edit]

library/ubuntu - Docker Hub

# 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[edit]

Related articles[edit]