How to turn a UNIX executable into a desktop icon shortcut? - python

I have a python program that is executable and I can run it on my terminal by typing: ./python_file_name
This is fine for my use, but I want the program to be accessible to non-computer people. I would like to be able to create a desktop shortcut that runs the UNIX executable file. Currently when I double click the executable file it just opens the python program in a text editor. I want to be able to have the end-user double click the file so that it executes in the background.
Just for completeness, the script generates a time-sensitive-data-scraping csv file in the user's downloads folder. Therefore, there is no need to create an application with a GUI or anything fancy like that. Also, I am using Ubuntu and the end-user will be on Mac OS X.

I think you're looking for py2app. It will allow you to send your user a nice little application bundle.

Related

Running a file automatically on mac [duplicate]

I created a script that will tell me what to wear in the morning based on the weather (i.e. rain slicker if it will rain, heavy jacket if it will be cold, etc). I have fairly basic programming experience with python and the script works perfectly, but I want to be able to create a file that I can just double-click from my desktop and the script will automatically run.
My goal is to be able to simply double click [something] in the morning and it will automatically run the script and thus tell me what to wear. How could I go about doing this?
System Specifications:
python
Mac OSX
This worked for me on Snow Leopard:
-Put the python script on the desktop.
-Right click on the script file, and choose "Get info"
-Find "Open With", and choose "Python Launcher" from the dropdown box
Now double-clicking the script file will run the script in a new terminal window.
I'm not sure what versions of OS X come with the Python Launcher application. If you don't have that, you can solve it with a couple extra steps:
-Put the python script anywhere
-Create a shell script on the desktop with one line:
python "/Users/john/scripts/what-to-wear.py"
(Where I've assumed your script is called what-to-wear.py and is in /Users/john/scripts. Be aware that you do need to use an absolute path.)
-Make the shell script executable. In a terminal:
chmod 755 what-to-wear-shell-script
-Double clicking the shell script should run it in a terminal, running your python script.
What you want to do is create an executable file.
I've never used a Mac or Python, but look at this question and the first answer:
How can I create a directly-executable cross-platform GUI app using Python?
Seems http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html is what you're looking for
Use a batch file to make it automatic
Example :
1. Open Notepad -> type the following.
This one's for Windows..It might give you a hint
:start
C:\Python34\python.exe(your python file location)Your *.py file location.
:end
Save this with a *.bat extension
That's it ..you can configure more on this batch,I guess batch is the automation for day to day script
In Linux/unix based OS , add #!/usr/bin/python3 line on top of your script file with extension .py , if you have python version 3. Or change it to the version installed in the machine
Further , make the file executable by
sudo chmod +x <fileName>
for windows, add windows python path and make the file executable
You want the script to download the weather information online and output the clothes based on your predefined rules?
If this is the case, use urllib to download the page and do some ad hoc parsing over the downloaded html page to get the whether information. And write your logic using nested IF THEN ELSE blocks.

Run a Python program at start-up on Windows

I am developing a key-logger on Python (only for curiosity sake). And the script will be an executable. The process will not need a UI or user interaction.
Is there any way, even in another executable to make the key-logger start at start-up?
I don't use Windows, but you can try making a batch script that runs your python file and make that script Run a program automatically when Windows starts:
Click the Start button Picture of the Start button , click All
Programs, right-click the Startup folder, and then click Open.
Open the location that contains the item you want to create a shortcut
to.
Right-click the item, and then click Create Shortcut. The new
shortcut appears in the same location as the original item.
Drag the shortcut into the Startup folder.
As I said, I don't use Windows, so it might be totally wrong.
You can refer here for making the BAT file, which basically says:
#echo off
python c:\somescript.py %*
pause
I think that the above answers are too complex. What I did was just drag and drop or copy and paste my file in the startup folder by clicking the quick access toolbar, typing "startup", and the job is done.
I am using Windows 10 operating system, so it might be different in your case.
I hope this is useful.
Edit: The key to this solution is to have the .py extension files open by default by the python console (not a text editor), otherwise it will just open the file instead of executing it. In order to select the default program a type of file is opened with, right click on the .py file -> Open with -> Choose default program. See this example:
Use VBScript:
1-> create anyname.vbs with this data:
Set wvbs=CreateObject("Wscript.Shell")
wvbs.run "full location of your File",0
2-> copy anyname.vbs file in C:\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup this folder
Now when windows start it will run your file in hidden mode
Open run with "Start + R", then open "shell:startup". it opens you a folder(the folder that was mentioned before at start menu), and every file that is on this folder, well run at startup.
The folder path is: "C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" (you can copy it on windows explorer, or copy this path and put your account name on USERNAME)
this is the trick i used in my script:
from os import getcwd
from shutil import copy
copy(getcwd()+'/FILE_NAME.exe','C:/Users/USERNAME/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup')
They are some ways for file name as well, but im not familiar with it. this code copies it self to startup folder and starts each time windows boots
Create a shortcut in the shell:startup folder with an absolute path to your pythonw.exe executable. The w version of Python is needed so that it starts up without a shell in the background.
Detailed instructions
Open the startup folder
Type the Windows and R keys at the same time. In the Run dialog, type shell:startup and it enter. This takes you to the Startup folder. Shorcuts in this folder are launched when the computer starts. (For me, the startup folder is at C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.)
Create new shortcut
Right-click in this folder and say "New shortcut." Browse to "This PC," "Windows (C:)," "Program Files,", "Python39", and select pythonw.exe. Your Python install is likely in a different location. More recently, Python is found somewhere within %appdata%. (You can paste %appdata% into the run dialog to open this folder and look for Python. It opens in Roaming by default, but be sure to look in Local, too.)
As mentioned at the start of this answer, it is important that you select pythonw.exe rather than python.exe as pythonw.exe will run without opening a command prompt.
On the next "Create Shortcut" screen, titled, "What would you like to name the shortcut," you can name it whatever you want. For the OP, I recommend "Self penetration test."
Click "Finish."
Edit the shortcut to launch your script
In the Startup folder, right click on the app you just created and select "Properties."
In the Properties dialog's Shortcut tab, edit the "Target" to look something like "C:\Program Files\Python310\pythonw.exe" pentest_keylogger.py and the "Start in" to be the folder in which pentest_keylogger.py is found. (As above, you will need to use your Python distribution's path.)
Test your script
Hit OK. Then double-click on the icon to see if it works silently as you desire.
Finally, reboot your machine and use resmon (from the run dialog) or Ctl-Alt-Delete and the task manager to see if your app is running in the background.
Acknowledgements
Thanks to Eryk Sun and Behfar baghery for the core ideas presented here.

Create a desktop icon for a Tkinter script

I have written a python script with a Tkinter GUI. I would like to create a desktop icon that will execute this script so that the end-user (not myself) will be able to double-click the icon and have the GUI load, rather than 'run' the script from the terminal or python shell and then have to F5 from there.
Is there a way to do this? I have googled many arrangements of my question but most answers seem to be normal python scripts, not ones which are Tkinter based.
I am using a Raspberry Pi with Wheezy and Python 2.7
Thanks in advance.
I create executables(icons that I click to start the programs I write) using 'py2exe'. I use windows 7 and I am not sure if it would work for you with Raspberry Pi, but a google search may clear that up for you. I will provide the link below. Basically you end up with a folder with the executable(icon) and also some files, without which the executable won't work. It's unfortunate that you get this extra 'baggage', but it's still handy and the best solution I have come across. I don't think there is a much better way, but I am not 100% on that. Interestingly, I found that you could delete most of these baggage files and the executable would still work, it was trial and error, but it didn't take long. If I want to send the folder to someone, I zip it first.
py2exe is here
If you need a 64 bit binary you can get it here, along with, actually, pretty much every other version. get py2exe here also
Besides creating executable file other option is create simple .bat file:
Open notepad
Enter "C:\ProgramData\Anaconda3\python.exe" "C:\Users\Your ID\script.py"
First part is path to python.exe, second to your python script
save file as .bat file, ex. "open_program.bat"
Now simply double click on saved .bat file icon should open your script.
I like using cx-freeze to convert python scripts to exe.
There is very easy to follow documentation to do this. In short, you create a setup.py script that specifies what libraries and packages you want to include or exclude in your application.
You can then run the build in the console and it will create a build folder, in which will be an Application File. You can create a Desktop short cut using send to and selecting desktop.
Documentation link
You can save the script as a .pyw file so the user can click on the file and the GUi would open

Put Python/Tkinter into an application icon

I wrote a program in Python, then created a GUI using Tkinter. When I use programs on my computer (like Microsoft Word), I don't need to access the GUI from the command line I just click the application icon.
How do I put my program (the program itself is in the same .py file as the GUI) into an application icon that will start my program?
What you are referring to in Windows is a shortcut link.
The equivalent in *nix environments would be the ln -s command.
Links in computer systems are special files that point to a different file, often with parameters for easy access. See the Wikipedia Article on them for more information. Windows shortcuts are files with the extension .lnk, whereas soft links in Linux have the mode +l.
If I wanted to link to a file on Windows using Python, I would right click and create a shortcut. If your python installation is old (below 2.7.3), there is a little bug in the installation where the regkeys HKEY_CLASSES_ROOT\py_auto_file and HKEY_CLASSES_ROOT\.py are misconfigured and don't pass on command line arguments, so a fail-safe when passing arguments would be to edit the shortcut link and stick in something like the following:
"C:\Python27\python.exe" "C:\dev\path\to\file.py --args"
Windows can execute a program from a shortcut link or link files and directories directly (called symbolic links), whereas linux would link files or directories and then call that interpreter with a shebang (#!) or a direct command (./) in order to execute it. Windows just pretty much links the program to execute with the arguments after it.
You can also just double-click the file in explorer, but this doesn't supply arguments (if you need them).

How should I launch a Portable Python Tkinter application on Windows without ugliness?

I've written a simple GUI program in python using Tkinter. Let's call this program 'gui.py'. My users run 'gui.py' on Windows machines from a USB key using Portable Python; installing anything on the host machine is undesirable.
I'd like my users to run 'gui.py' by double-clicking an icon at the root of the USB key. My users don't care what python is, and they don't want to use a command prompt if they don't have to. I don't want them to have to care what drive letter the USB key is assigned. I'd like this to work on XP, Vista, and 7.
My first ugly solution was to create a shortcut in the root directory of the USB key, and set the "Target" property of the shortcut to something like "(root)\App\pythonw.exe (root)\App\gui.py", but I couldn't figure out how to do a relative path in a windows shortcut, and using an absolute path like "E:" seems fragile.
My next solution was to create a .bat script in the root directory of the USB key, something like this:
#echo off
set basepath=%~dp0
"%basepath%App\pythonw.exe" "%basepath%\App\gui.py"
This doesn't seem to care what drive letter the USB key is assigned, but it does leave a DOS window open while my program runs. Functional, but ugly.
Next I tried a .bat script like this:
#echo off
set basepath=%~dp0
start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py"
(See here for an explanation of the funny quoting)
Now, the DOS window briefly flashes on screen before my GUI opens. Less ugly! Still ugly.
How do real men deal with this problem? What's the least ugly way to start a python Tkinter GUI on a Windows machine from a USB stick?
EDIT:
All the answers below were very good (py2exe, pyinstaller, small .exe, .wsf script.) The .wsf solution was the simplest, so I'm using it for now. I'll probably end up switching to one of the other three solutions if I want a prettier icon and the standard .exe extension. Thanks, everyone!
This Windows Scripting Host script (file extension .wsf) can be used instead of the batch file:
<job>
<script language="VBScript">
set WshShell = WScript.CreateObject("WScript.Shell")
CMDFile = "App\\pythonw.exe App\\gui.py"
WshShell.Run CMDFile, 1
</script>
</job>
Update: Alternatively compile this C program and link an icon resource:
#include <windows.h>
#include <process.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return _spawnl(_P_NOWAIT, "App/pythonw.exe", " App/gui.py", lpCmdLine, NULL);
}
Update 2: To build an App.exe with icon, save the C code to app.c, create an Windows icon file app.ico, and save the following line to app.rc:
appicon ICON "app.ico"
Using Visual Studio 2008 Express, run these commands:
"C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
rc.exe app.rc
cl.exe app.c /FeApp.exe /link app.res
Alternatively use "Visual Studio 2010 Express" or "Microsoft Windows SDK v7.0 for Windows 7 and .NET Framework 3.5 Service Pack 1" and adjust the commands accordingly.
Note that the icon will only be used for the App.exe starter program, not your Python program.
Use pyinstaller to zip up your distribution (the advantage over py2exe is that it knows different third-party libraries and is generally more up-to-date).
You can then create a .exe for your users to click upon to start your application. If you just copy the results of the pyinstaller build onto your USB drive you should be fine.
Make it to a single executable using py2exe.
You could do this in a hacky manner by writing you're own little C application that calls system('start "" "%basepath%App\pythonw.exe" "%basepath%\App\gui.py"'). Next you compile it without console and use it as a "shortcut".
The Short Answer:
This question was asked a few years ago, but I recently found a solution for a program I was working on that may still be useful for others. With this method, you will be able to create a standalone exe program launcher that can be placed anywhere and refer to a file in its same folder or subdirectory, while having a pretty icon of your choice and no DOS screen popping up. In other words, a true good-looking relative-path transportable shortcut file :)
The solution should be easy to follow and do even for non-programmers and goes as follows:
open notepad
write: %windir%\system32\cmd.exe /c start "" "%CD%\optional
subfolder\mainpy2exeGUI.exe"
save as "whatever.bat"
convert the bat-file to an exe file using a program called "BAT to EXE converter"
while checking the "invisible application" option, and selection the
icon file you want under the "versioninformations" tab. You can name
the output exe file to whatever you want. Link to the converter
program can be found at
http://www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/
The converter program download contains a 32 and 64-bit version, use the 32-bit version to make the shortcut usable by both older and newer PCs.
(note, this solutions is almost the same as suggested at http://www.freewaregenius.com/how-to-create-shortcuts-with-a-relative-path-for-use-on-usb-drives/. However the current solution is different in terms of the code it uses in step2 which allows the launcher progam to be placed anywhere on a computer and not just on the top directory of a USB-stick, and is new to emphasize that the invisible option should be checked. Those differences are crucial.)
More Details (optional):
The original question was: "What's the least ugly way to start a python Tkinter GUI on a Windows machine from a USB stick?"
What was needed can be broken down to four things:
1. An exe program launcher.
2. That works on any computer and in any directory (i.e. it supports relative paths).
3. That has an icon.
4. That does not open an "ugly" DOS window.
There were several possible solutions suggested but none so far that satisfies all criteria. The original poster went for the ".wsf" option which allowed for relative paths and no ugly DOS window, but did not allow a custom icon or the recognizable exe file.
Part of the problem with the previously suggested solutions include:
you do not have C/VB programming skills or software.
you want an icon to your launcher program. Using a shortcut file that executes "cmd" and uses it to open your GUI file will allow you to set an icon file, BUT the icon file reference is absolute and will fail on any other computer than the one you created the shortcut file on.
you do not want the "ugly" DOS window flash. The cmd shortcut solution mentioned in the previous point creates a DOS window that flashes before opening your GUI.
Making the py2exe main executable file as the program launcher would almost be a perfect solution because it satisfies all criteria, but a backdraw with it is that the py2exe ececutable would require an ugly "tlc" folder to be placed in the same top-directory. It is therefore better to hide the main py2exe launcher in a nicely named subfolder. Also, there are many cases where one would like to keep the program launcher and the program itself as separate exe files, for instance if you are only using your main py2exe program to function as a python-runner that can launch open-ended editable python scripts that you can edit on the go without having to create a new py2exe file for each time you make a change to one of your scripts.
You can also fork Portable Python sources on GitHub and create shortcut in the same way other Portable Python shortcuts are created.
This gives you nice way to start app, icon, you can set custom registry/env variables if you need to, etc etc.
As an example you can take e.g. IDLE shortcut from Portable Python sources.
I've made a batch script (PyRunEXE) which compiles a simple Assembly Language code to make an EXE launcher for you:
https://github.com/SzieberthAdam/pyrunexe

Categories