How to make scripts excutable? - python

How I transform my program into an executable (.exe)? It has 3 .py files and images.
Made with tkinter and python 3.7, I saw that people used py2exe or pyinstaller but I don't know how to do that.

I use py2exe but options like cx_freeze and pyinstaller are also out there. What goes for mac i beleive there exist a module called py2app ;)
to use py2exe you must first have python 3.4 since it hasnt't recently been updated. After that you need to make a file in which you write
from distutils.core import setup
import py2exe
setup(windows=["yourfile"])
then you need to run the file with the following command - i called the file setup.txt here.
py -3.4 setup.txt install
remember to write .py if you used a python file and then you just need one more step
py -3.4 setup.txt py2exe
and ofcause remeber you need to have py2exe installed. If you haven't before you do anything write in to the comandoprompt
pip install py2exe
The exe will then be found i the dist folder.
imported libraries are always triggy though i think i remember that last time i had to convert tkinter to .exe i had to use cx_freeze.

Related

How can I use Pyinstaller --onefile to successfully run my executable on a Windows system without Python?

I have a python file which contains a number of imported modules. Some of these modules are installed by pip. I am using Windows 10 and Python 3.8.0 and Pyinstaller 4.8 but I have also experimented with other versions of Pyinstaller.
I use pyinstaller --onefile file.py to create an executable. This file works fine in the Windows machine that has Python installed. However, when I attempt it in a Windows machine that does not have Python installed, the file starts then breaks (it ends without completing).
Is there something other than --onefile I am supposed to do to ensure that all modules are incorporated into the executable?
I have successfully converted other .py files into .exe and run them on the device without Python. The problem is, I think, the pip modules as the other .exe files did not require any pip install. Of course, this is pure speculation.
The imports are:
os
json
base64
sqlite3
shutil
from datetime import datetime, timedelta
And those that required pip:
win32crypt # (pypiwin32)
from Crypto.Cipher import AES # (pycryptodome)

Can't make a .py file into a .exe file

I'm somewhat new to python, so it would be nice if this was explained simply, if you can.
I'm trying to make a py file (main.py) into a exe file. I've followed this tutorial (https://www.geeksforgeeks.org/convert-python-script-to-exe-file/) as well as a youtube one, which did the same thing. My project has several modules. The conversion works, but if I try to open it, it tells me: (ModuleNotFoundError: No module named 'requests'). I would assume it does this for the rest of them if it gets the chance to. I don't know how to fix this and it would be nice if I could have a exe file so I could share programs with friends that don't have python installed. (I'm using pyinstaller for the process, same as the tutorial)
requests is a 3rd party library. I assume when you created the .exe, you did not "assembled" together your script with its dependencies.
According to the linked tutorial, you should check the pyinstaller's config and try to give pyinstaller the missing packages and somehow compile it with them, e.g.
For any missing package:
pyinstaller --hidden-import 'package_name' --onefile 'filename.py'
Just install requests:
pip install requests
Then you'll be able to bundle the script into an exe, provided there aren't any other missing dependencies.
According to your question either the requests module isn't installed or pyinstaller can't find the module!
If module isn't Installed then open up your terminal/Command Prompt and execute the follow command:
pip install requests
But if module is installed then what you can do is manually specify Pyinstaller which module to include also
so to compile it to exe use this command
pyinstaller --hidden-import 'requests' --onefile 'main.py'
Here the option --hidden-import means it will import the module requests also and --onefile means it you will get a single Exe file which will contain everything
Note! if you use --onefile this can effect your time taken to start your program!
Thanks
Edit:
According to the tutorial it shows you to open "PowerShell" but I recommend you to use "CMD" for this purpose!

Is there a workaround for using Openpyxl with Pyinstaller?

I am getting errors when trying to freeze a script that imports openpyxl - and no .exe is generated.
The same errors if the script contains only 'import openpyxl'.
I am currently using Python 3.7.1, Pyinstaller 3.6, and Openpyxl 2.5.12. I freeze the script by copying it into the Anaconda scripts folder and then using powershell to summon pyinstaller to freeze the script into a single .exe. I have successfully frozen some other scripts using this method.
Searching for answers online showed that openpyxl doesn't like being frozen, but a number of places seemed to say that they had managed to find workarounds.
I have tried a number of the solutions suggested, including placing the hook file in the directory and calling it out as a hidden import in the powershell window:
.\pyinstaller --hidden-import=openpyxl --onefile -w 'script.py'
However, nothing I have found has worked. My script is rather large and I would prefer to not have to re-write it using a different excel module. Is there a way I can freeze my script to allow others to use it without having to install python?
Try using cx_freeze it often gives less errors. Save the file names as given below and run the build.bat file
setup.py
import cx_Freeze
from cx_Freeze import *
setup (
name = "give_name",
options = {'build_exe':{'packages': ['package_name']}},
executables = [
Executable(
"give_name.py",
)
]
)
build.bat
py setup.py build

How can I use setuptools to create an exe launcher

I can install a python script as an exe file into "scripts" folder on windows.
How can I just create an exe launcher for a script without installing it?
Using 64-bit python, can I create an exe that will work with 32-bit python?
Instead of using setuptools, you can use py2exe to create your executable files.
Just read the tutorial page to understand how to create them.
Install it by typing in your terminal:
pip install py2exe
Create your .exe script (hello.py)
print "hello world!"
Create your setup config script (setup.py)
from distutils.core import setup
import py2exe
setup(console=['hello.py'])
Run your setup script by typing in your terminal
python setup.py py2exe
After that, 2 folders will be created inside the scripts folder.
They'll be called 'dist' and 'build' folder.
Inside the 'dist' folder you'll find an executable of your script.
In our example, we compiled the hello.py file, correct? So, inside the dist folder we should find a hello.exe file.
Q: Also, using 64-bit python, can I create an exe that will work with
32-bit python?
A: No! You'll have to install a 32-bit python and compile using the
32-bit version.
Hint: create a virtual machine to compile your 32-bit programs.
Also you can take a look at:
YT Tutorial
PythonCentral Tutorial
PythonTips Tutorial
StackOverflow Question

Do I need Python installed if I have PYC files?

I want to add Python as a scripting language to my game. If instead of distributing PY script, I distribute the PYC compiled files with my game, will the user still need Python installed, or will the DLL be sufficient?
Not only would they still need the Python interpreter, but the compiled python byte-code files are not guaranteed to work across different platforms.
You do need an executable to actually load the files into the VM. Fortunately it doesn't need to be very complex.
pyinstaller can be used to convert the .py file into an executable file
to install the pyinstaller
pip install pyinstaller
and to convert the .py file let's say file.py
pyinstaller file.py
this will make two new folders in the same directory
build and dist. dist folder contains all the required dll and file.exe to run the python code without python installed.

Categories