Change language

Using CX_Freeze in Python

We have created many interesting projects and programs in Python. Perhaps the time will come when we create something very interesting and we want to share it. Typically, to share our Python program, the recipient will need to have the same version of Python installed, as well as all the modules used. Well, that can be quite tedious to demand. The interest in converting to .exe is high enough for distribution and there are several options. In Python 2.7 Py2exe — great choice, and for Python 3, cx_freeze works pretty well.

CX_Freeze | .py to .exe

cx_Freeze is a set of freeze scripts and modules Python scripts to executables, in much the same way as do py2exe and py2app . Unlike these two tools, cx_Freeze is cross-platform and should work on any platform that Python itself runs on. It supports Python 2.7 or higher (including Python 3). 
First we need to download cx_Freeze: download from here or install it using pip:

Once we have cx_freeze, we’re ready to start. We’ll parse python.engineering with regex and urllib.

import urllib.request

import urllib.parse

import re

import time

 

 

url = https://python.engineering/

values ​​ = { ’s’ : ’basics’ ,

’submit’ : ’search’ }

  

data = urllib. parse.urlencode (values)

data = data.encode ( ’utf-8’ ) # the data should be bytes

req = urllib.request .Request (url, data)

resp = urllib.request.urlopen (req)

respData = resp.read ()

  

paragraphs = re .findall (r ’"p" (. *?) "/ p"’ , str (respData))

 

for eachParagraph in paragraphs:

  print ( eachParagraph)

 

time.sleep ( 14 )

Output: we added a 14 second sleep mode at the end so that we can run the executable and see the output before closing it. So let’s save this file as "reandurllib.py". 

Now we create a second file called "setup.py"

from cx_Freeze import setup, Executable

 

setup (name = "GeeksforGeeks" ,

version = "0.1" ,

description = "",

executables = [Executable ( "reandurllib.py" )])

So, here we are importing from the installation and the executable cx_Freeze, then call the configuration function adding 4 parameters.

  1. name: this is the name we want our executable
  2. Version: just a version number to give it,
  3. description: all the details we want to provide (optional)
  4. Executable This is finally what we need to transform using the executable function and the path of the Python script to be converted as a parameter.

Then we open cmd.exe, or bash, or any other shell, go to the directory that contains the setup.py file and the conversion script and run:

 python setup.py build 

Now we are given a directory for assembly. Inside it we find another directory, and inside it — our executable! If everything went well, it should parse the search results basic with CX_Freeze

  • CX_Freeze Documentation
  • This article courtesy of Amartya Ranjan Saikia . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

    Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.

    Shop

    Gifts for programmers

    Best laptop for Excel

    $
    Gifts for programmers

    Best laptop for Solidworks

    $399+
    Gifts for programmers

    Best laptop for Roblox

    $399+
    Gifts for programmers

    Best laptop for development

    $499+
    Gifts for programmers

    Best laptop for Cricut Maker

    $299+
    Gifts for programmers

    Best laptop for hacking

    $890
    Gifts for programmers

    Best laptop for Machine Learning

    $699+
    Gifts for programmers

    Raspberry Pi robot kit

    $150

    Latest questions

    PythonStackOverflow

    Common xlabel/ylabel for matplotlib subplots

    1947 answers

    PythonStackOverflow

    Check if one list is a subset of another in Python

    1173 answers

    PythonStackOverflow

    How to specify multiple return types using type-hints

    1002 answers

    PythonStackOverflow

    Printing words vertically in Python

    909 answers

    PythonStackOverflow

    Python Extract words from a given string

    798 answers

    PythonStackOverflow

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    606 answers

    PythonStackOverflow

    Python os.path.join () method

    384 answers

    PythonStackOverflow

    Flake8: Ignore specific warning for entire file

    360 answers

    News


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    How to specify multiple return types using type-hints

    Printing words vertically in Python

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically