Creation of .exe file from Python file using PyInstaller

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
pyinstaller pyfilename.py
  • 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
pyinstaller pyfilename.py --name ProperFileName

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
pyinstaller 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
pyinstaller pyfilename.py --hiddenimport=requests

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.

pyinstaller pyfilename.py --exclude-module=pytest
Follow Us On

Registered Office

CHG IT CONSULTANCY PVT LTD

STPI Technology Incubation Centre,
2nd Floor, No.5, Rajiv Gandhi Salai,
Taramani, Chennai – 600113,
Tamil Nadu, INDIA

Parent Office

CIC Corporation

2-16-4 Dogenzaka, Shibuya-ku,
Nomura Real Estate,
Shibuya Dogenzaka Building,
Tokyo 150-0043, JAPAN

  +81 03-3496-1571
AboutUs

CHG IT Consultancy Pvt. Ltd. is a subsidiary of CIC Holdings Co. Ltd. Japan. Our company is focused on IT related solutions to reap the benefits of global popularity of Software Industry.

Registered Office
CHG IT CONSULTANCY PVT LTD

STPI Technology Incubation Centre,
2nd Floor, No.5, Rajiv Gandhi Salai,
Taramani, Chennai – 600113,
Tamil Nadu, INDIA

CIC Corporation

2-16-4 Dogenzaka, Shibuya-ku,
Nomura Real Estate,
Shibuya Dogenzaka Building,
Tokyo 150-0043, JAPAN

+81 03-3496-1571