Prerequisites
- 64-bit Windows 2008/7
- Python 2.7.10 x86 1
- pip
- virtualenv
- Microsoft Visual C++ 2008 Redistributable Package (x86) or MSVC Compiler for Pyhton 2.7 2
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
- Install a MSVC redistributable (any version newer than 2008) on the target environment.
- 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
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. ↩︎
The compiler is required by Cython only. You’d definitely wanna try Cython if you care about performance. ↩︎
留言
張貼留言