Converting Python scripts into executable programs - Windows
py2exe is a Python extension which converts Python scripts (.py) into Microsoft Windows executables (.exe). These executables can run on a system without Python installed.
Installing py2exe
For Windows systems, you can download a third-party tool called 'py2exe'.
- Run the Command Prompt.
- Change to the Python\Scripts directory
- Download py2exe using the built-in utility pip:
Converting a script into an executable
- For each script file that needs to be converted into an executable, a setup script file needs to be created. Assuming that test.py is to be converted into an executable, a setup.py file should be created with the following lines of code.
| from distutils.core import setup import py2exe setup(console=['test.py']) |
- Run this script from command-line
| python setup.py py2exe |
- Two directories will be created when you run your setup script, build and dist. The test.exe will be stored in dist directory.
| C:\test>cd dist C:\test\dist>test.exe |
Converting Python scripts into executable programs - Linux
Using chmod in Linux
- Open terminal window from Linux GUI.
The chmod command changes file attributes. Every file has read, write and execute attributes. Any attribute can be set or unset by + or – operator. To make test.py an executable use following command
| $ chmod +x test.py |
You no longer need to execute test.py with Python interpreter. Instead test executable script is run by following command
$./test
No comments:
Post a Comment