程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Teach you how to switch between python2 and python3 draft boxes

編輯:Python

When installed at the same time Python2 and Python3 after , How to compatible and switch using details ( such as pip Use )

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved