PIXNET Logo登入

雪 薄草的部落格

跳到主文

歡迎光臨雪 薄草在痞客邦的小天地

部落格全站分類:

  • 相簿
  • 部落格
  • 留言
  • 名片
  • 4月 24 週六 201022:29
  • 如何編譯 pyd 檔 (for Windows)

   1. 必須先安裝 MinGW 軟體
   2. 安裝 MinGW 後,須手動 添加 {MinGW Home}\bin 到系統變數 PATH 裡
   3. 準備 setup.py (*1) 與 要包裝的 C 的檔案,這裡以 PyMod.c(*2) 為例
   4. 打開 cmd line
   5. → python.exe setup.py build -c mingw32
   6. build 完後會產生 build 資料夾 在當前路徑下,內有 lib.win32-2.4 與 temp.win32-2.4 二個資料夾, pyd 檔在 lib.win32-2.4
   7. 準備一個 測試用的 py 檔 , testPyd.py (*3),用來測試 pyd 檔是否可以使用



(*1) setup.py
from distutils.core import setup, Extension
 
module1 = Extension('PyMod', sources = ['PyMod.c'])
 
setup (name = 'PackageName',
        version = '1.0',
        description = 'This is a demo package',
        ext_modules = [module1])

(*2) PyMod.c
/*
   This program makes interface of two C functions to Python methods.
   Project: PC FARM Computing Centre ACDEMIA Sinica
 
   last update: Sep. 10, 1997
   Problem:
       There are two C functions named:
           int   CFunction1(char*, int, float);
           char* CFucntion2(long int, double);
       We want to have two method in Python module 'test':
           Method1(...) ---- which associated with CFunction1(...)
           Method2(...) ---- which associated with CFunction2(...)
*/
#include <stdlib.h>
#include <stdio.h>
#include "c:/portablepython1.0/include/Python.h"
/*******************************************************
  Declaration of C functions
********************************************************/
// a C function....
// just print the argument which user inputs.
int CFunction1( char* ptr, int a, float b){
   printf("string: %s\n",ptr);
   printf("int: %i   float: %f\n",a,b);
   return 1;
}
// another C function....
// just print the argument which user inputs.
char* CFunction2(long int a, double b){
   static char string[] = "CFunction2";
   printf("long int: %d    double: %g\n",a,b);
   return string;
}
/********************************************************
  Interface functions
********************************************************/
// interface function for CFunction1
static PyObject* Call_CFunction1(PyObject *self, PyObject* args){
   char *pstr;
   int a, result;
   float b;
   if(!PyArg_ParseTuple(args,"sif",&pstr,&a,&b)) return NULL;
   result = CFunction1(pstr,a,b);
   return Py_BuildValue("i",result);
}
// interface function for CFunction2
static PyObject* Call_CFunction2(PyObject* self, PyObject* args){
   char* result;
   long int a;
   double b;
   if(!PyArg_ParseTuple(args,"ld",&a,&b)) return NULL;
   result = CFunction2(a,b);
   return Py_BuildValue("s",result);
}
/***********************************************************
  Regiestration table
***********************************************************/
static struct PyMethodDef test_method[] = {
  {"Method1", Call_CFunction1, 1},
  {"Method2", Call_CFunction2, 1},
  {NULL,NULL}
};
void initPyMod(void){
   Py_InitModule("PyMod", test_method);
}

(*3) testPyd.py
import PyMod
a = PyMod.Method1("test", 10, 20.03)
b = PyMod.Method2(1234567890, 3.14159265)


參考文件:
   1. Python 編譯 pyd
   2. MinGW 官網
   3. MinGW Download
(繼續閱讀...)
文章標籤

雪 薄草 發表在 痞客邦 留言(0) 人氣(1,198)

  • 個人分類:Python
▲top
  • 4月 24 週六 201018:54
  • 如何將 Python 檔案編譯成 exe (執行檔) - py2exe

Q. 如何將 Python 檔案編譯成 exe (執行檔)
  • 下載與你使用的 Python 相對應的版本的 py2exe 編譯工具,並且安裝好它
  • 準備 steup.py 檔案 (*1) 與 你要包裝的 python 檔案
  • 打開 cmd line
  • → python setup.py install
  • → python setup.py py2exe
  • 完成後會在 此路徑裡 產生出 dist 跟 build 二個資料夾,執行檔與所須的檔案全在 dist 裡。

  • *1  setup.py 檔案內容

    from distutils.core import setup
    import py2exe
    setup(console=['hello.py'])
    hello.py 是指你寫好的 py 檔。

    參考文件:
  • py2exe官網
  • py2exe Tutorial

  • (繼續閱讀...)
    文章標籤

    雪 薄草 發表在 痞客邦 留言(0) 人氣(14,626)

    • 個人分類:Python
    ▲top
    1

    自訂側欄

    自訂側欄

    個人資訊

    雪 薄草
    暱稱:
    雪 薄草
    分類:
    好友:
    累積中
    地區:

    熱門文章

    • (1,198)如何編譯 pyd 檔 (for Windows)
    • (15,834)should be 的用法和單個 be 的用法

    文章分類

    • 音樂 (4)
    • Language (1)
    • AJAX (4)
    • Windows 指令 (1)
    • Linux 指令 (3)
    • JavaScript (2)
    • Java Applet (2)
    • JAVA (1)
    • 健康 (1)
    • Python (2)
    • 文章 (4)
    • 知識 (8)
    • Web安全 (4)
    • 笨蛋的英文筆記 (3)
    • 未分類文章 (1)

    最新文章

    • 10 tips for perfect pronunciation/10種完美發英的秘訣 (中英對照 精華文章)
    • 『資料隱碼』SQL Injection的源由與防範之道
    • SQL Injection – 駭客的 SQL填空遊戲(下)
    • SQL Injection (資料隱碼)– 駭客的 SQL填空遊戲(上)
    • XSS攻擊防禦技術白皮書
    • 卡內基的人生態度 -- 不批評、不責備、不抱怨
    • 一生受用的話
    • 如果你已經二十歲了,別再孩子氣了 - 李開復
    • 給孩子的備忘錄
    • 不要隨便牽手,更不要隨便放手

    動態訂閱

    文章精選

    文章搜尋

    誰來我家

    參觀人氣

    • 本日人氣:
    • 累積人氣: