Sometimes we need to share the projects we create in Python. For sharing the recipient needs to have the same version of Python generally along with the modules which we have there for the project. This is a tedious job, and we have a couple of options for avoiding this. In the case of Python 2.7, Py2exe is a good choice, and in case of Python 3 we work with cx_freeze for data science projects.
CX_Freeze- Converting from .py to .exe:
It is a set of scripts and modules for freezing of Python scripts into executable. It is similar to py2exe and py2app. But unlike the two mentioned tools it is cross-platform and works on any platform on which Python can work. It is supported in Python 2.7 or higher.
We will install cx_Freeze using pip in the following manner.
Now we will parse ‘pstanalytics.com’ with regex and urllib.
A 14 second sleep is added at the end for seeing the output at the end before closing. We will save this file as “reandurllib.py.”
Now let’s create a file “setup.py”
We have imported the cx_Freeze setup and the executable and now we will call the setup function. The Python script’s path to be converted will be there as parameter.
- name: This us the name we want the executable to be.
- Version: It is a version number we want to give.
- description: This is the details we want to give. It is optional.
- executable: These are the things which we shall convert by using the executable function and the Python script’s path we want to convert as parameter.
We will now open up cmd.exe, or bash or what we have to navigate the directory which has the setup.py and the script which we want to convert. Then we will run the following code.
We will now have a build directory. We will find another directory, and within that, we will find the executable. In case all things are correct we will it will parse the search result based on the result of basic from pstanalytics.org and then it will display the text result in 15 seconds before it closes.