.py to .exe in Python 3.6.1

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.
  6. With shift pressed right click on the same directory, so you are able to open a command prompt window.
  7. Type in the prompt as >> python setup.py build
  8. 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.

Also take a look How to send notifications to your phone using python script. Code Sheet of Python is also available.

From,

Maria Irudaya Regilan J

Advertisements

60 comments

  1. Juan Velasco Cánovas · June 12, 2017

    Yes I have done it. But now How I can run it in another computer?

    Like

    • Maria Irudaya Regilan · June 12, 2017

      Can’t you run it in another computer by just double clicking it…?… Does it shows any problem?

      Like

  2. Maria Irudaya Regilan · June 12, 2017

    Thanks for your comment… Copy the whole build folder to another Computer and try it… If any issues occur, do inform us… 🙂

    Like

  3. Jon · June 24, 2017

    Hey, thanks for the tutorial, when i run python setup.py build i get the error “ImportError: No module named ‘inda’”. Any idea on a fix?

    Like

    • Maria Irudaya Regilan · June 24, 2017

      Thanks… There’s no chance of getting an error like what you have got. Because I have changed my setup.py file to fix that error. Have you download my setup.py?

      Like

    • Joe · June 28, 2017

      go to the Scripts folder and run “pip install idna”

      Liked by 1 person

  4. Joe · June 28, 2017

    I got the same “No module named ‘idna’…I also downloaded your setup.py

    Like

  5. Zhitao · June 30, 2017

    Hi, Maria,
    it’s good to see your blog. It’s very helpful.
    I got the built file under build>exe.win-amd64-3.6>main.exe, and python36.dll. But i can’t run it.
    This is the error infomation.
    PS D:\Python\SnakeGame> python setup.py build
    running build
    running build_exe
    creating directory build\exe.win-amd64-3.6
    copying C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\bases\Console.exe -> build\exe.win-amd64-3.6\main.exe
    copying C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\python36.dll -> build\exe.win-amd64-3.6\python36.dll
    Traceback (most recent call last):
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\site-packages\win32\lib\win32verstamp.py”, line 120, in stamp
    bits = [int(i) for i in ver.split(“.”)]
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\site-packages\win32\lib\win32verstamp.py”, line 120, in
    bits = [int(i) for i in ver.split(“.”)]
    ValueError: invalid literal for int() with base 10: ”
    During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
    File “setup.py”, line 26, in
    executables = executables
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\dist.py”, line 349, in setup
    distutils.core.setup(**attrs)
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\distutils\core.py”, line 148, in setup
    dist.run_commands()
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\distutils\dist.py”, line 955, in run_commands
    self.run_command(cmd)
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\distutils\dist.py”, line 974, in run_command
    cmd_obj.run()
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\distutils\command\build.py”, line 135, in run
    self.run_command(cmd_name)
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\distutils\cmd.py”, line 313, in run_command
    self.distribution.run_command(command)
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\distutils\dist.py”, line 974, in run_command
    cmd_obj.run()
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\dist.py”, line 219, in run
    freezer.Freeze()
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\freezer.py”, line 623, in Freeze
    self._FreezeExecutable(executable)
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\freezer.py”, line 225, in _FreezeExecutable
    self._AddVersionResource(exe)
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\freezer.py”, line 166, in _AddVersionResource
    stamp(fileName, versionInfo)
    File “C:\Users\Zhitao\AppData\Local\Programs\Python\Python36\lib\site-packages\win32\lib\win32verstamp.py”, line 123, in stamp
    raise ValueError(“–version must be a.b.c.d (all integers) – got %r” % ver)
    ValueError: –version must be a.b.c.d (all integers) – got ‘.0.0.0’
    PS D:\Python\SnakeGame> python setup.py build
    running build
    running build_exe
    thank you.
    Zhitao

    Like

    • Maria Irudaya Regilan · June 30, 2017

      Thanks for your support… We are working to post some best posts that will be useful for you. On dealing with errors we could see a ValueError. If your have both the int() error and ValueError then change your script by using some more exception handling blocks. This will sure helps you. I trust you have a snake game application soon.

      Like

      • maor · July 8, 2017

        hi, I got the same problem as Zhitao, please help, I also tried using pyinstaller and py2exe but none of them worked.

        Like

      • Maria Irudaya Regilan · July 8, 2017

        Use cx_freeze… Hope it will solve your problems

        Like

      • maor · July 9, 2017

        I did, I tried a lot of ways… and the cx_freeze way gave the same problem as Zhitao, please help.

        Like

      • Maria Irudaya Regilan · August 25, 2017

        In your setup.py change your ‘version’ value assignment as an integer. Contact us anytime at http://www.facebook.com/jrtinst/

        Like

    • Ben · August 23, 2017

      Have you changed the setup variables in setup.py? Chances are you still have the placeholder version of

      Like

      • Maria Irudaya Regilan · August 23, 2017

        No changes have done here. If you suspect anything change it as favour to you. 🙂

        Like

  6. Erik R Anderson · July 27, 2017

    I too got a similar error to Zhitao. Not sure this solution works as it appears to be same issue as py2exe and python 3.6

    Like

  7. yopourquoi · July 29, 2017

    thumbs up

    Like

  8. Ali · July 30, 2017

    Hey…many thanks for this useful tutorial… just 2 quick questions: Any suggestion on how to convert it to a single exe file? also, how to reduce the file size? thanks.

    Like

    • Maria Irudaya Regilan · July 30, 2017

      Thanks for your support… We can make a setup file for your exe file… And we are working on it… Soon, you can know that too… Stay connected with our blog..

      Like

  9. ILIJA · August 2, 2017

    Hi Maria,
    very good tool!!! All others I tried are not working, so yours is the best! 🙂
    One comment:
    In addition to install “cx_Freeze” I have to install “inda” too. This is not mentioned
    One question:
    Some tools give an option to use key for encryption of source codes. I expect in executable file we have interpreter and sources, right? So, is it possible to have this option?
    Best regards
    ILIJA

    Like

    • Maria Irudaya Regilan · August 2, 2017

      Thanks.. We appreciate your comment that improve us… Sorry, I am not sure about having the encryption… We work on it and we will sure post it… Do follow our blog for more posts..

      Like

  10. Hach · August 6, 2017

    Hi, I followed everything through and managed to make the .exe file of the Python program I had. In my original Python script I had imported the tweepy module which I had installed using pip. Now when I try to run the .exe file I get an error telling me that the tweepy module can’t be found. I reinstalled tweepy from command line using pip and I tried running the .exe file again and it still doesn’t work. It’s strange because when I run the Python file it works. Do you know how I can get it to run as the .exe file?

    Like

  11. Pingback: Tic Tac Toe game in Python 3.6 – Maria Irudaya Regilan J
  12. Jiafei Peng · August 28, 2017

    Very good described steps from Maria.

    To the issue of “ImportError: No module named ‘inda’”:
    2. To install cx_Freeze, open your command prompt and type ‘pip install cx_Freeze‘
    To install idna, in command prompt type “pip install -I –user pyopenssl”

    Like

  13. Peter · September 10, 2017

    Maybe a silly question, but how to make “pip install cx_Freeze” work on Windows console command? Let me just add that I’ve tried adding path to python script to system variables just to get info that ‘pip’ is not recognized as an internal or external command… And does so basic feature must be really so hard to turn on. What do you all guys do with python applications? Run in compilers only???

    Like

    • Maria Irudaya Regilan · September 10, 2017

      May be the directory where you have been installed your python didn’t connected to your environment variables. Copy the path of your python directory and make it as your environment variable. It helps. 🙂

      Like

  14. Leir · September 12, 2017

    hi, Maria,thanks for your tutorial. I followed your steps, I got the similar error to Zhitao, and I changed version value”” to “1.0.0.0”, so it is error free. But when I double clicked the exe, it flashed back. Do you know why?

    Like

    • Maria Irudaya Regilan · September 12, 2017

      Thanks a lot. Check your program. Because our exe just works as we coded.. if you wanna talk with me, catch me at @regilanj in Facebook messenger. Do follow our blog. 🙂

      Like

  15. Sva · September 28, 2017

    Thanks for the post. I was able to build an exe. When I run the exe, I get an error message
    “Import Error : cannot import name ‘_methods’”
    Please advise whats missing. Thanks

    Like

    • Maria Irudaya Regilan · October 1, 2017

      Add these things in your setup.py

      addtional_mods = [‘numpy.core._methods’, ‘numpy.lib.format’]
      setup(name=…, version=…, description=…, options = {‘build_exe’: {‘includes’: addtional_mods}}, executables = … )

      Convert it again and I hope this will help you. Thanks for your comment and don’t forget to follow the blog to get our new posts.

      Like

      • Sva · October 3, 2017

        It Worked. Thanks.

        Like

  16. José A · November 19

    O que fazer?
    Ao tentar:
    “python setup.py build” ===> “No module named ‘idna’

    Segui sua recomendação:

    “pip install -I -user pyopenssl” ===> “no such option: -u”

    E agora?

    Like

    • J Maria Irudaya Regilan · November 20

      Thanks for reaching us. Please post your doubts in English. It’s easy to give you a solution

      Like

      • J Maria Irudaya Regilan · November 21

        go to the Scripts folder and run “pip install idna”. else, To install idna, in command prompt type “pip install -I –user pyopenssl”. try it.

        Like

    • J Maria Irudaya Regilan · November 21

      Go to the scripts folder and run “pip install idna”
      Else, to install idna, in command prompt type “pip install -I -iser pyopenssl”.
      Try these. 🙂

      Like

  17. shekar · November 28

    when i tried to run the exe file it displays following error. what should i do?
    Fatal Python error: Py_Initialize: unable to load the file system codec
    Traceback (most recent call last):
    File “C:\Users\SHEKAR\AppData\Local\Programs\Python\Python36-32\lib\encodings\
    __init__.py”, line 31, in
    ModuleNotFoundError: No module named ‘codecs’

    Like

    • J Maria Irudaya Regilan · November 28

      Reinstall your python from source and reinstall your packages. Thank you for reaching us. FOLLOW our blog for more ideas.

      Like

    • J Maria Irudaya Regilan · December 1

      Reinstall your python, cx freeze and other major packages you have installed in your project. This may work. FOLLOW our blog for more ideas.

      Like

  18. ghada · January 27

    Hey, thanks for the tutorial, when I run python setup.py build I encounter this error :
    raise ValueError(“–version must be a.b.c.d (all integers) – got %r” % ver)
    ValueError: –version must be a.b.c.d (all integers) – got ‘.0.0.0’
    how can I fix it ?
    thank you ,,

    Like

    • J Maria Irudaya Regilan · January 27

      Thank you for reaching us. Give your version value as an integer not as alphabets.
      Follow our blog for more information.

      Like

      • Cris · July 26

        can u fix it ?

        Like

      • No! It is specified as default as an integer %d not as a string. So, we are to follow the rules. 🙂

        Like

  19. Tran · January 28

    I ran the codes and this error appears: “SyntaxError: unexpected EOF while parsing” with an arrow point right below the “.” in File “C:\Data Discrepency\setup.py”, line 27

    Any idea on how to fix. Thank you very much for helping out!

    Like

    • J Maria Irudaya Regilan · January 28

      Thank you for reaching us. You may have missed out any parentheses at the end of your script. If not, send us your script file we’ll fix you. Follow us for more details.

      Like

  20. Ng Kean Teong · February 12

    KeyError: ‘TCL_LIBRARY’, how to fix this? thank you in advance.

    Like

  21. Luis · February 23

    The build worked fine, however for some reason I have an issue where the application will not open. It quickly opens up a command prompt, however that closes almost immediately after. Is there something I am missing?

    Like

  22. robert · March 19

    hello, can you do an exe file so that it runs only on the program? without a console window?

    Like

    • thanks for reaching us! Actually it is the console window that runs the program you have coded. If you don’t want the console window, make your own graphics on the program using some of our pythonic libraries like Turtle. Follow us for more news!

      Like

      • Robert · March 23

        Hi, thanks for your answer. The console window does not bother me when the program starts. The problem is when there are two windows in the start bar. One is my program and the other is the console window.

        Like

  23. Jonathan · March 21

    I followed you directions and got this error when I run the file. I use pandas, but not specifically numpy. Any ideas? I imported numpy in my code, and it still happens (even though I don’t need to import numpy in my code for it to run)

    ——
    (bandwidth36) C:\GitHub\bandwidth\build\exe.win-amd64-3.6>find_numbers.exe
    Traceback (most recent call last):
    File “C:\Python\Anaconda3\envs\bandwidth36\lib\site-packages\cx_Freeze\initscripts\__startup__.py”, line 14, in run
    module.run()
    File “C:\Python\Anaconda3\envs\bandwidth36\lib\site-packages\cx_Freeze\initscripts\Console.py”, line 26, in run
    exec(code, m.__dict__)
    File “find_numbers.py”, line 4, in
    File “C:\Python\Anaconda3\envs\bandwidth36\lib\site-packages\pandas\__init__.py”, line 19, in
    “Missing required dependencies {0}”.format(missing_dependencies))
    ImportError: Missing required dependencies [‘numpy’]

    Like

    • Thanks for reaching us! Remove the numpy import line in your code since you have said that you are not using numpy in it. Else, use the command in your prompt window, “pip install numpy”. Follow us for more news!

      Like

  24. Robert · March 23

    Hello, I have another question. How to add another program to an already created program to make use of the same libraries?

    Like

  25. sakthi · April 26

    An .exe was build but it doesn’t run why?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s