Using CX_Freeze in Python

PythonProgrammingServer Side Programming

Sometimes we feel to create something different which is very exciting, and according to human nature, we always love to share it.

Python also fulfills those wishes. Using Python, if we want to share our Python program with our friends we can do that, only need to have the same version of Python installed with all the modules those are used in program of their machine.

First we need to install CX_Freeze module using pip install CX_Frezze command in command prompt.

Pip Install

First step is to solve this assignment, a python program conversion. We need standard library modules, here we use urllib and re module where we parsed python.com.

Example

import urllib.request
import urllib.parse
import re
import time
my_url = 'https://www.python.com/'
my_values = {'s' : 'basics',
          'submit' : 'search'}
my_data = urllib.parse.urlencode(my_values)
my_data = data.encode('utf-8')
my_req = urllib.request.Request(my_url, my_data)
my_resp = urllib.request.urlopen(my_req)
my_respData = my_resp.read()
my_paragraphs = re.findall(r'<p>(.*?)</p>',str(my_respData))
for p in my_paragraphs:
   print(p)
time.sleep(20)

Here we use 20 second sleep at the end, so we can see the output before it closes.

This program is saved as "pythonCX_Freeze.py" file name.

Now next step is to create setup file and this file is called "setup.py"

Example Code

from cx_Freeze import setup, Executable
setup(name = "pythonCX_Freeze" ,
   version = "0.1" ,
   description = "" ,
   executables = [Executable("pythonCX_Freeze.py")])

In this program we importing pythonCX_Freeze setup and executable. Next we call setup() function with 4 parameters. First parameter name, this is the name of the program that we want to execute, second is version, this is the version number to give it, description if we want then we write necessary line otherwise blank it and last one is executable function with one parameter.

Next we open cmd.exe then navigate the directory where has the setup.py and the script.

Then we run python setup.py build.

Now given a build directory and within this directory we find another directory and within that directory we find our executable. If every step is done correctly then it should parse the search result of basic form on python.com and display result for 20 seconds before closing.

raja
Published on 20-Nov-2018 08:33:38

Advertisements