The reason why an executable file is convenient, because it can be installed and run in any other computer even if it doesn’t have python or any other dependencies installed.
Here we will be using PyInstaller library to carry out this conversion.
Installation Steps
- Install PyInstaller library using pip.
Article Content
pip install PyInstaller
Creating exe file
- Go to the address bar where the .py file for the project is located.
- Type cmd and a command prompt will open in that directory.
- Run
- Depending upon the total size of the project and the type of custom build after a few minutes, our .exe file will be created.
Custom Builds
- --name
This is a way to avoid your executable, spec file and build artefact folders being named after your entry-point script. --name is useful if we have a habit of naming our entry-point script something like pyfilename.py
- --onefile
The default option generates a directory containing dependencies along with an executable file, while the "--onefile" option simplifies distribution by producing a standalone executable file.
- --hidden import
This is one way to work around any code using import inside functions and import(). We can also use --hidden-import multiple times in the same command.
This option requires the name of the package that we want to include in our executable. For example, if our project imported the requests library inside of a function, then PyInstaller would not automatically include requests in our executable. So we use the command mentioned above.
- --exclude-module
This feature proves helpful in excluding developer-specific requirements such as testing frameworks from the final distribution. This is a great way to keep the artefact we give to the users, as small as possible. For example, if we are using pytest, we may want to exclude this from your executable.