安装Python3.X

2017年1月6日14:14:54

中午阅读卡包,知晓任务之后便在想Python2.X与Python3.X两个版本共存的问题,思考得到的解决方案是:安装Python3.X到其他目录,并通过绝对路径来执行。

安装Python 3.6.0到D盘指定目录Python3,完毕之后切换目录到Python3目录下测试,结果OK。

(注:没有添加Python 3.6.0 到环境变量,所以必须到安装目录之下执行才能唤起 Python 3.6.0

lianbche@5CG44636XG D:\Program Files\Python3
> python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>

Python 2.7.2是之前安装好并且已经添加了Python安装目录到环境变量,因此可以在Windows系统下的其他目录执行python命令唤起的均将是 Python 2.7.2这个版本。

lianbche@5CG44636XG D:\Program Files
> python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

第一个任务的策略

2017年1月6日14:41:37

第一个任务为通过练习来找到Python2.X与Python3.X的区别,考虑到当前《LearnPythonTheHardWay》只学习了前18课,遂决定继续Exercise19 ~ Exercise23并分别使用Python2.X与Python3.X实现。

使用Python3.X时的问题

问题1:更好的测试

敲完exercise 19之后脑海里立即浮现一个问题:怎样尽可能便捷的使用Python2.X和Python3.X来测试ex19.py ?

背景:在《安装Python3.X》里面提到唤起Python3只能去它的安装目录,那么现在我需要用它来执行ex19.py,怎么办呢?

方法:

  • 将ex19.py拷贝一份,存放到Python3得目录。
  • 使用绝对路径来唤起Python3。

选择:拷贝的工作太懒了,排除。所以选择在之前目录使用绝对路径来唤起Python3。比如目录D:\Learning\git\run-into-python\LearnPythonTheHardWay是我clone的git仓库目录,在其中执行python指令默认唤起的时Python2.X的版本,而我在当前目录是希望使用Python3.X来执行ex19.py的,通过指定Python3.X的绝对路径来唤起它就好了,就像如下示例中的 "D:\Program Files\Python3\python" ex19.py

lianbche@5CG44636XG D:\Learning\git\run-into-python\LearnPythonTheHardWay
> python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit
Use exit() or Ctrl-Z plus Return to exit
>>> exit()

lianbche@5CG44636XG D:\Learning\git\run-into-python\LearnPythonTheHardWay
> "D:\Program Files\Python3\python" ex19.py
  File "ex19.py", line 2
    print "You have %d cheeses!" % cheese_count
                               ^
SyntaxError: Missing parentheses in call to 'print'

lianbche@5CG44636XG D:\Learning\git\run-into-python\LearnPythonTheHardWay
>

注:忽略其中的执行错误,因为这个错误是由两个版本之间的区别所导致。

问题2:Python3.X中的print

2017年1月6日15:15:56

很明显,根据如上的错误信息 SyntaxError: Missing parentheses in call to 'print' 可知print后面需要跟上括号,是这样吗?

查看Python的官方文档,找到3.X的Doc,并在右上角搜索框搜索“print”,阅读对应内容,确认再3.X中print是以函数形式调用,因此添加括号,问题解决。

问题3:使用Python2.X去执行Python3.X的代码

2017年1月6日16:20:38

Exercise 20 与 Exercise 19在Python版本差异上基本上还是停留在print上,问题2提到了使用Python3.X执行Python2.X的代码时会提示 SyntaxError: Missing parentheses in call to 'print',那么反过来,使用低版本的Python去执行高版本的代码文件会发生什么情况呢?

答案是没有问题。不过这只是个巧合:通常来说软件版本迭代过程中会保证的“前向兼容”,Python3.X无法执行低版本Python的代码,从这一点上说就有些奇怪了,并且弄巧成拙,反倒让Python2.X可以执行高版本Python的代码了。下面是输出比较:

1.唤起Python3.X执行Python3的代码文件 lianbche@5CG44636XG D:\Learning\git\run-into-python\LearnPythonTheHardWay

> "D:\Program Files\Python3\python" ex20_python3.py ex20_example_file.txt
First let's print the whole file:

This is the 1st line.
This is the 2nd line.
This is the 3rd line.

Aha, you see my bottle :)

Now let's print the rewind, kind of like a tape.
Let's print three lines:
1 This is the 1st line.

2 This is the 2nd line.

3 This is the 3rd line.

2.唤起Python2.X执行Python3的代码文件,注意最后三行的打印输出了括号。

lianbche@5CG44636XG D:\Learning\git\run-into-python\LearnPythonTheHardWay
> python ex20_python3.py ex20_example_file.txt
First let's print the whole file:

This is the 1st line.
This is the 2nd line.
This is the 3rd line.

Aha, you see my bottle :)

Now let's print the rewind, kind of like a tape.
Let's print three lines:
(1, 'This is the 1st line.\n')
(2, 'This is the 2nd line.\n')
(3, 'This is the 3rd line.\n')

results matching ""

    No results matching ""