人生苦短,我用Python01:Hello Python!
目录:
Python
python和C的区别
对于C语言,代码编译后为机器码,机器码就可以直接在处理器上进行执行,每一条的指令都是在操作CPU工作。
而python代码的实现方式是由python代码编译为字节码,虚拟机将字节码再转换C语言可以识别的代码,最后由C编译为机器码。
而与C语言编写程序不同的是,Python等编程语言,不需要去管理内存,直接运行在虚拟机上或者说解释器上。
多种python
Python有很多系列,例如CPython,Jython,IronPython和RubyPython等等。跟前缀相关,CPython是转换成C可以识别的代码,而Jython则是转换成Java虚拟机可以识别的代码。
举例Jython,是把Jython的代码编译为Java虚拟机可识别的中间结果,由Java虚拟机进行余下的处理,而Java代码编译完也是Java虚拟机可识别的中间结果,这样Java和Jython的交互非常的快。
这些种python的代码规则是一样的,只不过Cpython是直接转换为C可识别代码,效率会更高一些,所以我们使用的Python也都是Cpython。
当然还有一种pypy,可以直接编译为字节码,只是在编译时间上使用较多,但是总体效率比CPython还要高。
Python的缺点
Python也有些不足,例如在线程方面,因为博主没有对Python使用到一定程度,也没有在其他语言上使用到一定程度,就不做过多评价。
Python的安装
在windows上安装
https://www.python.org/
选择download windows即可
https://www.python.org/ftp/python/2.7.13/python-2.7.13.amd64.msi
下载后直接进行安装即可。环境变量的配置,可以选择我的电脑右击,选择属性,高级系统设置,高级,环境变量,在系统变量的Path中添加安装python的路径
在Linux上安装
Python默认的话在Linux上已经安装好了Python的2.6.6版本。但是因为2.6和2.7的差距还有很大的,一般更新到2.7最好,如果要使用新的版本作为系统默认的python解释器,就需要把yum的解析依然指向Python2.6.6版本等操作,所以需要做如下的操作。
顺便说一下,python2.7就是为了过度到python3而准备的,在python2.7上可以适用大多数的python2.6的代码规则和python3的代码规则。
安装Python2.7
[root@why ~]# cd /opt/
[root@why opt]# wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz
--2017-06-04 01:56:11-- https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz
Resolving www.python.org... 151.101.72.223, 2a04:4e42:11::223
Connecting to www.python.org|151.101.72.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12495628 (12M) [application/octet-stream]
Saving to: “Python-2.7.13.tar.xz”
100%[=====================================================================================================================================================>] 12,495,628 14.9M/s in 0.8s
2017-06-04 01:56:13 (14.9 MB/s) - “Python-2.7.13.tar.xz” saved [12495628/12495628]
[root@why opt]# tar xf Python-2.7.13.tar.xz
[root@why opt]# cd Python-2.7.13/
[root@why Python-2.7.13]# ./configure --prefix=/opt/python-2.7.13
省略编译输出
[root@why Python-2.7.13]# echo $?
0
[root@why Python-2.7.13]# make && make install
升级默认python
[root@why Python-2.7.13]# mv /usr/bin/python /usr/bin/python.old
[root@why Python-2.7.13]# ln -s /opt/python-2.7.13/bin/python /usr/bin/python
[root@why Python-2.7.13]# sed -i 's?#!/usr/bin/python?#!/usr/bin/python2.6.6?g' /usr/bin/yum
[root@why Python-2.7.13]# head -1 /usr/bin/yum
#!/usr/bin/python2.6.6
如果有createrepo的话,还需要替换/usr/share/createrepo/genpkgmetadata.py和/usr/share/createrepo/worker.py文件中的python解释器,用刚才的方法
[root@why Python-2.7.13]# python --version
Python 2.7.13
virtualenv安装python3.5
编译python3.5
[root@why opt]# pip install virtualenv
[root@why opt]# wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tar.xz
[root@why opt]# tar xf Python-3.5.0.tar.xz
[root@why opt]# cd Python-3.5.0/
[root@why Python-3.5.0]# ./configure --prefix=/opt/python-3.5.0
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating Misc/python.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile
[root@why Python-3.5.0]# echo $?
0
[root@why Python-3.5.0]# make && make install
Ignoring indexes: https://pypi.python.org/simple
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-7.1.2 setuptools-18.2
创建虚拟环境
[root@why Python-3.5.0]# virtualenv -p /opt/python-3.5.0/bin/python3.5 ~/env35
Running virtualenv with interpreter /opt/python-3.5.0/bin/python3.5
Using base prefix '/opt/python-3.5.0'
New python executable in /root/env35/bin/python3.5
Also creating executable in /root/env35/bin/python
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pip, wheel...done.
[root@why Python-3.5.0]# source ~/env35/bin/activate
(env35) [root@why Python-3.5.0]# python --version
Python 3.5.0
(env35) [root@why Python-3.5.0]# deactivate
[root@why Python-3.5.0]# python --version
Python 2.6.6
会遇到的问题1
[root@why Python-3.5.0]# virtualenv -p /opt/python-3.5.0/bin/python3.5 ~/env35
Running virtualenv with interpreter /opt/python-3.5.0/bin/python3.5
/opt/python-3.5.0/bin/python3.5: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory
动态链接库没有被加载,需要添加到配置文件中并使之生效
会遇到的问题2
如果遇到python没有zlib,yum安装以下的包,重新编译
yum install zlib zlib-devel openssl openssl-devel readline readline-devel
会遇到的问题3
[root@why bin]# pip install virtualenv
Traceback (most recent call last):
File "/usr/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources
解决方式
[root@why bin]# curl https://bootstrap.pypa.io/ez_setup.py | python
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12537 100 12537 0 0 19819 0 --:--:-- --:--:-- --:--:-- 24391
ez_setup.py is deprecated and when using it setuptools will be pinned to 33.1.1 since it's the last version that supports setuptools self upgrade/installation, check https://github.com/pypa/setuptools/issues/581 for more info; use pip to install setuptools
Downloading https://pypi.io/packages/source/s/setuptools/setuptools-33.1.1.zip
Extracting in /tmp/tmp7fYHWx
Now working in /tmp/tmp7fYHWx/setuptools-33.1.1
Installing Setuptools
running install
running bdist_egg
running egg_info
省略部分
byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/register.py to register.pyc
byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/setopt.py to setopt.pyc
byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/build_py.py to build_py.pyc
byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/develop.py to develop.pyc
byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/bdist_rpm.py to bdist_rpm.pyc
byte-compiling build/bdist.linux-x86_64/egg/setuptools/command/bdist_egg.py to bdist_egg.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/entry_points.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying setuptools.egg-info/zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
creating dist
creating 'dist/setuptools-33.1.1-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing setuptools-33.1.1-py2.7.egg
Copying setuptools-33.1.1-py2.7.egg to /opt/python-2.7.13/lib/python2.7/site-packages
Adding setuptools 33.1.1 to easy-install.pth file
Installing easy_install script to /opt/python-2.7.13/bin
Installing easy_install-2.7 script to /opt/python-2.7.13/bin
Installed /opt/python-2.7.13/lib/python2.7/site-packages/setuptools-33.1.1-py2.7.egg
Processing dependencies for setuptools==33.1.1
Finished processing dependencies for setuptools==33.1.1
Hello python!
python代码实现
就和大学老师讲C++和Java一样,第一段代码都是从"Hello World"开始的,python实现的方式有两种——命令行和文件
命令行
[root@why ~]# python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world'
hello world
>>> print ('hello world')
hello world
>>> print('hello world')
hello world
文件
[root@why ~]# source ~/env35/bin/activate
(env35) [root@why ~]# vi hello.py
print('hello world')
(env35) [root@why ~]# python hello.py
hello world
(env35) [root@why ~]# vi hello.py
#print('hello world')
print 'hello world'
(env35) [root@why ~]# python hello.py
File "hello.py", line 2
print 'hello world'
^
SyntaxError: Missing parentheses in call to 'print'
python解释器
[root@why ~]# vi hello.py
[root@why ~]# chmod 755 hello.py
[root@why ~]# ./hello.py
hello world
[root@why ~]# cat hello.py
#!/opt/python-3.5.0/bin/python3
print('hello world')
#!/opt/python-3.5.0/bin/python3
是指定执行的时候通过那个程序来解析脚本的内容,'#!'称为幻数,这一行必须在脚本的顶端第一行,如果不是就内核就会认为是注释。其他行就可用#注释。#!/usr/bin/env python
则代表使用环境变量中的python进行解析
编码
python解释器在加载执行前先加载编码,在python2版本如果不指定编码,默认为ascii码。
- ASCII码是用来表示英文字符的一种编码规范。每个ASCII字符占用1个字节,最开始的时候表示英文已经没有任何的问题,又来对于更加复杂的语言,例如中文,就不够用了
- 各国开始使用各国的编码,例如中文使用GB2312来显示中文,而日本使用Shift_JIS来显示日文,各国有个多的标准,不好统一
- unicode——万国码,通过四个字节,可以容纳世界上所有的语言文件的编码,可以使工程国际化,不过四个字节过大,有些英文字符只需要一个字节就完全可以表达,就诞生了自动选择的utf-8
- utf-8是把unicode编码的字符根据不同的数字大小编码成1~6个字节,通常英文字母为1字节,中文为3字节,偏僻的才会是4~6字节
在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码,用记事本编辑的时候,从文件读取的UTF-8字符被转换为Unicode字符到内存里,编辑完成后,保存的时候再把Unicode转换为UTF-8保存到文件,例如web浏览器就会是浏览网页的时候,服务器会把动态生成的Unicode内容转换为UTF-8再传输到浏览器
[root@why practice_code]# vi unicode_code.py
print('王宏宇')
[root@why practice_code]# python unicode_code.py
File "unicode_code.py", line 1
SyntaxError: Non-ASCII character '\xe7' in file unicode_code.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
[root@why practice_code]# vi unicode_code.py
# -*- coding:utf-8 -*-
print('王宏宇')
[root@why practice_code]# python unicode_code.py
王宏宇
而python3默认为utf-8,可以不指定即可。
编码转换
在python2中,如果要把一个utf-8编码的why转换成gbk编码的why,需要两个步骤
对于why = wanghongyu
- 需要先去除utf-8编码,变为Unicode编码,通过why_unicode = why.decode('utf-8')
- 然后由Unicode编码变为gbk编码,通过why_gbk = why_unicode.encode('gbk')
而在python3中,直接转换成需要的gbk即可 - why_gbk = why.encode('gbk')
可能会遇到的问题
UnicodeEncodeError: 'ascii' codec can't encode characters in position 108-109: ordinal not in range(128)
在代码中加上以下三行
import sys
reload(sys)
sys.setdefaultencoding('utf-8')