Windows下Python2與Python3間的隨意切換

Windows中Python2與Python3并存洛心,隨意切換版本 :

首先需要知道蔬螟,python默認(rèn)執(zhí)行版本由系統(tǒng)環(huán)境變量中Python2和Python3的先后順序決定盛正;

這兒給大家展示在不同情況下利用不同的方法調(diào)用python;

#1:Python Launcher調(diào)用參數(shù)

> py --help

Python Launcher for Windows Version 3.7.150.1013

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

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
     The above all default to 64 bit if a matching 64 bit python is present.
-X.Y-32: Launch the specified 32bit Python version
-X-32  : Launch the latest 32bit Python X version
-X.Y-64: Launch the specified 64bit Python version
-X-64  : Launch the latest 64bit Python X version
-0  --list       : List the available pythons
-0p --list-paths : List with paths

py -2 #調(diào)用Python2
py -3 #調(diào)用Python3

img
程序運(yùn)行:
py -2 *.py #運(yùn)行python2程序

py -3 *.py#運(yùn)行python3程序
下面是一個python2程序的例子,當(dāng)使用py -3時(shí)路星,運(yùn)行失敳⒒取卧檐;

img

#2:修改執(zhí)行程序.exe名字

..\Python37\python.exe可以修改為python2.exe

..\Python27\python.exe可以修改為python3.exe

這樣直接調(diào)用python2與python3就不會混亂了克懊;

#3:程序中申明

在python程序首行添加#! python3指定由python3解釋運(yùn)行;

在python程序首行添加#! python2指定由python2解釋運(yùn)行七蜘;

#3:pip運(yùn)行

py -2 -m pip install numpy  #pip2 install numpy
py -3 -m pip install numpy #pip3 install numpy

#4:Python Launcher for Windows

python launcher是python3.3新增的一個功能谭溉,它對所有有的python版本都是兼容的;Python launcher for Windows可以幫助程序或命令選擇最合適的python版本作為解釋器橡卤,而不是最近安裝的python版本扮念;安裝3.3及更新版本的python時(shí),python launcher默認(rèn)加入到path中碧库。

詳細(xì)信息見Python Launcher for Windows

py是python launcher下的一個命令柜与;

##打開最近安裝的python版本

> py 
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

##查看py詳細(xì)參數(shù)

>py -h

Python Launcher for Windows Version 3.7.150.1013

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

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
     The above all default to 64 bit if a matching 64 bit python is present.
-X.Y-32: Launch the specified 32bit Python version
-X-32  : Launch the latest 32bit Python X version
-X.Y-64: Launch the specified 64bit Python version
-X-64  : Launch the latest 64bit Python X version
-0  --list       : List the available pythons
-0p --list-paths : List with paths

The following help text is from Python:

usage: C:\Program Files\Python37\python.exe [option] ... [-c cmd | -m mod | file | -][arg] ...
Options and arguments (and corresponding environment variables):
-b     : issue warnings about str(bytes_instance), str(bytearray_instance)
         and comparing bytes/bytearray with str. (-bb: issue errors)
-B     : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
-c cmd : program passed in as string (terminates option list)
-d     : debug output from parser; also PYTHONDEBUG=x
-E     : ignore PYTHON* environment variables (such as PYTHONPATH)
-h     : print this help message and exit (also --help)
-i     : inspect interactively after running script; forces a prompt even
         if stdin does not appear to be a terminal; also PYTHONINSPECT=x
-I     : isolate Python from the user's environment (implies -E and -s)
-m mod : run library module as a script (terminates option list)
-O     : remove assert and __debug__-dependent statements; add .opt-1 before
         .pyc extension; also PYTHONOPTIMIZE=x
-OO    : do -O changes and also discard docstrings; add .opt-2 before
         .pyc extension
-q     : don't print version and copyright messages on interactive startup
-s     : don't add user site directory to sys.path; also PYTHONNOUSERSITE
-S     : don't imply 'import site' on initialization
-u     : force the stdout and stderr streams to be unbuffered;
         this option has no effect on stdin; also PYTHONUNBUFFERED=x
-v     : verbose (trace import statements); also PYTHONVERBOSE=x
         can be supplied multiple times to increase verbosity
-V     : print the Python version number and exit (also --version)
         when given twice, print more information about the build
-W arg : warning control; arg is action:message:category:module:lineno
         also PYTHONWARNINGS=arg
-x     : skip first line of source, allowing use of non-Unix forms of #!cmd
-X opt : set implementation-specific option
--check-hash-based-pycs always|default|never:
    control how Python invalidates hash-based .pyc files
file   : program read from script file

         : program read from stdin (default; interactive mode if a tty)
arg ...: arguments passed to program in sys.argv[1:]


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市嵌灰,隨后出現(xiàn)的幾起案子弄匕,更是在濱河造成了極大的恐慌,老刑警劉巖沽瞭,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件迁匠,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)城丧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門延曙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人亡哄,你說我怎么就攤上這事枝缔。” “怎么了蚊惯?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵愿卸,是天一觀的道長。 經(jīng)常有香客問我拣挪,道長擦酌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任菠劝,我火速辦了婚禮赊舶,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘赶诊。我一直安慰自己笼平,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布舔痪。 她就那樣靜靜地躺著寓调,像睡著了一般。 火紅的嫁衣襯著肌膚如雪锄码。 梳的紋絲不亂的頭發(fā)上夺英,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天,我揣著相機(jī)與錄音滋捶,去河邊找鬼痛悯。 笑死,一個胖子當(dāng)著我的面吹牛重窟,可吹牛的內(nèi)容都是我干的载萌。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼巡扇,長吁一口氣:“原來是場噩夢啊……” “哼扭仁!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起厅翔,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤乖坠,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后知给,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體瓤帚,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡描姚,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了戈次。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片轩勘。...
    茶點(diǎn)故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖怯邪,靈堂內(nèi)的尸體忽然破棺而出绊寻,到底是詐尸還是另有隱情,我是刑警寧澤悬秉,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布澄步,位于F島的核電站,受9級特大地震影響和泌,放射性物質(zhì)發(fā)生泄漏村缸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一武氓、第九天 我趴在偏房一處隱蔽的房頂上張望梯皿。 院中可真熱鬧,春花似錦县恕、人聲如沸东羹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽属提。三九已至,卻和暖如春美尸,著一層夾襖步出監(jiān)牢的瞬間冤议,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工师坎, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留求类,地道東北人。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓屹耐,卻偏偏與公主長得像,于是被迫代替她去往敵國和親椿猎。 傳聞我的和親對象是個殘疾皇子惶岭,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容