How can I to convert python code py to exe? [duplicate] - python
I'm trying to convert a fairly simple Python program to an executable and couldn't find what I was looking for, so I have a few questions (I'm running Python 3.6):
The methods of doing this that I have found so far are as follows
downloading an old version of Python and using pyinstaller/py2exe
setting up a virtual environment in Python 3.6 that will allow me to do 1.
downloading a Python to C++ converter and using that.
Here is what I've tried/what problems I've run into.
I installed pyinstaller before the required download before it (pypi-something) so it did not work. After downloading the prerequisite file, pyinstaller still does not recognize it.
If I'm setting up a virtualenv in Python 2.7, do I actually need to have Python 2.7 installed?
similarly, the only python to C++ converters I see work only up until Python 3.5 - do I need to download and use this version if attempting this?
Steps to convert .py to .exe in Python 3.6
Install Python 3.6.
Install cx_Freeze, (open your command prompt and type pip install cx_Freeze.
Install idna, (open your command prompt and type pip install idna.
Write a .py program named myfirstprog.py.
Create a new python file named setup.py on the current directory of your script.
In the setup.py file, copy the code below and save it.
With shift pressed right click on the same directory, so you are able to open a command prompt window.
In the prompt, type python setup.py build
If your script is error free, then there will be no problem on creating application.
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.
See the original script in my blog.
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.
Python 3.6 is supported by PyInstaller.
Open a cmd window in your Python folder (open a command window and use cd or while holding shift, right click it on Windows Explorer and choose 'Open command window here'). Then just enter
pip install pyinstaller
And that's it.
The simplest way to use it is by entering on your command prompt
pyinstaller file_name.py
For more details on how to use it, take a look at this question.
There is an open source project called auto-py-to-exe on GitHub. Actually it also just uses PyInstaller internally but since it is has a simple GUI that controls PyInstaller it may be a comfortable alternative. It can also output a standalone file in contrast to other solutions. They also provide a video showing how to set it up.
GUI:
Output:
Alternatively use pyinstaller directly:
pip install pyinstaller
pyinstaller filename
I can't tell you what's best, but a tool I have used with success in the past was cx_Freeze. They recently updated (on Jan. 7, '17) to version 5.0.1 and it supports Python 3.6.
Here's the pypi
https://pypi.python.org/pypi/cx_Freeze
The documentation shows that there is more than one way to do it, depending on your needs.
http://cx-freeze.readthedocs.io/en/latest/overview.html
I have not tried it out yet, so I'm going to point to a post where the simple way of doing it was discussed. Some things may or may not have changed though.
How do I use cx_freeze?
Now you can convert it by using PyInstaller. It works with even Python 3.
Steps:
Fire up your PC
Open command prompt
Enter command pip install pyinstaller
When it is installed, use the command 'cd' to go to the working directory.
Run command pyinstaller <filename>
I've been using Nuitka and PyInstaller with my package, PySimpleGUI.
Nuitka
There were issues getting tkinter to compile with Nuikta. One of the project contributors developed a script that fixed the problem.
If you're not using tkinter it may "just work" for you. If you are using tkinter say so and I'll try to get the script and instructions published.
PyInstaller
I'm running 3.6 and PyInstaller is working great!
The command I use to create my exe file is:
pyinstaller -wF myfile.py
The -wF will create a single EXE file. Because all of my programs have a GUI and I do not want to command window to show, the -w option will hide the command window.
This is as close to getting what looks like a Winforms program to run that was written in Python.
[Update 20-Jul-2019]
There is PySimpleGUI GUI based solution that uses PyInstaller. It uses PySimpleGUI. It's called pysimplegui-exemaker and can be pip installed.
pip install PySimpleGUI-exemaker
To run it after installing:
python -m pysimplegui-exemaker.pysimplegui-exemaker
The best and easiest way is auto-py-to-exe for sure, and I have given all the steps and red flags below which will take you just 5 mins to get a final .exe file as you don't have to learn anything to use it.
1.) It may not work for python 3.9 on some devices I guess.
2.) While installing python, if you had selected 'add python 3.x to path', open command prompt from start menu and you will have to type pip install auto-py-to-exe to install it. You will have to press enter on command prompt to get the result of the line that you are typing.
3.) Once it is installed, on command prompt itself, you can simply type just auto-py-to-exe to open it. It will open a new window. It may take up to a minute the first time. Also, closing command prompt will close auto-py-to-exe also so don't close it till you have your .exe file ready.
4.) There will be buttons for everything you need to make a .exe file and the screenshot of it is shared below. Also, for the icon, you need a .ico file instead of an image so to convert it, you can use https://convertio.co/
5.) If your script uses external files, you can add them through auto-py-to-exe and in the script, you will have to do some changes to their path. First, you have to write import sys if not written already, second, you have to make a variable for eg, location=getattr(sys,"_MEIPASS",".")+"/", third, the location of example.png would be location+"/example.png" if it is not in any folder.
6.) If it is showing any error, it may probably be because of a module called setuptools not being at the latest version. To upgrade it to the latest version, on command prompt, you will have to write pip install --upgrade setuptools. Also, in the script, writing import setuptools may help. If the version of setuptools is more than 50.0.0 then everything should be fine.
7.) After all these steps, in auto-py-to-exe, when the conversion is complete, the .exe file will be in the folder that you would have chosen (by default, it is 'c:/users/name/output') or it would have been removed by your antivirus if you have one. Every antivirus has different methods to restore a file so just experiment if you don't know.
Here is how the simple GUI of auto-py-to-exe can be used to make a .exe file.
PyOxidizer can be an option here. It's pretty popular with 3.3k stars on Github. Its documentation says
PyOxidizer is capable of producing a single file executable - with a copy of Python and all its dependencies statically linked and all resources (like .pyc files) embedded in the executable. You can copy a single executable file to another machine and run a Python application contained within. It just works.
While I'm not sure if it is capable of producing .exe file PyOxidizer definitely helps with packaging and distribution.
Related
i want to compile .py to .exe
i want to compile .py to .exe. But when i do so the exe files is too big. Is there a solution?? i want the exe to be a smaller i know that python was not meant to be compiled, executables made from python are bound to be large , but the problem is i have learnt python like 3 years, and now i want to make apps/games for windows. i can't just install python on every device?? i want to make it small i learnt that there is something called executable-packing?? using tools like crinkler how can we do that with python with pyinstaller or cx_freeze
Python 3.6 is supported by PyInstaller. Open a cmd window in your Python folder (open a command window and use cd or while holding shift, right click it on Windows Explorer and choose 'Open command window here'). Then just enter pip install pyinstaller And that's it. The simplest way to use it is by entering on your command prompt pyinstaller file_name.py For more details on how to use it, take a look at link
How to share my Tkinter app with other users?
I have developed a tkinter application but I need other users (with Windows 10 OS) to use it. Some have a python interpreter installed but others don't. I tried to create an executable through py2exe and auto py-to-exe but none worked. I also tried to run the tkinter through pythonanywhere.com but it also didn't work. Is there a simply way to share the py files? Perhaps is there a click-to-run environment? What I don't need is to request users to install a full python application such as anaconda or WinPython, as this does not make sense for the users without a python interpreter.
I would use pyinstaller run cmd.exe as Administartor and type: pip install pyinstaller then run with: pyinstaller --onefile --noconsole --name your_script.py This creates a single file executable which also includes your Python with all dependencies of your script/project. If your projects contains external files like images/sounds you might need to edit the spec file as described here.
Full credit to the kid who made this video, it helped me out a lot when I had the same style of problem as you. https://www.youtube.com/watch?v=pzc4vYqbruk You'll just have to go through the generated .zip file to find the generated .exe file.
Unable to run python in CMD
I have recently started learning python using code academy and today I downloaded everything that I thought I would use. I downloaded Python and Atom. I have two separate drives on my computer. An SSD with not much storage and a hard drive with a lot of storage. My Windows is installed on the SSD, but I wanted to download python and atom on the hard drive, so I did so. When I installed Python I made sure to check add to PATH and the environmental variable thing. Now when I go to the command prompt, it shows "C:\Users\Gustavo>" but my python is installed on a different drive. Is there a way I could make this work? Thanks a lot.
There are two ways to install python: Download directly from the website Use a package manager Case 1: Download directly from the website Go to the python's website to download the version you would like to use. Install the downloaded file (During the installation you can customize the installation directory) Make sure to enable "Add python.exe to Path" After installation has been completed, open your command prompt and type where python. Your python directory should be printed. If all is good, then typing python should launch python in your command prompt. You can also run python by cd in the directory where your python is located and launch the .exe You have mentioned about changing path and environmental variables, and that's probably for the purpose of keeping multiple versions of python. If that's the case, there's actually a quick fix for this: Go to the folder where you installed Python. Copy the python.exe file, and rename that copy in the same directory as python3.exe (If you installed version 2, then rename as python2.exe). Now in command prompt type python2 or python3 and you should be able to launch either versions respectively. Note: If you face issues regarding paths, then you should detail the error messages. Case 2: Use a package manager Choose a package manger: chocolatey, scoop, and others. Check out these links for changing package manager's installation directory, installation method varies by the managers, so you should consult the developers should you experience problems: chocolatey scoop Package managers will manage the versions for you, if you choose to install multiple versions. You should refer to the package manager's website for detailed information. However, you can quickly check the installed version by typing python --version. The python version number should be printed back to you, same applies to python3. Double check your installation directory by which python Type python or python3 to run your python of choice. Lastly, you have mentioned atom. Atom is just a text editor: you can write python codes with it. When you are done editing, you can open the command prompt and navigate to where your code resides, and type python filename.py This will run your code directly from the command prompt. There are many atom plugins available to make this process seamlessly integrated within atom. iPython and Jupyter plugins are first things that comes to my mind, you should specifically check out Hydrogen.
How to make python portable?
I want to make a portable app that would have some code and python executable that would run on any Windows even if python is not installed. I would like it to be python 3.6 and so it has only pip and setup tools installed. EDIT: concerning duplicate not quite. I don't want to compile the code. I wanted to give them .py files but realize that Windows won't have python installed on default. I want something that can be carry on a flash drive but will run my code from source not binary.
Please correct me, if I understood it wrong. I think there are at least two ways to do it. suppose you have one portable_run.py script you want to run everywhere on a flashdisk. Make a exe file with pyinstaller for example. you can get a exe file like portable_run.exe. On target windows system what you need to do is to run the exe direcltly protable_run.exe Use a portable python distribution like winpython or python-xy. you just need to copy this portable distribution on the flash disk together with your portable_run.py. To run it on target system flashdisk/path-of-winpython/python portable_run.py Hopefully it could give you some idea.
I also encountered the same problem and managed to create a portable python with Python's official Windows embeddable package. I wrote the steps into a ps1 script so I can easily repeat the process without going through the pain. The steps: Download the portablepy.ps1 from the repo : https://github.com/Dreamsavior/portable-python-maker Create a blank folder, put the portablepy.ps1 to that folder. Execute the portablepy.ps1 The script will create a portable python 3.9.10 with pip in the current folder by default. To install custom version of Python run the script with -source and -destination parameter .\portablepy.ps1 -source "https://www.python.org/ftp/python/3.9.10/python-3.9.10-embed-amd64.zip" -destination "C:\SomeDir\PortablePython\" Where the -source is the url of the Python's Windows embeddable package from this page: https://www.python.org/downloads/windows/ And -destination is the path of your folder (ended with backslash).
How can I convert a .py to .exe for Python?
I'm trying to convert a fairly simple Python program to an executable and couldn't find what I was looking for, so I have a few questions (I'm running Python 3.6): The methods of doing this that I have found so far are as follows downloading an old version of Python and using pyinstaller/py2exe setting up a virtual environment in Python 3.6 that will allow me to do 1. downloading a Python to C++ converter and using that. Here is what I've tried/what problems I've run into. I installed pyinstaller before the required download before it (pypi-something) so it did not work. After downloading the prerequisite file, pyinstaller still does not recognize it. If I'm setting up a virtualenv in Python 2.7, do I actually need to have Python 2.7 installed? similarly, the only python to C++ converters I see work only up until Python 3.5 - do I need to download and use this version if attempting this?
Steps to convert .py to .exe in Python 3.6 Install Python 3.6. Install cx_Freeze, (open your command prompt and type pip install cx_Freeze. Install idna, (open your command prompt and type pip install idna. Write a .py program named myfirstprog.py. Create a new python file named setup.py on the current directory of your script. In the setup.py file, copy the code below and save it. With shift pressed right click on the same directory, so you are able to open a command prompt window. In the prompt, type python setup.py build If your script is error free, then there will be no problem on creating application. 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. See the original script in my blog. 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.
Python 3.6 is supported by PyInstaller. Open a cmd window in your Python folder (open a command window and use cd or while holding shift, right click it on Windows Explorer and choose 'Open command window here'). Then just enter pip install pyinstaller And that's it. The simplest way to use it is by entering on your command prompt pyinstaller file_name.py For more details on how to use it, take a look at this question.
There is an open source project called auto-py-to-exe on GitHub. Actually it also just uses PyInstaller internally but since it is has a simple GUI that controls PyInstaller it may be a comfortable alternative. It can also output a standalone file in contrast to other solutions. They also provide a video showing how to set it up. GUI: Output: Alternatively use pyinstaller directly: pip install pyinstaller pyinstaller filename
I can't tell you what's best, but a tool I have used with success in the past was cx_Freeze. They recently updated (on Jan. 7, '17) to version 5.0.1 and it supports Python 3.6. Here's the pypi https://pypi.python.org/pypi/cx_Freeze The documentation shows that there is more than one way to do it, depending on your needs. http://cx-freeze.readthedocs.io/en/latest/overview.html I have not tried it out yet, so I'm going to point to a post where the simple way of doing it was discussed. Some things may or may not have changed though. How do I use cx_freeze?
Now you can convert it by using PyInstaller. It works with even Python 3. Steps: Fire up your PC Open command prompt Enter command pip install pyinstaller When it is installed, use the command 'cd' to go to the working directory. Run command pyinstaller <filename>
I've been using Nuitka and PyInstaller with my package, PySimpleGUI. Nuitka There were issues getting tkinter to compile with Nuikta. One of the project contributors developed a script that fixed the problem. If you're not using tkinter it may "just work" for you. If you are using tkinter say so and I'll try to get the script and instructions published. PyInstaller I'm running 3.6 and PyInstaller is working great! The command I use to create my exe file is: pyinstaller -wF myfile.py The -wF will create a single EXE file. Because all of my programs have a GUI and I do not want to command window to show, the -w option will hide the command window. This is as close to getting what looks like a Winforms program to run that was written in Python. [Update 20-Jul-2019] There is PySimpleGUI GUI based solution that uses PyInstaller. It uses PySimpleGUI. It's called pysimplegui-exemaker and can be pip installed. pip install PySimpleGUI-exemaker To run it after installing: python -m pysimplegui-exemaker.pysimplegui-exemaker
The best and easiest way is auto-py-to-exe for sure, and I have given all the steps and red flags below which will take you just 5 mins to get a final .exe file as you don't have to learn anything to use it. 1.) It may not work for python 3.9 on some devices I guess. 2.) While installing python, if you had selected 'add python 3.x to path', open command prompt from start menu and you will have to type pip install auto-py-to-exe to install it. You will have to press enter on command prompt to get the result of the line that you are typing. 3.) Once it is installed, on command prompt itself, you can simply type just auto-py-to-exe to open it. It will open a new window. It may take up to a minute the first time. Also, closing command prompt will close auto-py-to-exe also so don't close it till you have your .exe file ready. 4.) There will be buttons for everything you need to make a .exe file and the screenshot of it is shared below. Also, for the icon, you need a .ico file instead of an image so to convert it, you can use https://convertio.co/ 5.) If your script uses external files, you can add them through auto-py-to-exe and in the script, you will have to do some changes to their path. First, you have to write import sys if not written already, second, you have to make a variable for eg, location=getattr(sys,"_MEIPASS",".")+"/", third, the location of example.png would be location+"/example.png" if it is not in any folder. 6.) If it is showing any error, it may probably be because of a module called setuptools not being at the latest version. To upgrade it to the latest version, on command prompt, you will have to write pip install --upgrade setuptools. Also, in the script, writing import setuptools may help. If the version of setuptools is more than 50.0.0 then everything should be fine. 7.) After all these steps, in auto-py-to-exe, when the conversion is complete, the .exe file will be in the folder that you would have chosen (by default, it is 'c:/users/name/output') or it would have been removed by your antivirus if you have one. Every antivirus has different methods to restore a file so just experiment if you don't know. Here is how the simple GUI of auto-py-to-exe can be used to make a .exe file.
PyOxidizer can be an option here. It's pretty popular with 3.3k stars on Github. Its documentation says PyOxidizer is capable of producing a single file executable - with a copy of Python and all its dependencies statically linked and all resources (like .pyc files) embedded in the executable. You can copy a single executable file to another machine and run a Python application contained within. It just works. While I'm not sure if it is capable of producing .exe file PyOxidizer definitely helps with packaging and distribution.