DEV Community

Cover image for How to convert .py to .exe? Step by step guide.
Eshleron
Eshleron

Posted on • Edited on

Python to .exe How to convert .py to .exe? Step by step guide.

Auto PY to EXE

The only tool that we are gonna be using is Auto PY to EXE!

Auto PY to EXE is an amazing application for making .exe file out of your project whether it is one .py file or any number of them.
The application has a nice gui and looks like this:

alt text

How to start

Step 1. Installation

Installing using PyPI:

To install the application run this line in cmd:

pip install auto-py-to-exe

To open the application run this line in cmd:

auto-py-to-exe

Note: if you have any problems installing this way or you want to install it from GitHub go to the main page or watch this instructional video by the developer of "Auto PY to EXE" himself.

For more additional information use this

"Issues When Using auto-py-to-exe"

Step 2. Converting

There are few main options you need to choose:

  1. Pick your .py file
  2. Pick "One Directory" or "One File" option
  3. Pick additional files

1. Pick your .py file

If you have multiple files choose one that starts the program.

2.1. "One Directory" option

alt text

Pretty simple. When choosing "One Directory" option "Auto PY to EXE" will put all dependencies in one folder. You can choose Output directory in "Advanced" menu. If you have media files like icons and backgrounds you shouldn't have any problems using them inside your .exe if you place media files/folders in the Output directory.
Something like this:

alt text

2.2. "One File" option

alt text

When choosing "One File" option "Auto PY to EXE" will create one .exe file containing all dependencies but NOT MEDIA FILES. If your program has only default Windows gui with no icons, backgrounds, media files or you are OK with placing media folder near .exe file feel free to skip the following explanation. For those who want to pack media files into .exe file itself read paragraph 3.

3. Pick additional files

There is a menu in "Auto PY to EXE" called "Additional Files" that lets you add files of your choice. There is a catch though. "Auto PY to EXE" uses pyinstaller which unpacks the data into a temporary folder and stores this directory path in the _MEIPASS environment variable. Your project won't find necessary files because the path changed and it won't see the new path either. In other words, if option "One File" is chosen picked files in the "Additional Files" menu will not be added to .exe file. To work around this you should use this code provided by developer of Auto PY to EXE here

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
try:
    # PyInstaller creates a temp folder and stores path in _MEIPASS
    base_path = sys._MEIPASS
except Exception:
    base_path = os.path.abspath(".")

return os.path.join(base_path, relative_path)
Enter fullscreen mode Exit fullscreen mode

To use this code in your project replace the link to the media file you have now
For example:

setWindowIcon(QIcon('media\icons\logo.png'))
Enter fullscreen mode Exit fullscreen mode

with

setWindowIcon(QIcon(resource_path('logo.png'))
Enter fullscreen mode Exit fullscreen mode

Now the link will be referenced correctly and chosen files successfully packed into .exe file.

For comparison:
Possible link before

"C:\Users\User\PycharmProjects\media\icons\logo.png"
Enter fullscreen mode Exit fullscreen mode

Possible link after

"C:\Users\User\AppData\Local\Temp\\_MEI34121\logo.png"
Enter fullscreen mode Exit fullscreen mode

Press CONVERT .PY TO .EXE

alt text

Wait

alt text

Step 3. Run your program!

Now everything is done!

Run it. Test it. See what`s up.

Make sure everything works well.

You made One Directory

Every file you need should be in the single directory.

You made One File

This way you should have single .exe file. If you had a need and if done correctly your .exe file will be packed with all media inside it. You will not need any media files/folders present with .exe file for it to display them properly.


P.S.

If you have any feedback or suggestions on what important information should be added feel free to let me know!
This guide is not a description of every possible option done every possible way.
I hope you found that information useful!
Good luck with your projects!

Latest comments (104)

Collapse
 
guneet999 profile image
Guneet Singh

I want to convert complete Django project in exe, I did that using pyinstaller and dist containing the exe file I am running batch file, working fine, but transferring to another person, its not working.

Collapse
 
antojeffry profile image
antojeffry

Image description

hey i am facing this problem.What might be the fix

Collapse
 
eshleron profile image
Eshleron

Hi. Look into the google-cloud-core. It is used in someway in your code directly or as a dependency.

Collapse
 
bouazizaitd profile image
Bouaziz-aitd

I am fairly new to Python. I use spyder IDE and have crated miniconda3 environments. I have two environments (base) that came with the installation and one (GIS) I created. I installed additional packages in the latter as being related to my projects. I installed the auto-py-to-exe using pip as the conda did not recognize it. I compiled a simple plot program that uses matplotlib and when running the onefile exe I get an error that matplotlib is not defined.
I got the following in the output window from auto-py-to-exe :
Running auto-py-to-exe v2.32.0
Building directory: C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad
Provided command: pyinstaller --noconfirm --onefile --windowed "C:/Users/baitdriss/Desktop/Temporaire/Python Learning/test.py"
Recursion Limit is set to 5000
Executing: pyinstaller --noconfirm --onefile --windowed C:/Users/baitdriss/Desktop/Temporaire/Python Learning/test.py --distpath C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\application --workpath C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\build --specpath C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad

319792 INFO: PyInstaller: 5.9.0
319812 INFO: Python: 3.9.16 (conda)
319859 INFO: Platform: Windows-10-10.0.19044-SP0
319869 INFO: wrote C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\test.spec
319921 INFO: UPX is not available.
319962 INFO: Extending PYTHONPATH with paths
['C:\Users\baitdriss\Desktop\Temporaire\Python Learning']
321311 INFO: checking Analysis
321343 INFO: Building Analysis because Analysis-00.toc is non existent
321359 INFO: Initializing module dependency graph...
321411 INFO: Caching module graph hooks...
321498 INFO: Analyzing base_library.zip ...
328990 INFO: Loading module hook 'hook-encodings.py' from 'C:\Users\baitdriss\AppData\Roaming\Python\Python39\site-packages\PyInstaller\hooks'...
334212 INFO: Loading module hook 'hook-pickle.py' from 'C:\Users\baitdriss\AppData\Roaming\Python\Python39\site-packages\PyInstaller\hooks'...
336908 INFO: Loading module hook 'hook-heapq.py' from 'C:\Users\baitdriss\AppData\Roaming\Python\Python39\site-packages\PyInstaller\hooks'...
339809 INFO: Caching module dependency graph...
340211 INFO: running Analysis Analysis-00.toc
340333 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by C:\ProgramData\miniconda3\python.exe
342737 INFO: Analyzing C:\Users\baitdriss\Desktop\Temporaire\Python Learning\test.py
342760 INFO: Processing module hooks...
342841 INFO: Looking for ctypes DLLs
342857 INFO: Analyzing run-time hooks ...
342899 INFO: Looking for dynamic libraries
346010 INFO: Looking for eggs
346020 INFO: Using Python library C:\ProgramData\miniconda3\python39.dll
346028 INFO: Found binding redirects:
[]
346045 INFO: Warnings written to C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\build\test\warn-test.txt
346142 INFO: Graph cross-reference written to C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\build\test\xref-test.html
346311 INFO: checking PYZ
346326 INFO: Building PYZ because PYZ-00.toc is non existent
346334 INFO: Building PYZ (ZlibArchive) C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\build\test\PYZ-00.pyz
346998 INFO: Building PYZ (ZlibArchive) C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\build\test\PYZ-00.pyz completed successfully.
347049 INFO: checking PKG
347055 INFO: Building PKG because PKG-00.toc is non existent
347077 INFO: Building PKG (CArchive) test.pkg
354176 INFO: Building PKG (CArchive) test.pkg completed successfully.
354208 INFO: Bootloader C:\Users\baitdriss\AppData\Roaming\Python\Python39\site-packages\PyInstaller\bootloader\Windows-64bit-intel\runw.exe
354223 INFO: checking EXE
354255 INFO: Building EXE because EXE-00.toc is non existent
354286 INFO: Building EXE from EXE-00.toc
354297 INFO: Copying bootloader EXE to C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\application\test.exe.notanexecutable
354823 INFO: Copying icon to EXE
354872 INFO: Copying icons from ['C:\Users\baitdriss\AppData\Roaming\Python\Python39\site-packages\PyInstaller\bootloader\images\icon-windowed.ico']
354880 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
354910 INFO: Writing RT_ICON 1 resource with 3752 bytes
354920 INFO: Writing RT_ICON 2 resource with 2216 bytes
354934 INFO: Writing RT_ICON 3 resource with 1384 bytes
354949 INFO: Writing RT_ICON 4 resource with 38188 bytes
354959 INFO: Writing RT_ICON 5 resource with 9640 bytes
354981 INFO: Writing RT_ICON 6 resource with 4264 bytes
355013 INFO: Writing RT_ICON 7 resource with 1128 bytes
355212 INFO: Copying 0 resources to EXE
355222 INFO: Embedding manifest in EXE
355233 INFO: Updating manifest in C:\Users\BAITDR~1\AppData\Local\Temp\tmpoutj0wad\application\test.exe.notanexecutable
355248 INFO: Updating resource type 24 name 1 language 0
355444 INFO: Appending PKG archive to EXE
355498 INFO: Fixing EXE headers
356262 INFO: Building EXE from EXE-00.toc completed successfully.

Moving project to: C:\Users\baitdriss\Desktop\Temporaire\Python Learning\dist
Complete.

I got also this message on the CMD prompt:
(base) PS C:\WINDOWS\system32> auto-py-to-exe
1383 INFO: Extra DLL search directories (AddDllDirectory): []
1383 INFO: Extra DLL search directories (PATH): ['C:\ProgramData\miniconda3', 'C:\ProgramData\miniconda3\Library\mingw-w64\bin', 'C:\ProgramData\miniconda3\Library\usr\bin', 'C:\ProgramData\miniconda3\Library\bin', 'C:\ProgramData\miniconda3\Scripts', 'C:\ProgramData\miniconda3\bin', 'C:\ProgramData\miniconda3\condabin', 'C:\Program Files\Common Files\Oracle\Java\javapath', 'C:\ProgramData\Oracle\Java\javapath', 'C:\WINDOWS\system32', 'C:\WINDOWS', 'C:\WINDOWS\System32\Wbem', 'C:\WINDOWS\System32\WindowsPowerShell\v1.0', 'C:\WINDOWS\System32\OpenSSH', 'c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn', 'c:\Program Files\Microsoft SQL Server\100\Tools\Binn', 'c:\Program Files\Microsoft SQL Server\100\DTS\Binn', 'C:\Program Files (x86)\QuickTime\QTSystem', 'C:\Program Files\dotnet', 'C:\Program Files (x86)\Box\Box Edit', 'C:\Users\baitdriss\AppData\Roaming\Python\Python39\Scripts', '.']

Any help willl be highly appreciated.

Collapse
 
eshleron profile image
Eshleron

Hi! It is hard to say for sure what is the issue in your case since you have a number of environments. I would suggest looking into installing auto-py-to-exe not with pip in this case. But with something more easily recognizable by conda(depends if conda is set in PATH and as a default interpreter). If nothing that you do works, try using a separate IDE like PyCharm to test the project and also the auto-py-to-exe settings that you use to exclude that as a potential problem.

Collapse
 
khanalim profile image
khanalim

Hello, I have made exe file of a python script. My text editor is PyCharm. Exe file shows unix installer. Everything works on my Mac, but whenever, I transfer my exe files or folder to another pc or Mac, that computer does not recognize the exe file.

I am totally lost. Anyone has any suggestions.

Collapse
 
eshleron profile image
Eshleron

Hi! If you're using any paths to directories or files do check that. Also use try/except clause and run stuff in cmd/terminal to see the errors if you don't have a GUI.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
eshleron profile image
Eshleron

Hi! Check if your program has any threads that need closing.

Collapse
 
doniamariadenny profile image
doniamariadenny

after converting .py to .exe how do i send it to someone through gmail??
pls help

Collapse
 
aadarsh_af profile image
Aadarsh

Hello @eshleron First of all thanks for creating this wonderful GUI App which would help developers convert their scripts, which again propose a GUI App, into a user friendly app!
I've made so many apps using pyinstaller and cx_freeze whichever whenever needed. They're truly a blessing!
Now basically I've created a tkinter script which shows the user latest News and Weather updates using API_KEYS(one for news and the other for weather)... And now when I'm packaging it (by adding all the extra scripts and files it'd need) into an exe file using pyinstaller or cx_freeze... Both the exe files show errors related to the API_KEY of weather app while the script works perfectly fine! I'm attaching images of the errors from both packagers

Error from the Pyinstaller Exe file
The above image is of the error from the Pyinstaller Exe file

Error from the Cx_Freeze exe file
The above image is of the error from the Cx_Freeze Exe file (KeyError 'aadarsh' means that the name I gave to my api_key in the config file)

I have gone through most of the google advices which were from Github, StackOverflow, and many likely sites... I've added snippets to my requests library files and also in the pyinstaller hook folder as per the advices but no change could be found in every other exe file created.... (well the hook advice was helpful earlier when i worked with pydocx library and pyinstaller)

Also please tell me how to contact you by messaging... I need help regarding other programming stuffs too! This one is urgent though

Collapse
 
eshleron profile image
Eshleron

Hi!
Thank you for the kind words regarding the app. Unfortunately for me I am not the author but I completely share your excitement and gratitude towards one.

As for the errors. I think the path solution is worthy of trying first (you can find it in this article under "one file" section)

Try it and see where it leeds.

Collapse
 
how2code profile image
Info Comment hidden by post author - thread only accessible via permalink
How2Code

Well, 1st this isn't a .py into a .exe file, it's a .py into a .pyc so this tutorial is automaticly fake, but no matter is it a .exe or a .pyc file, it does work properly, so I don't really care.

Collapse
 
carolinatc profile image
CarolinaTC

Hi all!
Can anyone help me? I try to create the exe and its ok, but when i try open it, doesn't work...
Anyone have this issue?
Thanks

Collapse
 
eshleron profile image
Eshleron

Please provide snippets of code or a screenshot of an error message

Collapse
 
sanchit1219 profile image
sanchit Kedia

Hi, i am reading data from an excel using pandas in the code. How should i write the path for the excel such that when i send the "one dir" file to another user it doesn't give the error "no such file or directory".

Collapse
 
eshleron profile image
Eshleron

Hi!
I think you just have to make sure when you open the file in your app that the file is there. So you should send a copy to other user so the original stays there or you delete this one and create a new one with the same name.
I think if the file is not present in the directory when you try to open it it will trigger the exception, but otherwise it shouldn't.

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more