一 编译安装
通过python源码下载网站下载源码包,然后解压
./configure --prefix=/usr --exec_prefix=/usr
make
make install
1、Python-related paths and files(python相关的路径与文件)
上述安装中,prefix 和 exec_prefix 参数用于指定相关文件存放路径,prefix默认路径/usr/local,exec-prefix默认同prefix,详解如下:
File/directory | Description |
---|---|
exec_prefix/bin/python | interpreter.解释器的存放路径 |
prefix/lib/pythonversion, exec_prefix/lib/pythonversion | 标准库路径 |
prefix/include/pythonversion, exec_prefix/include/pythonversion | 扩展文件路径 |
~/.pythonrc.py | 用户指定的初始化文件路径 |
二、环境变量和sys库
- PYTHONHOME
安装python的时候使用,用于取代prefix 和exec_prefix,如果想要指定不同的值,可以使用 PYTHONHOME=prefix:exec_prefix此方式设置 - PYTHONPATH
PYTHONPATH是python搜索库的路径,可以通过sys.path打印出所有的库路径,一般PYTHONPATH用于设置第三方库路径或者是自定义的路径,可以使用如下方式添加path路径:
sys.path.insert(0, "/home/yiifung/test") # 等价于 sys.path.insert(1, "/home/yiifung/test"),是永久性添加
sys.path.insert(index, path) 在index之前插入path
>>> sys.path
['', '/home', '/home/yiifung/test', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
>>>sys.path[0]
''
>>>
删除路径,使用sys.path.remove,即
>>> sys.path.remove("/home")
>>> sys.path.remove("/home/yiifung/test")
>>> sys.path
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
>>> quit()
也可以使用PYTHONPATH环境变量设置第三方库路径,示例如下:
[yiifung@master01 ~]$ ls -l hello.py
-rw-rw-r--. 1 yiifung yiifung 72 May 9 06:51 hello.py
[yiifung@master01 ~]$ cat hello.py
import sys
from fun import *
print "hahah"
x=1
y=2
z=add(x,y)
print(z)
[yiifung@master01 ~]$ PYTHONPATH=/home/yiifung/
[yiifung@master01 ~]$ ll package/
total 8
-rw-rw-r--. 1 yiifung yiifung 39 May 9 06:50 fun.py
-rw-rw-r--. 1 yiifung yiifung 263 May 9 06:51 fun.pyc
[yiifung@master01 ~]$ python hello.py
Traceback (most recent call last):
File "hello.py", line 2, in <module>
from fun import *
ImportError: No module named fun
[yiifung@master01 ~]$ PYTHONPATH=/home/yiifung/package/
[yiifung@master01 ~]$ python hello.py
hahah
3
[yiifung@master01 ~]$
注意 设置库搜索路径,一般有两种方式,sys.path和设置PYTHONPATH环境变量,sys.path是永久设置,PYTHONPATH一般用作临时,具体情况具体分析。
三、脚本格式
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#!/usr/bin/python
其中-- coding: UTF-8 -- 是用来指定Python源文件的字符编码,python2默认是ASCII,python3是默认是UTF-8;python2系列默认是不支持中文,只有使用 utf-8 等支持中文的编码,才可以使用中文。所以当使用python2的时候最好加上此注释;
其中#!/usr/bin/env python或者#!/usr/bin/python 用于告知系统用什么命令来运行此脚本,只在unix系统中可用,类似于#!/usr/bin/bash,如果设置了该行,可以使用./test.py命令执行运行(前提是test.py有执行权限),如果没有设置该行,只能使用python test.py执行
[yiifung@master01 ~]$ ./hello.py
bash: ./hello.py: Permission denied
[yiifung@master01 ~]$ chmod +x hello.py
[yiifung@master01 ~]$ ./hello.py
./hello.py: line 1: import: command not found
./hello.py: line 2: import: command not found
./hello.py: line 3: print: command not found
./hello.py: line 6: syntax error near unexpected token `('
./hello.py: line 6: `z=fun.add(x,y)'
[yiifung@master01 ~]$ vi hello.py
[yiifung@master01 ~]$
[yiifung@master01 ~]$
[yiifung@master01 ~]$
[yiifung@master01 ~]$ ./hello.py
hahah
3
[yiifung@master01 ~]$ chmod -x hello.py
[yiifung@master01 ~]$ ./hello.py
bash: ./hello.py: Permission denied
[yiifung@master01 ~]$ chmod +x hello.py
[yiifung@master01 ~]$ ./hello.py
hahah
3
[yiifung@master01 ~]$
其中#!/usr/bin/env python 的使用逻辑是,在/usr/bin/env的输出的环境变量PATH中寻找python的执行路径,然后执行
引用
https://docs.python.org/3.0/using/cmdline.html