linux下安装python
-去官网下载源码安装包或者lftp 172.25.254.250cd pub/software/pythonget Python-3.6.4.tgpython3.6-解压安装包到/opt目录tar xf Python-3.6.4.tgz -C /opt-安装编译过程中需要的依赖包:gcc,zlib,zlib-devel-进入解压的安装包进行编译cd /opt/Python3-*/./configure --prefix=/usr/local/python --with-ssl-安装:make && make install-添加python3的命令到环境变量里echo $PATH#临时添加export PATH='python3所在的路径:$PATH'#永久添加vim ~/.bashrc添加 export PATH="/usr/local/python/bin/:$PATH"或者 echo PATH="/usr/local/python/bin/:$PATH" >> ~/.bashrc
测试:
另开一个SHELL输入python3[root@sheen ~]# python3Python 3.6.4 (default, Aug 6 2018, 22:54:20) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linuxType "help", "copyright", "credits" or "license" for more information.>>>
~/ #表示当前用户家目录
python代码编写
python打印
python2中的print既是特殊字符又是函数
-python2: >>> print("hello") hello >>> print "hello " hello -python3 >>> print('hello') hello-python2向python3 >>> from __future__ import print_function #导入python2向python3打印的转换模块 >>> print "hello" File "", line 1 print "hello" ^ SyntaxError: invalid syntax >>> print("hello") hello
用命令编译文件
vim hello.py
#!/usr/local/python/bin/python3context="hello sheen"print(context)
chmod +x hello.py
cp hello.py /bin/welcome测试:[root@sheen bin]# welcomehello sheen
python的编码格式
python2:ASCIIpython3:Unicode
ASCII编码:
1字节=8bit,1英文字符=1字节,-----00000000(2^8-1)
Unicode编码:
一个字符代表两个字节,(2^16-1)
utf-8:
如果是英文,一个字节存储;如果是中文,用三个字节存储