How to convert .py to .exe file using pyinstaller ?

r.aruna devi
Analytics Vidhya
Published in
3 min readMay 11, 2020
Image from Google.

What is an exe file ?

exe file is an executable file format. It contains a program and has the ability to run as a program in computer. It does not require any import statements to execute. Just Double Click is enough to run the exe file.

Hope, python is already installed in your system. Install PyInstaller using pip.

pip install pyinstaller

Let’s get Started.

  1. Create a python file. Open Command prompt and run python file to check whether the code runs successfully without any error.
  2. Create an exe file.
  • Go to the folder where the .py file is saved. Open the command prompt in that folder. Type pyinstaller testing.py

Sample Code :

import warnings
warnings.filterwarnings("ignore")
# It's always good to import warnings
import pandas as pd
print("****************************** Started Execution ******************************")
n1 = input("Enter Name : ")
print("Hello {0} " .format(n1))
print("****************************** Executed Successfully ******************************")
input( " Close to Exit " )

If the code runs successfully, you’ll see this in cmd.

* completed successfully

Now, Go to the folder where .py file is placed. You’ll see few folder created newly. dist -> testing -> testing

*** Open dist folder ***
*** Open testing folder ***
*** Double click testing app ***

Click the testing application. It will open a command prompt and execute the code.

pyinstall command might create lot of files inside dist folder. If you want to have a single application file.

Run pyinstaller — onefile testing.py

--onefile

Package your entire application into a single executable file.

# Build the .exe, .app
pyinstaller hello.py

# Create only a single file (slower to run, easier to distribute)
pyinstaller hello.py -F

# Tell application not to launch console (e.g. PyQt5, GTK+, tkinter GUIs)
pyinstaller hello.py --noconsole

# Specify the icon file to use
pyinstaller hello.py --icon=path/to/icon.ico
Image from Google.

ERRORS you might get while deploying:

  1. ImportError: Unable to import required dependencies: numpy: No module named ‘numpy.random.common’

Solution1 : Add the below code in python file

import numpy.random.common
import numpy.random.bounded_integers
import numpy.random.entropy

Solution2 : hide the module in the pyinstaller command.

pyinstaller — hidden-import=”numpy.random.common” — hidden-import=”numpy.random.bounded_integers” — hidden-import=”numpy.random.entropy” testing.py

2. Output prompt closes immediately.

Solution1 : Add input() at the end of the code. By adding, it will not close the prompt until you exit.

3. Error like no module named sklearn.tree, Pyinstaller ; ModuleNotFoundError: No module named ‘sklearn.utils._cython_blas’

Solution1 : Hide the module by pyinstaller -F --hidden-import="sklearn.utils._cython_blas" --hidden-import="sklearn.neighbors.typedefs" --hidden-import="sklearn.neighbors.quad_tree" testing.py

Solution2 : When you run pyinstaller command, It will create a testing.spec file.

  • Open testing.spec file
  • In the hiddenimports add all the imports.

hiddenimports=['cython','pymysql','pandas._libs.tslibs.timedeltas','sklearn.neighbors.typedefs','sklearn.utils.typedefs'],

Run. pyinstaller testing.spec

--

--