码农公寓

Python3和Python2共用

2024-03-18 14:31:10

参考:https://www.zhihu.com/question/21653286

1.使用启动器py.exe

py -2 使用python2,如: py -2 hello.py

py -3 使用python3,如: py -3 hello.py

 

2. 指定文件由python2解释运行,还是由python3解释运行

在文件头增加

#! python2

#! python3

 

3. 运行pip

py -2 -m pip install XXXX

py -3 -m pip install XXXX

-2 还是表示使用 Python2,-m pip 表示运行 pip 模块,也就是运行pip

 

4. #! python2 和 # coding: utf-8 哪个写在前面?

#! python2

# coding: utf-8

Python3和Python2共用