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.
this is my first time here, and I'm fairly new to python, so please let me know if you need more information. Thanks in advance.
I am running python 3 on Windows 7
I discovered my problem after I used pip install numpy. This works just fine. Then, when I try to use import numpy in the python shell I receive the ModuleNotFoundError: No module named 'numpy'. That's when I noticed that my default version of python was 3.6.1, despite having updated to 3.6.2 at some point. I still have both .exe setup files and when I run them it shows that I only have 3.6.2 installed. However, when I type python --version in the command line i get Python 3.6.1, even though Python36-32 is what i have in my path.
I think my question is how can I make sure I'm running the newer version of python as my default, or if need be, how can I get rid of the older version?
When you type python in cmd, it searches python command inside the directories in the environment variable named Path. Actually Path includes both python directory and python scripts directory. For example, in my computer, Path includes:
C:\Users\user\AppData\Local\Programs\Python\Python36
C:\Users\user\AppData\Local\Programs\Python\Python36\Scripts
pip is in the python scripts directory. Your Path can be wrong. You should check it. This link can help you. You should have one python directory and one python script directory in Path, just the version you need.
Also, you can call pip as a module:
python -m pip install numpy
This will install the package to the version which is in the Path, Python 3.6.2 in your situation.
If none of these works, I recommend you to uninstall(delete) Python 3.6.1, and try to use pip again. If pip doesn't work(or disappeares), you can read this or use get-pip.py to install pip to your computer again. Maybe, you can delete all python versions, and install the version you need, and of course, you should be careful about Path again.
EDIT:
I am not sure about your problem. Some informations are needed for a certain solution.
You can find the source of an executable(python or py in your situtation) with where command. Here is an example from my local:
where python
Output:
C:\Users\user\AppData\Local\Programs\Python\Python36-32\python.exe
That means C:\Users\user\AppData\Local\Programs\Python\Python36-32\ directory is in my Path and when type python, cmd runs python.exe.
So, you can find out what are py and python exactly.
I'm pretty new to the community, and new to python. I know the basics. But now I'm trying to download third party modules via pip but everything I do regarding pip displays an error. I know that pip comes with python seeing as the pip file is there.
I am running python 3.6 and windows 10. I have downloaded pip off the internet also but when I try to run the program it says already downloaded. The first time I ran it it said that it was downloading and successfully downloaded so I'm kind of lost.
Thanks!
Sounds like it's already properly installed. pip is a program called from a system terminal, not from a Python prompt. Open up a command prompt and type pip -h see what happens. If this does not print out the help page on how to use pip, and instead you get an error to the effect of:
"pip" is not recognized as an internal or external command, operable program or batch file
You then need to point Windows where to find the program (pip.exe) by adding your Python scripts folder C:\...\Python36-32\Scripts\ to your environment PATH variable. To do this, open the Start menu and search for "environment variables". Open the dialogue and find one named PATH (case doesn't matter). If it doesn't exist, create it. Edit the value and add the file path to the scripts folder to the end using ; as a separator.
As per your comment, if you are getting an access denied windows error this answer from another question may help you.
You need need to be sure either your powershell is being run as administrator, or by creating a virtual environment.
The Installing Packages docs have a great overview, and instructions.
https://packaging.python.org/installing/#use-pip-for-installing
I am using python 3.6 and nothing is working for me. I just can't install any of the tools for creating an executable. I tried cx_freeze, using pip had Visual C++ errors which I fixed and then it had more errors so I downloaded a wheel and that installed and in my Scripts folder there are three cxfreeze related files, none of which are batch files and when I run 'cxfreeze' in cmd it just says it is not recognised. For some reason python is installed in appdata/local/programs... but that is set in my path so I cannot see how that doesn't work. I tried pyinstaller with both pip and the download and nothing but errors about pypiwin32 which I can't get because of some other errors.
I do not know what is happening and I do not know what to do about all of this. Can someone just help me get a tool working to create an executable please. Is this because I am using python 3.6?
Try py2app (if you are on a mac) or py2exe (if you are on a windows). The setup is relatively easy.
https://pythonhosted.org/py2app/
http://www.py2exe.org/
I have a problem with the following command:
setup.py install.
I know it should work, I have tried it on a laptop but I don't have access to it at the moment. I need to complete a homework so I tried the same on my PC. And when I type the same command into cmd it just runs pyscripter as if I would use right click on setup.py and click edit with pyscripter. It does nothing else. I am sure that I am in the right folder in cmd.
My python version is 2.7 and my pyscripter version is v2.5.3. My OS is win7. I have tried to install other modules but I get the same response.
Has anyone encountered the same problem? I have searched the internet but I haven't found any answers to this problem.
Assuming that you installed Python 2.7 in the default folder i.e. c:\python27, then you can type:
c:\python27\python setup.py install
Wherever you installed it, you should append that path to your PATH variable (you'll need to log on as an admin to do that).
Do python setup.py install instead.
Windows is probably not set up to recognize .py files as executable.
Recently our class at school used all of the above programs. about a handful of students had trouble installing like you described. Fortunally I didnt not have this problen but I can suggest you use Administration priviledges.
Make sure you download the correct version.
Go to your download folder and look at the file you have downloaded (do this via my computer not from your web browser)
Right click on the file and then click run as an administrator
Here is an awesome site for windows binaries: http://www.lfd.uci.edu/~gohlke/pythonlibs/
If the library you need is there, just download and install like any other windows program...