Differences between revisions 9 and 35 (spanning 26 versions)
Revision 9 as of 2008-12-10 19:46:20
Size: 5920
Editor: JohnPosner
Comment:
Revision 35 as of 2020-06-17 12:17:05
Size: 342
Editor: FlorianTom
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= Tutorial =

py2exe turns Python programs into packages that can be run on other Windows computers without needing to install Python on those computers. Python is needed on the computer where py2exe itself is run because py2exe is a Python program and it includes parts of Python in the package that is built.

To successfully complete this tutorial you'll need to know the basics of Python (you can get started at python.org's [[http://www.python.org/about/gettingstarted/|getting started]] page). You'll also need to know [[http://www.python.org/doc/faq/windows/#how-do-i-run-a-python-program-under-windows|how to run Python programs from the command prompt]].

There are a few simple steps needed to use py2exe once you've installed it:

   1. [[#Step1|Create/test your program]]
   2. [[#Step2|Create your setup script (setup.py)]]
   3. [[#Step3|Run your setup script]]
   4. [[#Step4|Test your executable]]
   5. [[#Step5|Build an installer if applicable]]

<<Anchor(Step1)>>
== Create/test your program ==

The biggest step is almost always the first one. The good news is that py2exe typically has little or no impact on this step. The vast majority of things you can do with Python will work with py2exe. Many modules just work seamlessly with py2exe, but some third party modules will require a little extra work. Luckily there is help available at WorkingWithVariousPackagesAndModules.

It's important that you make sure everything is working before you use py2exe. If py2exe fixes a broken program, then that's probably a bug in py2exe that needs to be fixed!

The first example we'll use here is our old friend...

{{{#!python
print "Hello World!"
}}}attachment:hello.py

We need to make sure it's working...

{{{
C:\Tutorial>python hello.py
Hello World!

C:\Tutorial>
}}}

Looks good!

<<Anchor(Step2)>>
== Create your setup script (setup.py) ==

py2exe extends [[http://www.python.org/doc/current/dist/|Distutils]] with a new "command". If you've installed third party Python modules then there's a good chance you've seen at least one distutils command:

{{{
C:\Tutorial>python setup.py install
}}}

"install" is a Distutils command that installs something (typically a Python module or package). The details Distutils needs to do that installation are contained in setup.py (and sometimes other associated files).

"py2exe" is a new Distutils command that is added when you import py2exe. To use py2exe you need to create a setup.py file to tell Distutils and py2exe what you want to do. Here's a setup.py whose simplicity is appropriate for our sample program...

{{{#!python
from distutils.core import setup
import py2exe

setup(console=['hello.py'])
}}}attachment:setup.py

Notice that this is ordinary Python. Let's go through it line by line...

 1. When working with py2exe the only part of Distutils we'll typically need to reference directly is the setup function, so that's all we'll import.
 2. Once Distutils is loaded, we need to load py2exe so that it can add its command.
 3. Whitespace is good!
 4. Call setup and tell it that we want a single console application and the main entry point is "hello.py".

<<Anchor(Step3)>>
== Run your setup script ==
The next step is to run your setup script. Make sure to give the py2exe command and expect to see lots and lots of output:

{{{
C:\Tutorial>python setup.py py2exe
running py2exe
*** searching for required modules ***
*** parsing results ***
creating python loader for extension 'zlib'
creating python loader for extension 'unicodedata'
creating python loader for extension 'bz2'
*** finding dlls needed ***
*** create binaries ***
*** byte compile python files ***
byte-compiling C:\Tutorial\build\bdist.win32\winexe\temp\bz2.py to bz2.pyc
byte-compiling C:\Tutorial\build\bdist.win32\winexe\temp\unicodedata.py to unicodedata.pyc
byte-compiling C:\Tutorial\build\bdist.win32\winexe\temp\zlib.py to zlib.pyc
skipping byte-compilation of c:\Python24\lib\StringIO.py to StringIO.pyc

[skipping many lines for brevity]

skipping byte-compilation of c:\Python24\lib\warnings.py to warnings.pyc
*** copy extensions ***
*** copy dlls ***
copying c:\Python24\lib\site-packages\py2exe\run.exe -> C:\Tutorial\dist\hello.exe

*** binary dependencies ***
Your executable(s) also depend on these dlls which are not included,
you may or may not need to distribute them.

Make sure you have the license if you distribute any of them, and
make sure you don't distribute files belonging to the operating system.

   ADVAPI32.dll - C:\WINDOWS\system32\ADVAPI32.dll
   USER32.dll - C:\WINDOWS\system32\USER32.dll
   SHELL32.dll - C:\WINDOWS\system32\SHELL32.dll
   KERNEL32.dll - C:\WINDOWS\system32\KERNEL32.dll

C:\Tutorial>
}}}

Two directories will be created when you run your setup script... build and dist. The build directory is used as working space while your application is being packaged. It is safe to delete the build directory after your setup script has finished running. The files in the dist directory are the ones needed to run your application. If you are using Python 2.4 or newer then MSVCR71.dll will be included in dist - this is a Microsoft DLL and you need to determine if you have the legal right to distribute it.

<<Anchor(Step4)>>
== Test your executable ==
Now that the package has been created it is ready to test:

{{{
C:\Tutorial>cd dist

C:\Tutorial\dist>hello.exe
Hello World
}}}

Excellent, it works!!!

<<Anchor(Step5)>>
== Build an installer if applicable ==
py2exe is not an installer builder - it merely assembles the files needed to run your Python program. There are plenty of good installer builders out there including some that are open source (e.g., [[http://nsis.sourceforge.net/Main_Page|NSIS]]) and some that are free (e.g., [[http://www.jrsoftware.org/isinfo.php|Inno Setup]]).
The branch delay slot is a side impact of pipelined architectures due to the branch hazard, i.e. the fact that the department would not be resolved until the instruction has worked its means via the pipeline.<<<BR>>
><<<BR>>
>
<<<BR>>
><<<BR>>
>
Here is my web blog - [[https://pussyqueen888.com|สล็อตออนไลน์]]

The branch delay slot is a side impact of pipelined architectures due to the branch hazard, i.e. the fact that the department would not be resolved until the instruction has worked its means via the pipeline.<
><
> <
><
> Here is my web blog - สล็อตออนไลน์

Tutorial (last edited 2020-06-17 15:15:20 by JimmyRetzlaff)