Pyinstaller isn't recognizable when trying to convert into .exe - python

I'm trying to convert my game that I created with pygame into a .exe file using pyinstaller. For some reason, it does not work. However, it says that I have it installed but it's not reconizable for some reason. Here are some images:
When I try to run it using: pyinstaller --onefile -w main.py
This image is when I try to uninstall and reinstall pyinstaller. It currently says that it's installed but when I try to use the command:
"pyinstaller --onefile -w main.py", it says it isn't recognized.
I'm really desperate... I've searched the ENTIRE internet for a solution but couldn't find one and this is my FIRST stack overflow post. Please help. Thanks in advance!

If pyinstaller is compatible with 3.9 (which it should be according to the documentation https://pypi.org/project/pyinstaller/#:~:text=Works%20out%2Dof%2Dthe%2D,libraries%2C%20thus%20ensuring%20full%20compatibility.&text=Libraries%20like%20PyQt5%2C%20PySide2%2C%20wxPython,or%20external%20data%20files%20manually), then make sure you're running the version of python that has pyinstaller on it. It looks like you're using windows which I don't think has a default python package (mac comes with 2.7 installed which is hella annoying). So if you do have other python versions installed, you need to specify which one you you want to run - in this case 3.9 because that's where. The command I used on mac (I think it should be the same on windows) is
/Library/Frameworks/Python.framework/Versions/3.8/bin/pyinstaller -m PyInstaller --onefile MyPythonProgram.py
That first portion is the location of the pyinstaller folder in your python installation so it will be different from mine but from the -m onwards it should be the same. To get the location of pyinstaller try the directory from your second picture.

Related

How can I to convert python code py to exe? [duplicate]

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.

Pyinstaller and Tesseract OCR

I am using Tesseract OCR for my program and I am going to convert it into a single .exe file using pyinstaller. The problem is that in order for Tesseract to work, I need to reference the path to the program installed on my computer, like this: pytesseract.pytesseract.tesseract_cmd = 'E:\\Tesseract-OCR\\tesseract'
Since this is not just a separate library that can be imported, but a standalone program, I can't pass it to pyinstaller as an '--add_data' argument. How do I make a one-file executable then?
Assuming you're on Windows, I ran into this problem and think I solved it by compiling a static version of tesseract (which does not need to be installed) and including its path as a binary in the pyinstaller spec file.
Official compiling instructions here:
https://tesseract-ocr.github.io/tessdoc/Compiling.html#windows
Install MS Visual Studio 15 (with c++) and vcpkg and execute one of the following through command prompt:
for 64-bit:
vcpkg install tesseract:x64-windows-static
for 32-bit:
vcpkg install tesseract:x86-windows-static
The tesseract executable will be located a few subfolders within the vcpkg folder on your PC. With that file, you also need to download a .trainneddata file and place it within a folder called 'tessdata' in the same directory with the tesseract exe.
Create a pyinstaller spec file and edit the Analysis(binaries=[]) section to include the folder path where tesseract is located (if you're not using a subfolder for tesseract I think you'd need to add both tesseract.exe and the tessdata subfolder). I also changed inclide_binaries=True
Run pyinstaller and include the option --specpath 'yourspecfile.spec'
I haven't yet attempted to try it on a different PC, so haven't fully tested that it works as intended (I don't know anything about compiling c++, there may be additional files/links needed for tesseract that are still intact since I've only been testing on the build PC)
#Zstr33's answer is correct, but it lacked detail. Following instructions have been tested on Windows 10 64-bit. Link to official compiling instructions here: https://tesseract-ocr.github.io/tessdoc/Compiling.html#windows.
Steps:
Install Visual Studio. Make sure to install the below items:
Then, click on individual components.
Then, select the following.
You can add whatever other components you want, but those are the ones that are needed to compile tesseract into a static binary. Also, if you don't use English, click on the language packs tab and add the English Language pack, this is needed for vcpkg.
Follow the quick start guide for installing vcpkg, found here: https://github.com/microsoft/vcpkg#getting-started.
Navigate to where you copied the vcpkg directory, or add it to path. Then run: vcpkg install tesseract:x64-windows-static for 64-bit, or vcpkg install tesseract:x86-windows-static for 32-bit.
Go to place where you put the tesseract directory\tesseract_x64-windows-static\tools\tesseract for 64-bit, and place where you put the tesseract directory\tesseract_x86-windows-static\tools\tesseract for 32-bit.
To use with pyinstaller, using --onefile.
I built my application exe using Tesseract and EasyOCR with the following command, hope this helps.
python -m PyInstaller --paths "fullpath-to-custom-libraries" --add-data "C:\Program Files\Tesseract-OCR;Tesseract-OCR" --collect-all easyocr --onedir -w main.py
I did get it to run with Pyinstaller after all.
First, I needed to create 2 Hook files as described here:
https://github.com/jbarlow83/OCRmyPDF/issues/659#issuecomment-714479684
Then, when running the exe, I still got an error missing pikepdf._cpphelpers
To solve that, just add
from pikepdf import _cpphelpers
in your python file as described here:
How to fix a pyinstaller 'no module named...' error when my script imports the modules pikepdf and pdfminer3?
My Pyinstaller call looks like that:
pyinstaller --onefile appname.py --paths="C:\python\anaconda3\envs\appname\Lib\site-packages" --additional-hooks-dir="C:\coding\appname\Hooks"
since bundling everything up with pyinstaller could be a real pain, I did the following steps:
Imported Pytesseract in my script
created the Exe file with pyinstaller (without defining anything in my spec file)
bundled Tesseract-Ocr installer and my script.exe with an external installer creator.
So the final user will have both the tesseract installer and tesseract.
With the external installer you have a lot of freedom and you can also play with the path variable.
I tried with pyinstaller and ocrmypdf forever and did not get it to work. I ended up using Nuitka. Worked right from the start :-)
Use sth. like:
python -m nuitka --mingw64 --standalone --follow-imports yourapp.py
http://nuitka.net/doc/user-manual.html
There was a similar answer here somewhere already, just could not find it anymore to link to it.

How to let others run your Python program without installing Python

I made my second program in Python. It's a program that calculates the roots of a quadratic equation. I think it's cool and I want to let my friends use it without having to let them install python.
I heard about Pyinstaller from a friend and I tried this method out: first I typed pip install pyinstaller in cmd. Then I changed directory to the folder that contains the file that I want to share with my friends (it's called vkv.py). Then I entered this command: pyinstaller vkv.py but I got this error: Indexerror: tuple index out of range. Apparently the problem was that I have Python 3.6.0 and Pyinstaller only works with versions up to Python 3.5.
So I had to try another method. Yesterday, I tried cx_Freeze and some other method that I forgot, but both of them failed. Cx_Freeze failed due to me having Python 3.6.0 (same as Pyinstaller) and I don't remember what went wrong with the other method.
My friend (who told me about Pyinstaller) told me to use virtualenv, so I looked up a tutorial on the matter. Looks like I needed to make a virtual environment where I use Python 3.5. So these are the commands that I typed in cmd:
pip install virtualenv
mkdir Environments
cd environments
virtualenv -p C:\Users\hp\AppData\Local\Programs\Python\Python35\python.exe py35_env (before entering this command, I installed Python 3.5.0)
C:\Users\hp\Environments\py35_env\Scripts\activate
Now that the environment has been made and activated, I installed Pyinstaller in this environment, with pip install pyinstaller. Then I changed directory to: C:\Users\hp\Desktop\Code\Python testing (which is where the vkv.py file is located at). Then I typed: pyinstaller vkv.py, but now I got a whole bunch of lines, with an error on the last line: ImportError: DLL load failed: %1 is not a valid Win32 application.. Here is a screenshot of it:
Being the curious person that I am, I wanted to know what would happen if I opened another cmd window and tried Pyinstaller again without the environment (so I basically tried the very first method again, listed above). It is strange that I got the same "ImportError" and not the "IndexError" from before.
So now my questions are (ranked from more important to less important):
what can I do to let my friends run the Python file without having to install Python?
What does this ImportError mean and how can I fix it?
What happened there with the last time that I tried pyinstaller vkv.py in cmd outside of the environment? Why did it give me an ImportError and not the IndexError, which is what I got when I first tried to run this command?
Sorry to make this a long post, but I like to give a lot of information because I'm afraid that I might leave something important out.
Thanks in advance for any kind of help!
As you want to use Python 3.6, you can't use Pyinstaller, py2exe, cx_Freeze or others. However, there is a tool called Transcrypt and it's compatible with Python 3.6. It can be installed with pip: pip install transcrypt, and converts Python code into JavaScript. To use it open the console and type transcrypt vkv.py.
It automatically generates a folder, __javascript__, and files on it. When transcript ends, you are ready to use it with html.
(Assuming the .html is in the same directory as the .py and the folder)
<html>
<head>
<title>Example</title>
</head>
<body>
<script src="./__javascript__/vkv.min.js"></script>
</body>
</html>
You can use the html as an executable (depending on your program, here is the documentation) by running it with your browser.
Try removing 3.6 and installing 3.5.3 from python.org.
Retry with Pyinstaller.
Try using py2exe, it's a python module.
Its really simple all you need to do is:
Download and install it http://sourceforge.net/projects/py2exe/files/
Create your setup.py
Run your setup.py
Here's a site that will explain it more in detail http://inventwithpython.com/appendixc.html
I am able to use pyinstaller in my Python 3.6 environment. You need to download the zip file for Development Release (unstable) and instead of using pip, run the setup.py file from downloaded pyinstaller code.

How to make a python executable when nothing is working?

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/

How do I run a Python 3.5 program that uses Tkinter on a computer without Python installed?

I have coded a program in Python 3.5 that uses the Tkinter import. I'm trying to figure out a way to run it on computers that don't have Python. First I tried freezing it but I haven't been able to because none of the freezing tools I found support Python 3.5. Then I tried possibly using a online idle but I couldn't find any that support Tkinter. I would prefer to be able to get a .exe file or something similar but if I could run it online that would be good too any ideas?
EDIT
So I have now successfully downloaded PyInstaller using pip. My current problem is when I type this into the console: pyinstaller.exe --onefile --windowed Finder.py
I get this error: 'pyinstaller.exe' is not recognized as an internal or external command,
operable program or batch file.
EDIT
I have now found the pathway to pyinstaller.exe. Now when I try to use it it says Access is denied.
I finally figured it out after about three days of work. Fist I downloaded PyInstaleller in the zipped form and extracted it. Then I put my program in the PyInstaller folder. Then I opened a regular command prompt. I then typed cd then the location of the PyInstaller folder. Finally I typed pyinstaller.py --one file --windowed program.py. Then when I went into the PyInstaller folder there was a folder called program with the .exe file in the dist folder. Thanks everyone for all of your help!
You can use pyinstaller to do that. I think its work fine on linux em linux.
Another option is use py2exe.
Try pyinstaller -F -w Finder.py as the command or you could check out CxFreeze.

Categories