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

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    psycopg2: insert multiple rows with one query

    12 answers

    NUMPYNUMPY

    How to convert Nonetype to int or string?

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Javascript Error: IPython is not defined in JupyterLab

    12 answers

    News


    Wiki

    Python OpenCV | cv2.putText () method

    numpy.arctan2 () in Python

    Python | os.path.realpath () method

    Python OpenCV | cv2.circle () method

    Python OpenCV cv2.cvtColor () method

    Python - Move item to the end of the list

    time.perf_counter () function in Python

    Check if one list is a subset of another in Python

    Python os.path.join () method