For historical reasons ,[Python](https://so.csdn.net/so/search?from=pc_blog_highlight&q=Python) There are two big branch versions ,Python2 and Python3, Because some libraries only support one version Branch , So you need to install it on your computer at the same time Python2 and Python3, So how to make two versions of Python compatible , How to make the script in the corresponding Python Run... On version , This is worth summarizing .
about Ubuntu 16.04 LTS In terms of version ,Python2(2.7.12) and Python3(3.5.2) By default, install at the same time , default python The version is 2.7.12.
Of course you can use it python2 To call .
If you want to call python3, Just use python3.
about Windows, It's a little complicated . Because no matter python2 still python3,python Executable files are called python.exe, stay cmd The input python The version number you get depends on which version of the environment variable is python The path is more forward , After all windows It's in order . For example, the sequence of environment variables is like this :
that cmd Under the python The version is 2.7.12.
conversely , It is python3 Version number of .
This brings a problem , If you want to use python2 Run a script , Later you want to use python3 Run another script , How do you do ? Changing environment variables back and forth is obviously troublesome .
Many online methods are simple and crude , Take two. python.exe Change your name , Change one to python2.exe, Change one to python3.exe. This is certainly possible , But how to modify the executable , After all, it's not a very good way .
I looked up some carefully python Technical documentation , I found another good solution .
To borrow py To call different versions of Python.py -2 call python2,py -3 It's called python3.
When python The script needs python2 Runtime , Just add... Before the script , And then run py xxx.py that will do .
#! python2
When python The script needs python3 Runtime , Just add... Before the script ,, And then run py xxx.py that will do .
#! python3
It's that simple .
meanwhile , This also perfectly solves the problem of pip stay python2 and python3 An error is reported in a coexisting environment , Tips Fatal error in launcher: Unable to create process using ‘”’ The problem of .
When you need to python2 Of pip when , just
py -2 -m pip install xxx
When you need to python3 Of pip when , just
py -3 -m pip install xxx
python2 and python3 Of pip package It's a perfect separation .