How to convert .py to .exe? Step by step guide.
Tags: python, beginners, learning
Published: 2019-01-24 11:54:53

---
title: How to convert .py to .exe? Step by step guide.
published: true
cover_image: https://thepracticaldev.s3.amazonaws.com/i/3c27e6ni9a7preeeqo0v.jpg
description: Instruction on how to convert .py to .exe
tags: python, beginners, oop, learning
---

# 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] (https://warehouse-camo.cmh1.psfhosted.org/4fc81b16448c94eda8a266110b56284ca9883185/68747470733a2f2f692e696d6775722e636f6d2f6464304c43326e2e706e67)

## 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] (https://pypi.org/project/auto-py-to-exe) or watch this instructional video by the developer of "Auto PY to EXE" [himself] (https://github.com/brentvollebregt).

{% youtube OZSZHmWSOeM %}
### For more additional information use this
####["Issues When Using auto-py-to-exe"] (https://nitratine.net/blog/post/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] (https://thepracticaldev.s3.amazonaws.com/i/9mrf0i0cm0shdgoizjdz.JPG)

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] (https://thepracticaldev.s3.amazonaws.com/i/1a8b99f7f5gggq7fe7cv.JPG)

## 2.2. "One File" option
![alt text] (https://thepracticaldev.s3.amazonaws.com/i/cuq5tm4xsjnngoi6uqmc.JPG)

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] (https://nitratine.net/blog/post/issues-when-using-auto-py-to-exe/#debugging) 


    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)
    
    
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'))

*with*

    setWindowIcon(QIcon(resource_path('logo.png'))

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"

Possible link after

    "C:\Users\User\AppData\Local\Temp\\_MEI34121\logo.png"
Press **CONVERT .PY TO .EXE**

![alt text] (https://warehouse-camo.cmh1.psfhosted.org/d2f89e7dfcbbd3635e0f098b43dbc9df7c74b7a4/68747470733a2f2f692e696d6775722e636f6d2f663354456e5a492e706e67)

Wait

![alt text] (https://warehouse-camo.cmh1.psfhosted.org/a71bb45213b2285ac68eedc2994709c478deb12d/68747470733a2f2f692e696d6775722e636f6d2f4d6a644f4e63432e706e67)


## 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!