How to build an executable application from your Python script (QT special)This is a guide on how to build a executable windows file from a Python script. It is especially focused on how to solve the problems that might occur while building a python QT application. For me, it is important that I easily can distribute my applications and if you give someone who isn’t as geeky as yourself some Python script they most probably won’t know what to do with it. What we instead want to do is to create a executable file that can be run without having Python installed. This can be done with the help of py. A note for you cool Linux or Mac users out there: This guide only covers windows. Mac users can use py. You badass Linux users can find a guide at omgwtfgames. We at popdevelop might post a guide for you later on. Lets start with the outline of what we need to do to build our executable: Create a Python program. Download and install py. Create a setup. py script. Run the setup. py script. Create a Python program. This shouldn’t be so hard, but since I’m a sucker for GUI applications I will give you two options here. Regardless of the option you choose in this example the scripts should be saved as “main. The first option is to create a simple python script that prints “Hello Executable” in the console. This is done as shown below. The second option is to use a QT GUI Python application. I wrote a guide here on popdevelop on how to do this last week so if you don’t know how to implement a GUI app using QT please follow the Setting up IDE and creating a cross platform QT Python GUI application guide. The code in script we used in that example is shown below and requires that you have installed Py. QT. Installing py. Go to py. 2exe’s sourceforge page to download the installer. Use the one suitable for your Python version. In this guide we use Python 2. Python Setup Py Install Distutils Python DownloadExecute the installation file. If you have installed Python the installation application should find your python installation and you should have something that looks like the figure below. Don’t you just love that blue color?! The lovely py. 2exe installer. Step through the installation and py. You can check this out by going to the python shell and importing py. This is shown in the figure below. The setup function now is receiving a parameter windows=. Again create the executable running python. This creates two different files; a C/C++ source file example. The generated C source file contains the.
Check that py. 2exe is installed. If this does not work you should troubleshoot the installation of Python or py. Leave a comment on this post and I will try to help you out! Create a setup. py script. To configure how you want the executable file to behave you will need to create a setup script. If modules are missing or if something goes wrong while creating your executable this is probably where you will fix it. For the first “Hello Executable” option used in this guide we won’t be needing anything fancy. The setup. py should contain the code shown below. To generate the executable go to the folder where your main. This will generate a “build” folder and a “distr” folder. In the distr folder you will find your . ![]() If we used this setup. Py. QT script we would get the error below: C: \src\dist> main. Traceback (most recent call last): File . Below you see the script that solves the Import. Error problems. What this does is that it runs a windows application with no console. Then the options parameter has been added making py. Sip, which it complained about earlier and Py. Qt. 4. Qt. Gui. The Qt. Gui import is there because for some reason the application will not import Py. Qt. 4. Qt. Core from Qt. Gui when py. 2exe has built it. Conclution. This guide shows you how to build an executable out of a python script. If you have any problems while following this guide please leave a comment and I will try to fix it for you. Further reading. Now that you’ve got your setup script and built your exe there might be some energy left in you so here’s a couple of sites that you might want to visit. The peeps over at bytes. It’s dated but has lived from 2. It also shows how to build a Mac version of your Py. Qt Python script. Python Extension Programming with CAny code that you write using any compiled language like C, C++, or Java can be integrated or imported into another Python script. This code is considered as an . On Unix machines, these libraries usually end in . On Windows machines, you typically see . Pre- Requisites for Writing Extensions. To start writing your extension, you are going to need the Python header files. On Unix machines, this usually requires installing a developer- specific package such as python. Windows users get these headers as part of the package when they use the binary Python installer. Additionally, it is assumed that you have good knowledge of C or C++ to write any Python Extension using C programming. First look at a Python Extension. For your first look at a Python extension module, you need to group your code into four part . You need to follow the includes with the functions you want to call from Python. The C Functions. The signatures of the C implementation of your functions always takes one of the following three forms . There is no such thing as a void function in Python as there is in C. If you do not want your functions to return a value, return the C equivalent of Python's None value. The Python headers define a macro, Py. They are defined as static function. Your C functions usually are named by combining the Python module and function names together, as shown here . You will be putting pointers to your C functions into the method table for the module that usually comes next in your source code. The Method Mapping Table. This method table is a simple array of Py. Method. Def structures. That structure looks something like this . This flag usually has a value of METH. This function is called by the Python interpreter when the module is loaded. It is required that the function be named init. Module, where Module is the name of the module. The initialization function needs to be exported from the library you will be building. The Python headers define Py. MODINIT. All you have to do is use it when defining the function. Your C initialization function generally has the following overall structure . Save above code in hello. We would see how to compile and install this module to be called from Python script. Building and Installing Extensions: The distutils package makes it very easy to distribute Python modules, both pure Python and extension modules, in a standard way. Modules are distributed in source form and built and installed via a setup script usually called setup. For the above module, you need to prepare following setup. This usually is not a problem on Windows. Importing Extensions. Once you installed your extension, you would be able to import and call that extension in your Python script as follows ! For example, following function, that accepts some number of parameters, would be defined like this . This is the object you will be parsing. The second argument is a format string describing the arguments as you expect them to appear. Each argument is represented by one or more characters in the format string as follows. Here format is a C string that describes mandatory and optional arguments. Here is a list of format codes for Py. Arg. Instead of passing in the addresses of the values you are building, you pass in the actual values. Here's an example showing how to implement an add function . The following arguments of Py. The Py. Object* result is a new reference. Following table lists the commonly used code strings, of which zero or more are joined into string format. Code. C type. Meaningcchar. A C char becomes a Python string of length 1. A C double becomes a Python float. A C float becomes a Python float. A C int becomes a Python int. A C long becomes a Python int. NPy. Object*Passes a Python object and steals a reference. OPy. Object*Passes a Python object and INCREFs it as normal. O& convert+void*Arbitrary conversionschar*C 0- terminated char* to Python string, or NULL to None. C char* and length to Python string, or NULL to None. Py. For example, Py.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
August 2017
Categories |