This Season
 

How to Compile Python Code

Since Python is an interpreted language, compilation of Python code can mean many things, including compilation to byte code or transformation to another language. When you learn how to take Python code and compile it into an executable for Windows platforms, you can create a Python program and have Windows users seamlessly run it.

Related Searches:
    Difficulty:
    Moderate

    Instructions

    Things You'll Need

    • Windows computer with a Python environment installed on it
    1. Set Up

      • 1

        Create your Python program or import it into the Windows environment.

      • 2

        Run your code in the Python interpreter and make sure there are no errors in the code:
        c:Python\> Python mycode.py

      • 3

        Download the py2exe win32 compiler from the py2exe website (see Resources below).

      • 4

        Save the downloaded file to disk.

      • 5

        Open the downloaded file and click through the installation process. This should take a few seconds. The installation will create a py2exe.exe program in your distutils directory distributed with your Python installation that you will use to create the compiled code.

      Compile Python Code

      • 1

        Open a new text file in Notepad or your favorite editor and save it as mysetupfile.py. This text file will tell the py2exe program which Python programs to convert into an exe.

      • 2

        Add the following lines in the text file:
        from distutils.core import setup
        import py2exe
        setup(console=['mycode.py'])

        where "mycode.py" is the name of your Python program you wish to compile.

      • 3

        Run the mysetupfile.py file through the py2exe program:
        c:\Python> Python mysetupfile.py py2exe

      • 4

        Wait until the py2exe compiler finishes producing its output. If all goes well, the compiler may inform you of the DLLs that your Python program is dependent on once it finishes compiling.

      • 5

        Wait until the compilation is done. Once it is, a directory called "dist" will be created. Enter the directory by type:
        c:\Python> cd dist

      • 6

        Look in the directory to see an exe of your project. Test the compile by running the program:
        c:\Python> mycode.exe

    Tips & Warnings

    • Frederick Lundh's Compiling Python web page has a rundown on a variety of different techniques and platforms to compile Python (see Resources below). You can find tips for compiling to byte code, Mac and Unix environments.

    • There are a number of Windows popular Python packages that may have gotchas when you are trying to compile. Some of these packages include wxPython, numPy and pyXML. If you are having difficulty with external modules or packages, consult the py2exe website (see Resources below).

    • The compilation process creates only an exe file, not a fully installable package. You will need to use a utility like MSI or Innosetup to create a full installer package once the exe is done.

    Related Searches

    Resources

    Read Next:

    Comments

    • chr1573r Nov 10, 2009
      Worked perfectly for the purpose intended, thanks!
    • bunkbomb Jul 18, 2009
      Back in python, add a line of code that waits for a user response (e.g. raw_input("Press enter when finished with result")). The program then doesn't finish running until you've given input, letting you look at your result for as long as you like.
    • bunkbomb Jul 18, 2009
      Back in python, add a line of code that waits for a user response (e.g. raw_input("Press enter when finished with result")). The program then doesn't finish running until you've given input, letting you look at your result for as long as you like. ~bunkbomb
    • csreddy Jul 09, 2009
      The exe works fine, but when i click my exe file , the console shows the result and close immediately.Is there anyway to make console wait for it ? Thanks,

    You May Also Like

    • How to Compile a Python File

      Python is slightly different from most other programming languages, both higher- and lower-level, in that it does not need to be compiled...

    • How to Create a Regular Expression in Python

      Searching for patterns in text can come in handy in a wide variety of tasks. With regular expressions, you can harness Python...

    • How to Convert Python to EXE

      The simplest way to create EXE files from Python programs is to use the open source py2exe program hosted on SourceForge. Most...

    • How to Compile C Code as a Library

      The C programming language lets you create code for executable or dynamic link library (DLL) files. A DLL contains several functions that...

    • How to Learn Python for Free

      Python is an excellent programming language, and thankfully, there are many free resources for learning it. You don't need to go out...

    • How to Compile Chrome OS Source Code

      Google's entry into the operating system market is called Chrome OS. Chrome is unique among operating systems, in that it relies heavily...

    • How to Make Python Script Executable

      One reason for Python's increased popularity as a high-level programming language is its portability. Scripts written on a Mac, for example, will...

    • How to Run a Python Script

      Python is a scripting language that runs on many different operating systems. Python is capable of managing and parsing files, pruning databases,...

    • How to Convert CPP File to EXE

      A .cpp is a file extension that is used for C++ source files. A file with the .cpp extension contains C++ programming...

    • How to Create a Tuple in Python

      Python has three types of complex data types called lists, tuples and dictionaries. A tuple is unique because its elements are ordered...

    Follow eHow

    Related Ads