How to create a executable file to a python program - python

I want to know if there are a way to create a executable file to a python program (in linux or windows).
Imagine a person that doesn't know how to open the terminal but want to use the program, I want to create a executable file which that person only have to click on it and the program opens.
thanks

I use the command
c:\pyinstaller\pyinstaller.py --onefile --console my_script.py
it works like a charm ;) (note only applies to windows) ... if theyre using linux they should be really comfortable in a terminal

Well, there are many possibilities to both do what you ask and to answer your question.
The simplest answer would be thus: On windows, with a Python interpreter installed, it registers the .py, .pyc and .pyo file extensions to be opened using the told Python interpreter and thus you get a free "double-click-to-run" behavior.
On Linux, a similar behavior can be achieved by telling the loader which interpreter to use in order to run the script by adding the following line as the 1st line in the file:
#!/usr/bin/python -O
and marking it as an executable using chmod +x myfile.py.
This will allow a user to run your file with a double-click from whatever file explorer he uses.
As for other possibilities, there are quite a few solutions, I'd recommend you to run a google search for it.
For Windows I'd recommend py2exe, and I'm not familiar with Linux solutions, and, anyway it was discussed on StackOverflow here
Best of luck!

Related

How to execute .py file with double-click

I just uninstalled and reinstalled python on my Windows machine. Before I uninstalled my previous version I was able to just double-click on a python script and it would open the command prompt, run the script, and close automatically. After re-installing with the newest version (3.9), I am no longer able to execute the script like that with a double-click.
Clearly I had done something special last time to set that up for myself, but I don't remember what it was. Any idea how I can get that double-click deal going again?
Doing the following should fix it:
Right click on the .py file you want to open;
Open with -> Choose default program -> More options;
Select the python.exe file.
Explanation:
Your Python scripts have to be processed by another program called the Python interpreter. The interpreter reads your script, compiles it into bytecodes, and then executes the bytecodes to run your program.
Installing a new version might have messed the path to the Python interpreter. The steps listed above will tell Windows to associate .py files with your Python interpreter, thus fixing the issue.
This link with Python on Windows FAQ might also be of help.
There will be an option of "Open With" after right-click on the file go and choose CMD. I hope it helps if not then sorry. Because I use Parrot OS
Save the following text to a file called something like python.reg (the .reg extension is important). You might need to modify the last line to be your exact path to python.exe!
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Python.File\shell\open]
[HKEY_CLASSES_ROOT\Python.File\shell\open\command]
#="\"C:\\Python2.7\\python.exe\" \"%1\" %*"
Find the python.reg file you just saved and double-click it to load those contents into your Registry. If you've performed one of the other operations in other answers (like "Open With"), those "UserChoice" settings are stored somewhere else in the Registry and will override the "Classes" setting shown in this script. So, do one or the other, don't combine them!

Making executable file on OSX from a python script

I need to make executable file on OSX/Mac. I have python3 script that uses excel file. Executable file also should be able to work just by double clicking the icon and also have python packages. I mainly work with Ubuntu18 but have access to Mac for few days.
What I've tried so far:
I've written a short bash script that activates python environment (with "source activate" command) and runs python script. Appified the script with this: https://gist.github.com/anmoljagetia/d37da67b9d408b35ac753ce51e420132
I know that terminal commands work but double-clicking the app in Mac does nothing.
With pyinstaller converted bash+python script to exe, then with wine tried converting that to executable program but that double-clicked program does nothing.
Tried py2app, but in the mac terminal it says "cannot execute binary file".
Does someone have any recommendations for my problem? As I've mentioned there are few main requirements:
works by double-clicking
works on mac
has all python packages
is able to read specific excel file (I will know the name of it, but just path may be confusing in some versions, because I would like to use relative path or something like [pwd]/file.xlsx)
Anyway, I'm having most problems with the first two points but don't want to forget the last two.
Thank You for help!

Getting custom files to launch like .exe in Windows

How does Windows know that an .exe application is an .exe application. Also, how would you tell windows to send this type of application to this program, that you have created. Like how Python programs are .py and are text files, but when you click on it, it acts like an executable. Or is it the other way around? I don't know. Any type of help would be useful, thanks.
In Windows this is different.
There is a variable called PATHEXT in the environment, like this
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY
Notice that I've added the .py extension to mark python scripts to be executable. That is the first step.
Then, you need to tell Windows which program will run this script. For that, you have the assoc and ftype commands.
When you type assoc you get the list of known file extensions. Here, I get .py=Python.File as output. Now you have to connect this extension to a program, ie. the python executable python.exe. Do this with ftype:
D:\\>ftype Python.File
Python.File="C:\Program Files (x86)\python27\python.exe" "%1" %*
I think that the correct calls to assoc and ftype are done when installing python. Then you only have to append the extension to PATHEXT.
A .py file doesn't execute when you write $ ./file.py unless you add #!/usr/bin/env python or C:\\Python27\python.exe something like that at the very beginning of it. The OS tries to execute the following: the_thing_you_wrote_after_# file.py If you change the extension to .lold it will work too.
If you try to do ./file.py without adding the line I was talking about earlier, the current shell'll try to parse the text and will definitely give errors.
I'm not quite sure, but Windows checks if it's possible to run a file by checking its extension first. It also searches for the entry point. If you rename your test.exe to test.test it won't work.
You can't launch a .py file like an app unless you use special tools like py2exe and cxfreeze.

Is there any way to make a Python program that has the user use the terminal (NO GUI) into a stand-alone for Mac?

I finally got py2app to work, and my program was made. However, it won't open because it relies on the terminal and raw_input. I just found out py2app is more for GUI interfaces.
All I want, is to turn the program into an application my users can click on, and it'll open in Terminal. Without them having to either install Python, or go to the terminal and type python "filename" (also, don't they have to set up the paths and everything to do that?).
Please help; I've been pulling my hair out all day looking for the answer. If this isn't possible, I'm just going to give them the .py file and instruct them to start it with python in the terminal and hope it's already set up so they can do that.
I know that on a mac you change the extension of the file to .command and that will make it so you can just click on it and it will run through the terminal if that's what it is specified to do. However I'm not sure if it will work if they do not actually have python installed.
Using PyInstaller http://www.pyinstaller.org/ it's possible to make an executable for different platforms. I believe OSX is supported.

How to convert python .py file into an executable file for use cross platform?

I've been searching through SO for a while now trying to come up with an answer to this but due to my inexperience with programming I don't understand much of the documentation, nor am I confident enough to experiment too much.
Would anyone be able to describe in slightly simpler terms how I would use programs like Py2exe, PyInstaller, cx_freeze etc.? I just want a way for others (mainly friends) to be able to run my (simple, text only) program without having to download python themselves. If there is an easier way to do this I'd appreciate knowing that too.
Running Vista 32bit, python 2.7
There are two distinct ways of freezing python scripts to create executables:
Packing the interpreter and *.pyc files into one exe file-container. Such an approach is used by tools like PyInstaller, Py2exe, cx_freeze.
Creating native code from Python source, usually using a middle step of converting Python-source to C or C++ code. This is done by such tools as Shed-skin and Nuitka. The problem of this aproach is that such tools do not always support all the functionality of Python (e.g. they can have some typing limitations and so on)
The point where you have to start is reading the documentation. Such tools are not just push-and-run style tools, they usually have some configuration that must be implemented (that's the problem of possibly all build systems, and as the project grows, the configuration and number of hooks also grows).
You can start with Py2exe tutorial and 'hello-world' to get acquainted with that how compilation is done. As far as I know it's a simplest way to get your goal.
And the last thing, you can't create cross-platform native executables as their file formats are strongly operating system and hardware dependent.
Download py2exe
Download this msvcp90.dll
Copy your FileCode.py AND msvcp90.dll to C:\Python27\
In C:\Python27\ create new text file, then enter this code inside it:
from distutils.core import setup
import py2exe
setup(console=['Avril.py'])
Replace Avril.py with YourFileName.py
Save the file as setup.txt
Open CMD and type this:
cd C:\Python27\
python setup.txt py2exe
Now go to C:\Python27\dist\ and there's your .exe program.
Source: Manvir Singh
Python scripts can be made directly executable, like shell scripts, by putting the python environment path in the top of the script file.
#!/usr/bin/env python3.5
The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.
Detailed description also for linux is here.
Install pyinstaller, a program that converts .py to .exe for python 2.7 to where python is located:
cd C:\python27\scripts
pip install pyinstaller
then move whatever python file you want to compile to C:\python27\scripts, compile from there by using:
pyinstaller --onefile yourfile.py
the --onefile is optional but it packages the whole thing(in this example yourfile.py) into one .exe. Once everything is done there will be 2 new folders along with a .spec file. From C:\python27\scripts open the folder dist. Your .exe will be located there in one file which you can double tap to execute and distribute to anyone who doesn't have python. Hope it helps.

Categories

Resources