Python - Python2与Python3合理共存Windows平台

Install Python2 and Python3

Python 2.7.13 - Windows x86-64 MSI installer
Python 3.6.0 - Windows x86-64 executable installer
Note: For Python 3.3 and later, select the option "Install launcher for all users(recommended)".

Set Environment Variables
Sample:
C:\Python27;C:\Python27\Scripts;C:\Python27\Lib\site-packages;
C:\Python36;C:\Python36\Scripts;C:\Python36\Lib\site-packages;

Python Launcher for Windows Version

Python Launcher for Windows Version

>py -2
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

>py -3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Run an sample

Method 1 : python or py

>python --version
Python 2.7.13

>py --version
Python 3.6.0

Method 2 : py -2 or py -3

>type HelloWorld-py2.py
print 'Hello World! Hello Python!'
>
>py -2 HelloWorld-py2.py
Hello World! Hello Python!


>type HelloWorld-py3.py
print ('Hello World! Hello Python!')
>
>py -3 HelloWorld-py3.py
Hello World! Hello Python!
>

Note:py -2.7 HelloWorld-py2.py and py -3.6 HelloWorld-py3.py are recommended.

Method 3 : Specify the Python version in a script

>type HelloWorld-py2.py
#! python2.7
print 'Hello World! Hello Python!'
>
>py HelloWorld-py2.py
Hello World! Hello Python!


>type HelloWorld-py3.py
#! python3.6
print ('Hello World! Hello Python!')
>
>py HelloWorld-py3.py
Hello World! Hello Python!
>

pip2 and pip3

Install Python2 Package: pip2 install <Package_name> or py -2 -m pip install <Package_name>
Install Python3 Package: pip3 install <Package_name> or py -3 -m pip install <Package_name>

upgrade pip2:py -2 -m pip install --upgrade pip
upgrade pip3:py -3 -m pip install --upgrade pip

>pip2 --version
pip 9.0.1 from c:\python27\lib\site-packages (python 2.7)

>pip3 --version
pip 9.0.1 from c:\python36\lib\site-packages (python 3.6)

Run an sample

>pip3 list --format=legacy
pip (9.0.1)
setuptools (28.8.0)

>pip3 install requests --proxy=10.144.1.10:8080
Collecting requests
  Downloading requests-2.13.0-py2.py3-none-any.whl (584kB)
    100% |████████████████████████████████| 593kB 137kB/s
Installing collected packages: requests
Successfully installed requests-2.13.0

>pip3 list --format=legacy
pip (9.0.1)
requests (2.13.0)
setuptools (28.8.0)

py help

$ py -h
Python Launcher for Windows Version 3.6.150.1013

usage: py [ launcher-arguments ] [ python-arguments ] script [ script-arguments ]

Launcher arguments:

-2     : Launch the latest Python 2.x version
-3     : Launch the latest Python 3.x version
-X.Y   : Launch the specified Python version
-X.Y-32: Launch the specified 32bit Python version

......
posted @ 2017-02-13 16:52  Anliven  阅读(535)  评论(0编辑  收藏  举报