Submit to PyPI (Python Package Index)

ref:
http://guide.python-distribute.org/
http://docs.python.org/2/distutils/packageindex.html
http://dormousehole.readthedocs.org/en/latest/patterns/distribute.html
http://www.ibm.com/developerworks/cn/opensource/os-pythonpackaging/

setup.py Example

#!/usr/bin/env python

import os
import sys

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup


if sys.argv[-1] == 'publish':
    os.system('python setup.py sdist upload')
    sys.exit()

long_description = open('README.rst').read() + '\n\n' + open('HISTORY.rst').read()

license = open('LICENSE').read()

requirements_lines = [line.strip() for line in open('requirements.txt').readlines()]
install_requires = list(filter(None, requirements_lines))

packages = [
    'haul',
    'haul.finders',
    'haul.finders.pipeline',
    'haul.extenders',
    'haul.extenders.pipeline',
]

setup(
    name='haul',
    version='1.3.2',
    description='An Extensible Image Crawler',
    long_description=long_description,
    keywords='haul web image content scraper parser crawler',
    author='Vinta Chen',
    author_email='vinta.chen@gmail.com',
    url='https://github.com/vinta/Haul',
    license=license,
    install_requires=install_requires,
    include_package_data=True,
    packages=packages,
    test_suite='tests',
    zip_safe=False,
    classifiers=(
        'Development Status :: 3 - Alpha',
        'Environment :: Web Environment',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Natural Language :: Chinese (Traditional)',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
        'Topic :: Multimedia :: Graphics',
        'Topic :: Software Development :: Libraries :: Python Modules',
        'Topic :: Utilities',
    ),
)

ref:
https://github.com/vinta/Haul/blob/master/setup.py
https://pypi.python.org/pypi?%3Aaction=list_classifiers

如果你的 project 只有一個檔案,例如 sbi.py,要用 py_modules 而不是 packages

py_modules=['sbi', ],

Configuration

因為 setuptools 默認只會打包 *.py 檔案
但是你可以在 package_data 中指定額外的檔案也一起打包進去
另外 package_data 裡的檔案在安裝的時候會繼承你原來的檔案讀寫權限

package_data = {
    # 任何目錄中含有 .txt 文件,都包含它
    '': ['*.txt', ],
    # 包含 demo 包 data 文件夹中的 *.dat 文件
    'demo': ['data/*.dat', ],
}

或是使用 MANIFEST.in

Commands

you need to register an account on PyPI (Python Package Index) to submit your package

$ python setup.py install

# 會安裝依賴的 packages 並 link 到 site-packages,而不是把檔案複製過去
$ python setup.py develop

# 你必須在 setup.py 裡指定 `test_suite`
$ python setup.py test

# 打包
$ python setup.py sdist

# 向 PyPI 註冊或更新你的 package 訊息(只是註冊而已,檔案要用下面那個 upload 指令上傳)
$ python setup.py register

# 打包並上傳到 PyPI
$ python setup.py sdist upload

pyenv: Python version management

pyenv 是一套用來管理 Python 版本的工具,類似 Ruby 的 rbenv 或 Node.js 的 nvm
https://github.com/yyuu/pyenv

Install

Before doing the following steps, you must install Command Line Tools.

$ brew update
$ brew install readline openssl

$ brew install pyenv pyenv-virtualenv pyenv-virtualenvwrapper
# or
$ brew upgrade pyenv pyenv-virtualenv pyenv-virtualenvwrapper

To enable shims and autocompletion add to your profile:
  if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

To use Homebrew's directories rather than ~/.pyenv add to your profile:
  export PYENV_ROOT=/usr/local/opt/pyenv

ref:
https://github.com/vinta/dotfiles
http://tbb.co/managing-python-on-os-x-with-pyenv/

Usage

# 列出可安裝的 Python 版本
$ pyenv install -l

# 安裝
$ pyenv install 2.7.9
$ pyenv install 3.4.3

# 列出已安裝的 Python 版本
$ pyenv versions

# 設置默認的 Python 版本
$ pyenv global 2.7.9

# 讓 Python 2 和 3 並存
$ pyenv global 2.7.9 3.4.3

# 切換回系統默認的版本
$ pyenv global system

# 設置當前目錄的 Python 版本
# 會在目錄下產生一個 `.python-version` 檔案
$ pyenv local 3.4.3

# 設置當前的 shell 使用的版本
$ pyenv shell 3.4.3

# Run this command after you install a new version of Python,
# or install a package that provides binaries.
$ pyenv rehash 

# 列出 easy_install 指令的真實路徑
$ pyenv which easy_install

# 列出有安裝 bpython 指令的 Python 版本
$ pyenv whence bpython

virtualenv / virtualenvwrapper

$ brew install pyenv-virtualenv
$ brew install pyenv-virtualenvwrapper

To enable auto-activation add to your profile:
  if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi

ref:
https://github.com/yyuu/pyenv-virtualenv
https://github.com/yyuu/pyenv-virtualenvwrapper