-->

Welcome to our Coding with python Page!!! hier you find various code with PHP, Python, AI, Cyber, etc ... Electricity, Energy, Nuclear Power

Tuesday 3 July 2018

Python | How can I convert a .py to .exe for Python?

Steps to convert .py to .exe in Python 3.6
  1. Install Python 3.6.
  2. Install cx_Freeze, (open your command prompt and type pip install cx_Freeze.
  3. Install idna, (open your command prompt and type pip install idna.
  4. Write a .py program named myfirstprog.py.
  5. Create a new python file named setup.py on the current directory of your script.
  6. In the setup.py, code below and save it.
  7. With shift pressed right click on the same directory, so you are able to open a command prompt window.
  8. In the prompt, type python setup.py build
  9. If your script is error free, then there will be no problem on creating application.
  10. Check the newly created folder build. It has another folder in it. Within that folder you can find your application. Run it. Make yourself happy.
setup.py:
from cx_Freeze import setup, Executable

base = None    

executables = [Executable("myfirstprog.py", base=base)]

packages = ["idna"]
options = {
    'build_exe': {    
        'packages':packages,
    },    
}

setup(
    name = "<any name>",
    options = options,
    version = "<any number>",
    description = '<any description>',
    executables = executables
)
EDIT:
  • be sure that instead of myfirstprog.py you should put your .pyextension file name as created in step 4;
  • you should include each imported package in your .py into packages list (ex: packages = ["idna", "os","sys"])
  • any name, any number, any description in setup.py file should not remain the same, you should change it accordingly (ex:name = "<first_ever>", version = "0.11", description = '')
  • the imported packages must be installed before you start step 8.

The thing is, I suffered a lot to do this. Spent many days working on it. Finally, I got the answer. I realized that I got not only the answer but also an ImportError.  Then, I found a solution for that too and I made my python script to an application. Wanna know how? Follow the following steps and so you make people to follow you. Have a nice day ðŸ™‚

Steps to convert .py to .exe in Python 3.6

  1. You must be installed Python 3.6 and cx_Freeze.
  2. To install cx_Freeze, open your command prompt and type ‘pip install cx_Freeze
  3. make a .py program of yourself say ‘my first prog.py’ .
  4. Create a new python file named ‘setup.py’ on the current directory of your script.
  5. On the setup.py, code this and save it:
from cx_Freeze import setup, Executable
import sys

base = None

if sys.platform == 'win32':
    base = None


executables = [Executable("my first prog.py", base=base)]

packages = ["idna"]
options = {
    'build_exe': {

        'packages':packages,
    },

}

setup(
    name = "<any name>",
    options = options,
    version = "<any number>",
    description = '<any description>',
    executables = executables
)
  1. With shift pressed right click on the same directory, so you are able to open a command prompt window.
  2. Type in the prompt as >> python setup.py build
  3. If your script is error free, then there is no problem on creating application. Check the newly created folder ‘build‘. It has another folder in it. Within that folder you can able to find your application. Run it. Make yourself happy.
Wanna know your try and your success. Don’t forget to share your comments.

No comments:

Post a Comment

Thanks for your comments

Rank

seo