Python的环境基本配置

1. Python的环境基本配置

1.1 查看版本

安装Python(可以源码安装)
yum install python
查看当前python版本,在任意终端输入: 
python  --version

1.2 查看当前python位置

    在任意终端输入:which python

1.3 查看系统存在的所有python路径

在任意终端输入:whereis python

1.4 将特定的python版本加入系统路径PATH

         (1)打开bashrc,在任意终端输入:gedit ~/.bashrc ** 

         (2)输入: export PATH="/usr/local/python/:$PATH"

         (3)保存,退出

         (4)使改变生效:source ~/.bashrc

         (5)打开新的终端,输入which python,你会发现python已经改为你想要的版本了。

2. 配置python的pip

2.1 配置外部源

配置要同步的pypi源
vim ~/.pip/pip.conf(自己创建)

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

国内镜像
http://pypi.douban.com/simple/ 豆瓣
http://mirrors.aliyun.com/pypi/simple/ 阿里
http://pypi.hustunique.com/simple/ 华中理工大学
http://pypi.sdutlinux.org/simple/ 山东理工大学
http://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学
https://pypi.tuna.tsinghua.edu.cn/simple 清华

2.2 自建pypi源

安装pip(如安装pip-1.5.4.tar.gz)
pip install pip2pi

本地镜像位置
mkdir /pypi

pip2tgz /pypi name==version
比如: pip2tgz /pypi pbr==0.5.21

同步完成后要建立索引
dir2pi /pypi (对pypi目录下的 所有软件包建立索引)

配置web访问界面,以nginx为例
配置nginx

server {
         listen80;
         server_name FQDN;
location {
         root /path/to/python_packages/;
         autoindex on;
       }
}

下载需要的pip包:

pip2tgz /path/to/python_pakages -r packages_list.txt

创建索引

dir2pi /path/to/python_packages/

dir2pi会在/path/to/python_packages/目录下生成simple目录,里面记录所有包的索引信息供pip快速查找。

pip本地库到此搭建完毕。

使用方式:

方式1:
pip install numpy

方式2:
pip install –i http://FQDN/simple/ —-trusted-host FQDN pymysql

3. Python的lib路径

3.1 执行下面命令查看

$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

3.2 添加目录

Python 运行环境在查找库文件时是对 sys.path 列表进行遍历,如果我们想在运行环境中注册新的类库,主要有以下三种方法:

  • 在sys.path列表中添加新的路径。
    import sys
    sys.path.append(‘/usr/local/python/’)

  • 设置PYTHONPATH环境变量。
    命令窗口添加路径
    export PYTHONPATH=$PYTHONPATH:/home/songhaibin/

  • 将库文件复制到sys.path列表中的目录里(如site-packages目录)。

  • 最简单的办法是用 .pth 文件来实现。Python 在遍历已知的库文件目录过程中,如果见到一个 .pth 文件,就会将文件中所记录的路径加入到 sys.path 设置中,这样 .pth 文件说指明的库也就可以被 Python 运行环境找到。

猜你喜欢

转载自blog.csdn.net/yaffils79/article/details/82560623