Python open a JAR shortcut - python

I have a JAR file saved on my desktop, is there a way that I ca get python to pen this up, as if I was just clicking on the link from my desk top, I am very new to python and know this is a dumb question, but would help me out alot.

Why do you want to execute it via Python?
In my Windows environment such .jar file is executed by Java:
C:\jre7\bin\javaw.exe -jar "%1" %*
Of course your OS should be able to change such association to open .jar with other program. On my Windows I can use local menu to open .jar file with Java, Eclipse, Firefox or I can chose other program.

I'll assume you're using Windows (it seems that way from how you've presented the question).
To simulate double-clicking a file on Windows, use os.startfile (available only on Windows):
os.startfile("path/to/myjarfile.jar")
This will do whatever your system is configured to do with files with a .jar extension.
For a more complete answer on starting files on Windows see this answer.

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.

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

How to write a script (for Windows XP) to run a python program?

Basically, I'd like to run a script (versus typing python program.py) or even have a shortcut that I could click on and start the program. Any ideas?
From python.org:
On Windows systems, there is no
notion of an “executable mode”. 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.
Also from python.org:
On Windows 2000, the standard Python
installer already associates the .py
extension with a file type
(Python.File) and gives that file type
an open command that runs the
interpreter (D:\Program
Files\Python\python.exe "%1" %*). This
is enough to make scripts executable
from the command prompt as ‘foo.py’.
If you’d rather be able to execute the
script by simple typing ‘foo’ with no
extension you need to add .py to the
PATHEXT environment variable.
Try using a batch file (.bat).
Type in whatever commands you want to execute in the proper order, in notepad, such as:
python program.py
Save the file as iHateTyping.bat
Open the command prompt using Run.
Go to the directory where you saved the file using cd.
Type in:
iHateTyping.bat
& you're done.
I encourage you to read more about batch files in the link highlighted above.
Press Ctrl+R, then type python.py to run your Python script.
Use py2exe to create a portable windows executable file.

Default save path for Python IDLE?

Does anyone know where or how to set the default path/directory on saving python scripts prior to running?
On a Mac it wants to save them in the top level ~/Documents directory. I would like to specify a real location. Any ideas?
On OS X, if you launch IDLE.app (by double-clicking or using open(1), for example), the default directory is hardwired to ~/Documents. If you want to change the default permanently, you'll need to edit the file idlemain.py within the IDLE.app application bundle; depending on which Python(s) you have installed, it will likely be in one of:
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPorts/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 3.x/IDLE.app/Contents/Resources
Edit the line:
os.chdir(os.path.expanduser('~/Documents'))
On the other hand, if you start IDLE from the command line, for example, with:
$ cd /some/directory
$ /usr/local/bin/idle
IDLE will use that current directory as the default.
I actually just discovered the easiest answer, if you use the shortcut link labeled "IDLE (Python GUI)". This is in Windows Vista, so I don't know if it'll work in other OS's.
1) Right-click "Properties".
2) Select "Shortcut" tab.
3) In "Start In", write file path (e.g. "C:\Users...").
Let me know if this works!
In Windows 10+, click the Windows Start button, then type idle, and then right-click on the IDLE desktop app and open the file location. This should bring you to the Start Menu shortcuts for Python, and you'll find a shortcut to IDLE there. Right-click on the IDLE shortcut and select properties. Set the "Start in" directory to be where you want default save path to be.
It seems like you can get idle into the directory you want if you run any module from that directory.
I had previously tried opening idlemain.py through the path browser. I was able to open and edit the file, but it seemed like I wasn't able to save my modifications.
I'm just glad to hear other people are having this problem. I just thought I was being stupid.
If you open a module, that sets the default working directory.
Start IDLE.
File -> Open to open your file. And set the current working directory.
In my case, the default directory is set to the directory from which I launched IDLE. For instance, if I launched IDLE from a directory called 'tmp' in my home directory, the default save path is set to ~/tmp. So start your IDLE like this:
~/tmp $ idle
[...]
On Windows (Vista at least, which is what I'm looking at here), shortcut icons on the desktop have a "Start in" field where you can set the directory used as the current working directory when the program starts. Changing that works for me. Anything like that on the Mac? (Starting in the desired directory from the command line works, too.)
For OS X:
Open a new finder window,then head over to applications.
Locate your Python application. (For my mac,it's Python 3.5)
Double click on it.
Right click on the IDLE icon,show package contents.
Then go into the contents folder,then resources.
Now,this is the important part:
(Note: You must be the administrator or have the administrator's password for the below to work)
Right click on the idlemain.py,Get Info.
Scroll all the way down. Make sure under the Sharing & Permissions tab,your "name"(Me) is on it with the privilege as Read & Write.
If not click on the lock symbol and unlock it.
Then add/edit yourself to have the Read & Write privilege.
Lastly,as per Ned Deily's instructions,edit the line:
os.chdir(os.path.expanduser('~/Documents'))
with your desired path and then save the changes.
Upon restarting the Python IDLE,you should find that your default Save as path to be the path you've indicated.
I am using windows 7 and by going to Start-> IDLE(Python 3.6 32-bit)
The click on properties and then in the shortcut tab go to
Start in and entering the desired path worked for me kindly note if IDLE is open and running while you do this you'll have to shut it down and restart it for this to work
If you locate the idlelib directory in your Python install, it will have a few files with the .def extension. config-main.def has instructions on where to put the custom config files. However, looking through these I did not find any configurable paths (your install may vary). Looks like you might need to crack open the editor code to alter it.
If you are using linux, you can create simple .sh file as presented below::
#!/bin/sh
cd /fullPath/PythonScripts/
idle
make the file executable by right click-> properties-> permissions-> check the execute as program checkbox-> done
Run the file :)

Categories