Python开发环境以及包的管理

Python包的管理[1]

Package Management非常重要,模块化开发便于统一安装,下载,和管理以及使用。使用打包的方式使得我们的项目变得易用而且稳定,便于版本管理。这里使用python自带的distutils来实现源程序的打包。关于其他的包管理工具(distutils,setuptools,distribute,easy_install,pip,distutils2)的使用参考包管理工具之间关系,以及Python管理工具

1. 基本概念

python语境下的module是复用代码的基本单元,包括三个类别(pure Python modules, extension modules, and packages)

  • pure Python module

    a module written in Python and contained in a single .py file.

  • extension module
    其他语言( C/C++,Java )写成的模块,一般是预编译好的,例如.so文件,DLL文件等
  • package

    a module that contains other modules; typically contained in a directory in the filesystem and distinguished from other directories by the presence of a file init.py

  • root package

    every directory listed in sys.path contributes modules to the root package

2. 工程的结构化[2]

Kenneth Reitz推荐的仓库模板如下所示:

package的init.py非常重要,最常见的作用是把同级文件的类,函数以及,module等import到最外层去,方便调用

3. 编写setup脚本

setup脚本是Distutils构建、发布、安装模块的核心。包括元数据的编写,以及包结构的组织。元数据包括name(name of the package),version(version of this release),author(package author’s name),author_email(email address of the package author),description(short, summary description of the package)…..的信息。
下面是一个例子文件的组织

1
2
3
4
5
6
7
8
9
setup.py
src/
mypkg/
__init__.py
module.py
data/
tables.dat
spoons.dat
forks.dat

相应的setup()为

1
2
3
4
5
setup(...,
packages=['mypkg'],
package_dir={'mypkg': 'src/mypkg'},
package_data={'mypkg': ['data/*.dat']},
)

module distribution
a collection of Python modules distributed together as a single downloadable resource and meant to be installed en masse.

4.发布源程序

  • 发布源程序
    发布,如果没有设置特定的格式,根据所在的平台生成默认的格式python setup.py sdist
    sdist creates the archive of the default format for the current platform。he default format is a gzip’ed tar file (.tar.gz) on Unix, and ZIP file on Windows.
    You can specify as many formats as you like using the —formats option, for example:
    python setup.py sdist --formats=gztar,zip
    支持的格式如下所示:

  • 发布二进制编译好的包
    发布binary packagepython setup.py bdist
    发布特定格式的二进制包python setup.py bdist --format=zip
    支持的格式如下所示:

    创建RPM包python setup.py bdist_rpm
    创建windows安装包python setup.py bdist_wininst

  • 发布到PyPI上去
    Python Package Index(PyPI)是python第三方库管理平台。可以将开发的包上传到该平台上去。
    PyPI还有国内镜像,如果安装包的时候比较慢,可以使用国内镜像。此外,还可以建立自己私有的pPyPI包管理平台。
    例如清华的镜像:
    • 临时使用
      pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
      注意,simple 不能少, 是 https 而不是 http
    • 设为默认
      修改 ~/.pip/pip.conf (没有就创建一个), 修改 index-url至tuna,例如
      1
      2
      [global]
      index-url = https://pypi.tuna.tsinghua.edu.cn/simple

5.包的安装

  1. 可执行python setup.py install
  2. 也可使用pip安装,pip已成为包安装的事实标准
    • 安装pip install SomePackage
    • 查看已经安装的包pip list --outdated哪些需要更新
    • 升级包pip install --upgrade SomePackage
    • 卸载包pip uninstall SomePackage

python独立开发环境的构建[3]

  1. 使用virtualenv建立独立的python开发环境(也就是为不同的项目配置自个使用的依赖模块),对于保持干净、独立(隔绝)、有序、高效的环境非常有用。python的几大神器之一,还有诸如pip,fabric等工具等。

    • 安装$ pip install virtualenv
    • 新建virtualenv pyenv
    • 激活虚拟环境source venv/bin/activat
    • 退出虚拟环境deactivate
    • 创建requirements.txt 文件pip freeze > requirements.txt
    • 安装时就可以按需求安装好pip install -r requirements.txt
  2. 管理virtualenv

    • virtualenvwrapper封装在virtualenv之上,让开发者和虚拟环境的相处更加愉快。
    • 安装pip install virtualenvwrapper
    • 配置.bashrc文件
      export WORKON_HOME=~/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh 当然上个路径不一定相同,需要which virtualenvwrapper.sh
    • 基本使用

      • 创建虚拟环境mkvirtualenv venv
      • 使用某虚拟环境workon venv
      • 列出所有工作环境lsvirtualenv
      • 切换到某工作环境cdvirtualenv
    • pip list -l 列出目前虚拟环境的安装列表


fabric

自动化神器。
http://fabric-chs.readthedocs.io/zh_CN/chs/tutorial.html


Pycharm

  • 快捷键:
    ctrl+z 退一步;ctrl+shift+z 返回上一步
    alt+enter万能键 智能补充import 或其他功能

PEP 8:Python风格指南


代码同步

环境部署在远程开发机,而代码使用本地IDE编写(不想用VIM),需要需要找到一个代码实时同步的工具,把本地编写的代码传到开发机上。
https://github.com/ysmood/nobone-sync


参考文献

  1. https://docs.python.org/2.7/distutils/
  2. http://pythonguidecn.readthedocs.io/zh/latest/writing/structure.html
  3. http://pythonguidecn.readthedocs.io/zh/latest/dev/virtualenvs.html
  4. http://blog.useasp.net/archive/2014/09/09/packaging-python-libraries-and-upload-to-pypi-python-package-index.aspx