Multiple python version installs: Difference between revisions
Jump to navigation
Jump to search
m (→Windows) |
|||
Line 317: | Line 317: | ||
* [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://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:Python]] [[Category:Programming]] [[Category:Docker]] |
Revision as of 22:01, 8 January 2019
同一電腦有兩種版本的 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 $ /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 版本
- 如果不小心打到小寫 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
安裝套件[5]
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
安裝套件
已知 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
comparison of docker images
Docker image:tab | Estimated time of python v. 3.x installation (minute) |
---|---|
python:3.6 | less than 1 |
centos:centos7 | 4 |
ubuntu:18.04 | 1 |
docker image: python
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
- library/docker - Docker Hub python v. 2.7.15 [Last visited: 2018-08-30]
docker image: ubuntu
# 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
docker image: centos
library/centos - Docker Hub [Last visited: 2018-09-06]
- default python version: python v. 2.7.5
# install python3 on centos:centos7 image image: centos:centos7 before_script: - yum install -y gcc zlib-devel wget - yum install -y openssl-devel - rpm -aq|grep openssl - yum groupinstall -y 'Development Tools' - python -V # https://www.python.org/downloads/ - wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz - tar xzf Python-3.6.6.tgz - cd Python-3.6.6 - ./configure - make - make install - python3 -V - cd ../ # https://github.com/pypa/pip/releases - curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" - python3 get-pip.py - pip3 -V - pwd - pip3 install -r requirements.txt
References
References
- ↑ 在Windows 10中打開命令提示字元的10種方式 - 笨笨包的天空
- ↑ 3. Using Python on Windows — Python 3.6.5 documentation
- ↑ What do the three arrow (">>>") signs mean in python? - Stack Overflow
- ↑ Install and upgrade pip on Mac OS X | - Softwaretester -
- ↑ python - Could not find a version that satisfies the requirement tensorflow - Stack Overflow
Related articles