您的位置:首页 > 编程语言 > Python开发

边学边译Dive into python,中文版Dive into python,深入Python(一)

2005-09-25 18:03 751 查看
最近弄来了BT的源代码,想研究一下,结果发现是Python写的,不太懂。就从程序员合订本的光盘里找了一些资料,有一本Dive into python,发现很不错,干脆就一边翻译,一边学了,发上来翻译好的文档,大家一同分享吧!翻译不好的地方还请多多见谅:)第一章. [/b]安装Python
[/b]欢迎来到Python的世界。让我们开始深入了解它。这一章将引导你安装合适版本的Python。
1.1[/b]何种Python适合你?
[/b] 第一件事,你需要安装Python,你认为呢?
如果你使用的是联机终端,那么为你提供服务的ISP服务商没准已经装好了Python。大多数流行的Linux系统都将Python作为默认得部署部分。虽然你可能更喜欢Mac下的图形方式的Python,但是Mac OS X 10.2及其后续版本已经默认安装了命令控制台方式的Python。
Windows(MS)还未曾默认提供过任何版本的Python,不过不用失望。这里有几种方法,只要你轻点几下鼠标,敲几行命令,就可以将Python安装在你的Windows上。
如你所见,Python成功运行于这些成功的操作系统上。它们包括:Windows, Mac OS,
Mac OS X,以及全兼容于免费的Unix的演化系统,比如Linux。他也能被解释运行于Sun Solaris, AS/400, Amiga, OS/2, BeOS,和其它一些你从未听说过的平台。
最为大的是,Python的程序在一个平台上编写,只需稍许的注意,就可以运行于任何支持它的平台。例如:我在Windows环境下编写程序,却可以在Linux下部署并发布。
现在回到我们原先的问题:“何种Python适合你?”。答案是任何一个可以运行在你的计算机上的都可以。
1.2Windows[/b]下的Python
[/b] Windows下有两种选择来安装Python。
ActiveState开发了Windows下的Python,我们称它为ActivePython,它包含了全部版本的Python,以及针对Windows扩充了服务插件、API接口、注册信息等的集成IDE开发环境。
ActinvePython虽然不是开源软件,但是它是免费下载的。我用它的IDE环境学习了Python,我也推荐你去试试,除非你有特别的理由可以拒绝它。唯一可能的理由是:ActivePython由于ActiveState的种种原因,总是在最新版本的Python发布几个月之后才进行最新的更新,也就是ActivePython的Python版本会比最新的Python可能低。如果你由于种种原因坚持要使用最新版本的Python,我们有第二个方法在Windows上安装Python。
第二种方法我们成为“官方”Python安装。Python本身就是可以在Windows下运行的。可以从开源中心(open source)免费下载,而且是最新的版本。

安装方式一:安装ActivePython
[/b]1、 从http://www.activestate.com/Products/ActivePython/下载ActivePython;
2、 如果你使用的是Windows 95, Windows 98或 Windows ME,安装ActivePython之前你还必须下载Windows Installer 2.0(http://download.microsoft.com/download/WindowsInstaller/Install/2.0/W9XMe/EN−US/InstMsiA.exe)(注:这是英文版的,中文用户请下中文版);
3、 双击安装文件:ActivePython−2.2.2−224−win32−ix86.msi;
4、 一步一步根据提示安装程序;
5、 如果空间紧张,可以选择不安装文档,但是我不推荐您这么做,除非你完全无法腾出14M的空间;
6、 安装完毕,关闭向导。到开始->程序-> ActiveState ActivePython2.2−>PythonWin IDE你将看见类似的提示:
PythonWin 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994−2001 Mark Hammond (mhammond@skippinet.com.au) −
see 'Help/About PythonWin' for further copyright information.
>>>

安装方式二:从[/b]Python.org (http://www.python.org/)安装Python;
[/b]1、 去http://www.python.org/ftp/python/选择版本号最高的最新版的.exe安装文件,也就是下载最新的Python在Windows下的安装文件;
2、 双击Python−2.xxx.yyy.exe(从安装文件的名称中你就可以看出版本号);
3、 一步一步根据提示安装程序;
4、 如果磁盘空间紧张,你可以删除HTMLHelp、工具脚本(Tools/)和测试库(Lib/test/);
5、 如果你没有你的机器的管理员的权限,你可以选择高级选项(Advanced Options)选择无管理员安装(Non−Admin Install)这将会影响到系统中注册信息的写入和开始菜单中程序组的创建;
6、 最后安装完成,关闭向导,到开始->程序->Python 2.3−>IDLE (Python GUI)你将看到如下一些提示信息:
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************
IDLE 1.0
>>>

1.3[/b] Mac OS X下的Python
在Mac OS X下你有两个选择来安装Python:安装它,或者不安装它。也许你会选择安装它。
Mac OS 1.2及其后续的版本都预先安装了命令控制台方式的Python。如果你觉得这种方式很适合你,那么我可以说,你只能使用本书的1/3的内容了。总之,预安装的Python是不支持XML解析的,当你阅读到XML相关章节,你将不得不安装完全版的Python。
即使你喜欢使用预装的Python,你也必须安装最新的完全版的Python。也同样需要图形交互界面。
安装方式一:预安装的Python
[/b] 要使用预安装的Python,请按如下步骤进行操作:
1、打开/Applications文件夹;
2、打开Utilities文件夹;
3、双击终端(Terminal),打开终端窗口,键入命令行;
4、在命令提示符下键入Python。
尝试一下下面的做法:
Welcome to Darwin!
[localhost:~] you% python
Python 2.2 (#1, 07/14/02, 23:25:09)
[GCC Apple cpp−precomp 6.14] on darwin
Type "help", "copyright", "credits", or "license" for more information.
>>> [press Ctrl+D to get back to the command prompt]
[localhost:~] you%

安装方式二:安装最新的Python在Mac OS X上
[/b]根据一下步骤安装Python:
1、从http://homepages.cwi.nl/~jack/macpython/download.html下载MacPython−OSX的硬盘镜像文件;
2、如果你下载好了,在桌面上双击MacPython−OSX−2.3−1.dmg文件,挂载硬盘镜像;
3、双击MacPython−OSX.pkg安装文件,开始安装;
4、安装程序将提示你输入管理员用户名和密码;
5、根据向导指示一步步安装;
6、安装完毕,打开/Applications文件夹;
7、打开MacPython−2.3文件夹;
8、双击PythonIDE,开始运行Python。
当你运行MacPython IDE时,将会弹出一个窗口。如果没有弹出,请选择:Window−>Python Interactive (Cmd−0)。将会出现如下提示的屏幕信息:
Python 2.3 (#2, Jul 30 2003, 11:45:28)
[GCC 3.1 20020420 (prerelease)]
Type "copyright", "credits" or "license" for more information.
MacPython IDE 1.0.1
>>>

虽然安装了最新的Python,但是原先的预装的Python仍然是完好存在的。当你在控制台方式下运行脚本命令,你将会更清楚的知道你当前使用的版本是否是预装的。
例如1.1 两个版本的Python
[/b]
[localhost:~] you% python
Python 2.2 (#1, 07/14/02, 23:25:09)
[GCC Apple cpp−precomp 6.14] on darwin
Type "help", "copyright", "credits", or "license" for more information.
>>> [press Ctrl+D to get back to the command prompt]
[localhost:~] you% /usr/local/bin/python
Python 2.3 (#2, Jul 30 2003, 11:45:28)
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits", or "license" for more information.
>>> [press Ctrl+D to get back to the command prompt]
[localhost:~] you%

1.4 Mac OS 9上的Python
Mac OS 9没有安装任何版本的Python,但是安装也是异常的简单,而且只有一种安装方式可以选择。
按照下面的步骤在Mac OS 9上安装Python:
1、从http://homepages.cwi.nl/~jack/macpython/download.html.下载MacPython23full.bin程序文件;
2、如果你的计算机没有自动的解压缩文件,双击MacPython23full.bin文件,用
Stuffit Expander解压缩它;
3、双击安装文件,安装MacPython23full;
4、根据向导指示一步步安装;
5、安装完毕,关闭向导,打开/Applications文件夹;
6、打开MacPython−OS9 2.3文件夹;
7、双击Python IDE,运行Python。
当你运行MacPython IDE时,将会弹出一个窗口。如果没有弹出,请选择:Window−>Python Interactive (Cmd−0)。将会出现如下提示的屏幕信息:
Python 2.3 (#2, Jul 30 2003, 11:45:28)
[GCC 3.1 20020420 (prerelease)]
Type "copyright", "credits" or "license" for more information.
MacPython IDE 1.0.1
>>>

1.5[/b]在RedHat Linux上安装Python
[/b]如果是要在如Linux这样的UNIX兼容操作系统上安装二进制可运行文件包的Python,那么它会和在Linux下一样简单。预装版的二进制可运行文件包,已经被大多数流行的Linux系统所部署。除非你希望通过编译源文件来得到它。
http://www.python.org/ftp/python/下载最新版本的Python RPM安装包(就是rpms/目录下的,版本数字最大的文件)。下载完毕就可以安装RPM安装包了,例如:1.2在RedHat Linux 9上安装
[/b]
localhost:~$ su −
Password: [enter your root password]
[root@localhost root]# wget http://python.org/ftp/python/2.3/rpms/redhat−9/python2.3−2.3−5pydotorg.i386.Resolving python.org... done.
Connecting to python.org[194.109.137.226]:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7,495,111 [application/octet−stream]
...
[root@localhost root]# rpm −Uvh python2.3−2.3−5pydotorg.i386.rpm
Preparing... ########################################### [100%]
1:python2.3 ########################################### [100%]
[root@localhost root]# python 1
Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2−4)] on linux2
Type "help", "copyright", "credits", or "license" for more information.
>>> [press Ctrl+D to exit]
[root@localhost root]# python2.3 2
Python 2.3 (#1, Sep 12 2003, 10:53:56)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2−5)] on linux2
Type "help", "copyright", "credits", or "license" for more information.
>>> [press Ctrl+D to exit]
[root@localhost root]# which python2.3
/usr/bin/python2.3 3
1、哦!键入“Python”,是老版本的预装的,并不是你想要的;
2、这次我们键入“Python 2.3”。你也许需要先使用脚本命令使当前目录指向最新的Python;
3、这样可以得到你安装的最新版本的Python的路径。用它在每一个#!行(每个脚本的第一行)使当前目录指向最新版的Python的目录,并且确保你键入的是“Python 2.3”来进入Python的界面。
1.6 [/b]在[/b]Debian GNU/Linux上安装Python
[/b]如果你的运气十分的好在使用Debian GNU/Linux的话,就可以通过apt命令来安装它。
例如1.3:在[/b]Debian GNU/Linux安装
localhost:~$ su −
Password: [enter your root password]
localhost:~# apt−get install python
Reading Package Lists... Done
Building Dependency Tree... Done
The following extra packages will be installed:
python2.3
Suggested packages:
python−tk python2.3−doc
The following NEW packages will be installed:
python python2.3
0 upgraded, 2 newly installed, 0 to remove and 3 not upgraded.
Need to get 0B/2880kB of archives.
After unpacking 9351kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Selecting previously deselected package python2.3.
(Reading database ... 22848 files and directories currently installed.)
Unpacking python2.3 (from .../python2.3_2.3.1−1_i386.deb) ...
Selecting previously deselected package python.
Unpacking python (from .../python_2.3.1−1_all.deb) ...
Setting up python (2.3.1−1) ...
Setting up python2.3 (2.3.1−1) ...
Compiling python modules in /usr/lib/python2.3 ...
Compiling optimized python modules in /usr/lib/python2.3 ...
localhost:~# exit
logout
localhost:~$ python
Python 2.3.1 (#2, Sep 24 2003, 11:39:14)
[GCC 3.3.2 20030908 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> [press Ctrl+D to exit]

1.7[/b]从源文件编译来安装得到Python
[/b]如果你更喜欢用编译方式来在自己的机器上编译得到Python,那么你可以到http://www.python.org/ftp/python/去下载最新版本的.tgz文件,并且像平常一样:设定(configure),批编译( make),编译安装( make install)。
例如1.4:从源文件编译安装:
localhost:~$ su −
Password: [enter your root password]
localhost:~# wget http://www.python.org/ftp/python/2.3/Python−2.3.tgz Resolving www.python.org... done.
Connecting to www.python.org[194.109.137.226]:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8,436,880 [application/x−tar]
...
localhost:~# tar xfz Python−2.3.tgz
localhost:~# cd Python−2.3
localhost:~/Python−2.3# ./configure
checking MACHDEP... linux2
checking EXTRAPLATDIR...
checking for −−without−gcc... no
...
localhost:~/Python−2.3# make
gcc −pthread −c −fno−strict−aliasing −DNDEBUG −g −O3 −Wall −Wstrict−prototypes
−I. −I./Include −DPy_BUILD_CORE −o Modules/python.o Modules/python.c
gcc −pthread −c −fno−strict−aliasing −DNDEBUG −g −O3 −Wall −Wstrict−prototypes
−I. −I./Include −DPy_BUILD_CORE −o Parser/acceler.o Parser/acceler.c
gcc −pthread −c −fno−strict−aliasing −DNDEBUG −g −O3 −Wall −Wstrict−prototypes
−I. −I./Include −DPy_BUILD_CORE −o Parser/grammar1.o Parser/grammar1.c
...
localhost:~/Python−2.3# make install
/usr/bin/install −c python /usr/local/bin/python2.3
...
localhost:~/Python−2.3# exit
logout
localhost:~$ which python
/usr/local/bin/python
localhost:~$ python
Python 2.3.1 (#2, Sep 24 2003, 11:39:14)
[GCC 3.3.2 20030908 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> [press Ctrl+D to get back to the command prompt]
localhost:~$

1.8[/b]用户界面
[/b] 好了,现在你已经安装了Python,现在让我们看看如何在用户界面下使用它。
Python有两种使用方法。一种是脚本解释型的,在命令行方式下,像命令一样使用它;另一种是像应用程序一样去使用它,在应用程序中双击脚本来运行。而且交互式用户界面提供了表达式运算、语句运行和函式运算功能。这一点非常有用,尤其是在调试、快速编辑、和测试时。我见到过一些程序员,他们使用Python的用户交互界面来当计算器用。
在你的平台上不论以什么样的方式启动Python的用户交互界面,并且和我一起进入它的世界:
例如1.5 用户交互界面的第一个简单应用
[/b]
>>> 1 + 1 1
2
>>> print 'hello world' 2
hello world
>>> x = 1 3
>>> y = 2
>>> x + y
3
1、Python可以进行任何的Python表达式的计算,包括所有的基本算术运算符组成的表达式;
2、用户交互界面可以执行任意的Python语句,包括打印(print)语句;
3、你可以总是将值付值给变量,它将被用户界面永久的记住(除了很长时间不用的情况)。
1.9[/b]摘要
[/b] 现在选择一个你想用的Python版本安装它。
基于你自己的平台,你可能会安装不知一个版本的Python。如果是那样,你需要知道它们的路径。你不可能靠简单的键入Python来运行你想要运行的版本。你没准需要输入一个全路径方式的命令来运行你所想要的版本的Python。
祝贺你,欢迎你来到Python的世界。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: