Cython編譯

Windows

Microsoft Visual C++ Compiler for Python (for python 2.7)

  • 安裝Microsoft Visual C++ Compiler for Python
  • 建立setup.py檔,檔案引用如下:

    try:
      from setuptools import setup
      from setuptools import Extension
    except ImportError:
      from distutils.core import setup
      from distutils.extension import Extension
    
  • 進入[開始] > [Microsoft Visual C++ Compiler Package for Python 2.7] > Visual C++ 2008 64-bit Command Prompt,輸入以下指令後,進入cython project下,進行編譯 python setupy.py build_ext --inplace --compiler=msvc.

SET DISTUTILS_USE_SDK=1
SET MSSdk=1

Ubuntu linux

  • sudo apt-get install build-essential 安裝gcc,然後就可以編譯了。

setup.py範本

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

ext_modules=[
    Extension(
        name="demo",
        sources=["demo.pyx"],
        libraries=["m"] # Unix-like specific
    ),

     Extension(
        name="demo2",
        sources=["demo2.pyx"],
        libraries=["m"] # Unix-like specific
        include_dirs = [np.get_include()],
    )
]

setup(
    # 專案名稱
    name = 'my_project',

    # 專案版本
    version='1.0',

    # 作者訊息
    author = 'Hung-Hsin Chen',
    author_email = '[email protected]',

    # 模組
    ext_modules = cythonize(ext_modules)
)
  • setup函數還有一些參數:

    • packages: 告訴Distutils需要處理那些包(包含init.py的資料夾)
    • package_dir: 告訴Distutils哪些目錄下的檔被映射到哪個源碼包,感覺好像是一個相對路徑的定義。一個例子:package_dir = {'': 'lib'},表示以lib為主目錄。
    • ext_modules: 是一個包含Extension實例的清單,Extension的定義也有一些參數。
    • ext_package: 定義extension的相對路徑
    • requires: 定義依賴哪些模組
    • provides: 定義可以為哪些模組提供依賴
    • scripts: 指定python源碼檔,可以從命令列執行。在安裝時指定--install-script
    • package_data 通常包含與包實現相關的一些資料檔案或類似於readme的檔。

      package_data = {'': ['*.txt'], 'mypkg': ['data/*.dat'],}
      
    • data_files: 指定其他的一些文件(如設定檔): 規定了哪些檔被安裝到哪些目錄中。如果目錄名是相對路徑,則是相對於sys.prefix或sys.exec_prefix的路徑。如果沒有提供範本,會被添加到MANIFEST檔中。

      data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
                  ('config', ['cfg/data.cfg']),
                  ('/etc/init.d', ['init-script'])]
      

setup.cfg

  • setup.cfg提供一種方式,可以讓包的開發者提供命令的預設選項,同時為使用者提供修改的機會。對setup.cfg的解析,是在setup.py之後,在命令列執行前。
  • setup.cfg檔的形式如下:
    • 其中,command是Distutils的命令參數,option是參數選項,可以通過python setup.py --help build_ext方式獲取。
      [command]
      option=value
      ...
      

Setuptools

  • 上面的setup.py和setup.cfg都是遵循python標準庫中的Distutils,而setuptools工具針對Python官方的distutils做了很多針對性的功能增強,比如依賴檢查,動態擴展等。
    • include_package_data:為True時自動添加受版本控制的資料檔案,可替代package_data,同時,exclude_package_data可以排除某些檔。當你需要加入沒有被版本控制的檔時,還是老老實實使用package_data吧。
    • install_requires:代替require函數。表示當前包的安裝依賴於哪些分發包,這些資訊會寫入egg的元資訊中,這些包在安裝時會自動(從PyPI)下載並安裝。如果包在PyPI中找不到,則會從dependency_links標識的URL中獲取。
    • extras_require:當前包的高級/額外特性需要依賴的分發包。
    • entry_points:這個很經典。見下麵的講解。
    • setup_requires: 安裝腳本執行時需要依賴的分發包,主要用於構建過程。注意,這裡列出的包不會自動安裝,如果需要,同時要在install_requires中指定。
    • dependency_links:URL地址。這些位址在安裝setup_requires或tests_require指定的包時使用。會寫入egg的metadata資訊中。
from setuptools import setup, find_packages
setup(
    name = "HelloWorld",
    version = "0.1",
    packages = find_packages(),
    scripts = ['say_hello.py'],
    # Project uses reStructuredText, so ensure that the docutils get
    # installed or upgraded on the target machine
    install_requires = ['docutils>=0.3'],
    package_data = {
        # If any package contains *.txt or *.rst files, include them:
        '': ['*.txt', '*.rst'],
        # include any *.msg files found in the 'hello' package, too:
        'hello': ['*.msg'],
    },
    # metadata for upload to PyPI
    author = "Me",
    author_email = "[email protected]",
    description = "This is an Example Package",
    license = "PSF",
    keywords = "hello world example examples",
    url = "http://example.com/HelloWorld/",   # project home page, if any
    # could also include long_description, download_url, classifiers, etc.
)

easy_install and pip

  • 在 Python 標準程式庫中,是有個 distutils,可用來建立與安裝額外模組,適用於簡易的安裝場合,而在過去,有些程式庫也進一步對 distutils 進行了擴充,這些程式庫在發展的過程中,有過一段混亂的時局,像是先有了 Setuptools,而後又有了 Distrubute,然後 Distribute 又回併至 Setuptools。

  • 過去無論是安裝 Setuptools 或是 Distribute,都有 easy_install 可以使用,有一陣子都是用它來安裝套件,然而,後來也被 Pip 取代了。

  • 就結論而言,在 Python 2.x 中想要安裝套件,Pip 是 Python 社群目前最建議的方式。而從 Python 3.4 開始也從善如流,開始內建了 Pip!

  • easy_install 跟 pip 都是 Python 的套件管理程式,有了它們,在使用 Python 開發程式的時候會帶來不少方便。而pip 改善了不少 easy_install 的缺點。

function easy_install pip
安裝套件 easy_install pip install
更新套件 easy_install -U pip install -U
移除套件 easy_install -m pip uninstall
顯示說明 easy_install --showhelp pip help
搜尋套件 NA pip search

參考資料

results matching ""

    No results matching ""