跳到主要內容

Pyinstaller on Windows

Pyinstaller on Windows

Prerequisites

Create and activate virtualenv

virtualenv c:\venv\pyapp
cd c:\venv\pyapp\Scripts
activate

From now on, we’ll be working within a virtualenv environment.

PowerShell

If you would like to give PowerShell a try like me. Before activate your virtualenv, you need to change your script execution policy via

Set-ExecutionPolicy RemoteSigned

in a privileged PowerShell terminal.

Install pywin32 and pyinstaller

easy_install http://sourceforge.net/projects/pywin32/files/pywin32/Build%20219/pywin32-219.win32-py2.7.exe/download
pip install pyinstaller

The msvcr90.dll Problem

There is a known issue was discussed on StackOverflow. Quick solution is to install the specific Microsoft Visual C++ 2008 Redistributable Package (x86) which can be downloaded here.

If one test pyinstaller with naive Hello, world program. That issue won’t show up until some binary packages such as _socket or _ssl are used. In my case, it is SimpleHttpServer.

Here is the code I tested with pyinstaller from Python official document.

import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

Archive Python app via pyinstaller

pyinstaller --onefile app.py

Then you can find app.exe on the dist folder.

Deploy

  1. Install a MSVC redistributable (any version newer than 2008) on the target environment.
  2. Copy your app to the environment.

As mentioned above, my app is suffered from the msvc90r problem (I doubt that a serious app won’t). Hence to execute the app without problem, a MSVC redistributable runtime must be installed on deployed environment as well. Note the redistributable runtime newer than MSVC 90 (VS2008) may work with the app or not. I didn’t test it extensively and you are on your own. Some kudos maintain compatibility matrix over different MSVC redist versions on the Internet. Use the google, luke.

Minify/Optimize with Cython

Per this SO question You may have problem to build Cython extension with the compiler. As mentioned in one answer of the SO question, set two environment variables as following resolve the problem.

SET DISTUTILS_USE_SDK=1
SET MSSdk=1

  1. I choose 32bit (x86) to have one binary works for both 32bit and 64bit Windows (let’s take ia64 for ignorant). However you may reconsider this if your application need to work closely with Windows system. For example, a 32bit program need to specify special flag to access registry which seen by 64bit program only. In such case, your code can’t be agnostic. ↩︎

  2. The compiler is required by Cython only. You’d definitely wanna try Cython if you care about performance. ↩︎

留言

這個網誌中的熱門文章

得利油漆色卡編碼方式

得利油漆色卡編碼方式 類似 Munsell 色彩系統 ,編碼方式為 HUE LRV/CHROMA 例如 10GY 61/449 ( 色卡 ) 編碼數值 描述 10GY hue ,色輪上從 Y(ellow) 到 G(reen) 區分為 0 ~ 99 ,數值越小越靠近 Y,越大越靠近 G 61 LRV (Light Reflectance Value) 塗料反射光源的比率,數值從 0% ~ 100% ,越高越亮,反之越暗,也可理解為明度 449 chroma 可理解為彩度,數值沒有上限,越高顏色純度 (濃度) 越高 取決於測量儀器,對應至 RGB 並不保證視覺感受相同。 參考資料: 色卡對照網站 e-paint.co.uk Written with StackEdit .

C++17 新功能 try_emplace

C++17 新功能 try_emplace 回顧 emplace 大家的好朋友 Standard Template Library (STL) 容器提供如 push_back , insert 等介面,讓我們塞東西進去; C++11 之後,新增了 emplace 系列的介面,如 std::vector::emplace_back , std::map::emplace 等,差異在於 emplace 是在容器內 in-place 直接建構新元素,而不像 push_back 在傳遞參數前建構,下面用實例來說明: struct Value { // ctor1 Value ( int size ) : array ( new char [ size ] ) , size ( size ) { printf ( "ctor1: %d\n" , size ) ; } // ctor2 Value ( const Value & v ) : array ( new char [ v . size ] ) , size ( v . size ) { printf ( "ctor2: %d\n" , size ) ; memcpy ( array . get ( ) , v . array . get ( ) , size ) ; } private : std :: unique_ptr < char [ ] > array ; int size = 0 ; } ; struct Value 定義了自訂建構子 (ctor1),以指定大小 size 配置陣列,複製建構子 (ctor2) 則會配置與來源相同大小及內容的陣列,為了方便觀察加了一些 printf 。當我們如下使用 std::vector::push_back 時 std :: vector < Value > v ; v . push_back ( Value ( 2048 ) ) ; 首先 Value 會先呼叫 ctor1,傳給 push_ba...

Notes for C/C++ Programming

Notes for C/C++ Programming Notes for C/C++ Programming Off by One Error Prone # define LEN 128 char buf [ LEN ] = { 0 } ; // -1 for prevent missing NULL terminator // since strncpy doesn't preserve it for us. strncpy ( buf , something , sizeof ( buf ) - 1 ) ; // snprintf does preserve NULL terminator for us // i.e. actual size for placing characters is sizeof(buf) - 1 // but it takes extra time to parsing format snprintf ( buf , sizeof ( buf ) , something ) ; Bad things often happen when we mess with -1 or +1 to pointers point to continuous elements. Absorber # define LEN 128 char buf [ LEN + 1 ] = { 0 } ; // ^^^^ Always preserve 1 byte strncpy ( buf , somethin , sizeof ( buf ) ) ; Constant Correctness (C/C++) Bad void do_something ( char * dest , char * src ) ; struct MyList { void do_something ( std :: string & name ) ; } ; Reason : Will the name be modified by the lookup method? Will stat...