How to auto-run a script - python

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.

Related

How do force or change my terminal to run python executable file in python 3 just by opening the file?

My terminal is running python 2 so when I run my python file the program fails.
So basically I am new to programming and trying to create a small python script to help me auto create folders. I also want to give the script to colleges so that they can use it on their systems too.
I know that I can run my file in terminal by using "python3 myfile.py" and it will work, but that's too much off a mission to do for my colleges and as my colleges are not familiar with code or terminal for that matter, I wanted to create an executable file so that they just click to open type a few answers to the promoted question and boom folders created.
This is where I run into a problem, I have "#!/usr/bin/env python3" at the top of my file but when I run the script IDLE opens up and it just shows the code I have written but doesn't seem to run the actual script I wrote. Am I doing something wrong?
I also then though perhaps I could just use the terminal to run the file as it is now executable, so I go into terminal and enter "myfile.py" and the program runs but in python 2 so my script fails as it is in python3. So another question would be is there a way to code into my python file, when running this file make sure you use python3? as I would want this to work on all colleges system without them having to write out anything in terminal?
Sorry for the long explanation but any advice would be greatly appreciated.
Thank you in advance
When you are on windows you can just create a .bat file where you write: python3 myfile.py in it.
Both files have to be in the same directory.
If you want to have a .exe you can also use py2exe.
You could also try just #!/usr/bin/python3 without env.
More here.

How to run python script without writing command on cmd again and again

I am practicing python code and executing it through command line. each time i have to execute my python file i go to cmd and write
D:\PythonPractice>python myClass.py
Is there any standard and comfortable way available to execute python code quickly?
If you are executing the same command, with no changes to arguments or anything, you can pack it in a .bat file (windows executable)
Name it something like myscript.bat
D:\PythonPractice>python C:\path\to\myClass.py
Put full path inside it. Now double click will do.
Use PyCharm Software by JetBrains (same company who developed Android studio and Kotlin language), it will help you in many ways .
Run Python with single press of a button.
Add modules easily just with some clicks.
Debugging the code as smooth as possible.
It is Awesome, I am using it for past couple of months.
you can change file association which controls what to do when invoking filename in command line. For instance, when you just type text filename in cmd, notepadd will be opened, for word docs Word or other document editor.
Have a look into following link from google or you can fiddle by yourself starting from Control Panel > Control Panel Home > Default Programs > Set Associations. Select a file type in the list and click Change Program.
Alternatively,you can use any of Python IDE (PyCharm,PyDev etc.) so you will be able to run directly scripts from editor.
With Notepad++, you can also create a shortcut to be able to launch your python script :
Menu Run > Run (or press F5)
Choose the python .exe (default in C:\Program Files (x86)\Python36-32\python.exe
Then add this code next to the path : "$(FULL_CURRENT_PATH)"
This will allow to execute the current file
example of command with Python 3.6 :
"C:\Program Files (x86)\Python36-32\python.exe" "$(FULL_CURRENT_PATH)"
Then save the command, by assigning a shorcut key, it's better with a modifier like SHIFT (SHIFT + F10)
Now you can launch any script with this shortcut.

How to start a python script (with Parameters) on another location from desktop

We are using Ubuntu for OpenERP development, everyday at morning I have to browse to openERP directory from terminal, start the "openerp-server" python file with lots of parameters, and then keep the terminal window open.
Is there a way that I can double click a python file on desktop which will start the following file with its parameters from its own directory ?
The directory I use right now to start the program is "home/username/Projects/openerp_7_0/server/"
after navigating to this path in terminal I enter:
"python openerp-server --addons-path=../addons "
So how can I create a shortcut for this on desktop.
Rather than “double clicking a python file on desktop” you could put a shell script on the desktop (and single click to start, not double click), somewhat as follows.
#!/bin/sh
cd $HOME/Projects/openerp_7_0/server/
/usr/bin/xterm -e 'python openerp-server --addons-path=../addons; /bin/bash' &
(Alternately, say /usr/bin/gnome-terminal instead of /usr/bin/xterm.) The ; /bin/bash part may be unnecessary if the openerp-server runs indefinitely. Include the ; /bin/bash part if you want the terminal to remain (and display any output) when openerp-server finishes; don't include it if you want the terminal to go away when openerp-server finishes.
Although I would recommend trying to import the python file and then running it that way, I get the feeling that in this case you can't.
So you could try something like this.
import os
os.system('python /home/username/Projects/openerp_7_0/server/openerp-server --addons-path=../addons')

Opening a file, and taking input from the file opened in a Python program

I have written a program in python that is another front end for un-tarring files on OSX. I want to be able to double click on the file and open with the program that I wrote. How do I make the program automatically know that it is being opened with that file, and to work on that file?
You will need to make your make your script executable with the command chmod a+x <your script.py>
You will also need to tell your OS that the python interpreter is needed to execute this file. In Linux the line #!/usr/bin/env python at the top of the file is what does this. I assume its the same in OSX.
Then right click and select "Open with" and then "Other...". And select your script.
The Python script will need to be configured correctly to run when the OS calls it in this way.
Another way to do it(Caveat: I haven't personally tried this) is to use a shell script as advised in this anwer on the superuser forums.
Python scripts on OS X are not by default recognized as launchable applications by the Finder. What you need to do is use a tool that will package your Python script as a Mac OS X application. Then, you can right-click or ctrl-click a file of your chosen file type and locate that application through the Open With... dialog.
One option I'm aware of for doing this is part of the MacPython package, called py2app:
http://wiki.python.org/moin/MacPython/py2app
So, your steps would be:
1) Use py2app to package your Python script as a launchable application
2) Associate the application with the file type you'd like it to open using by right-clicking or ctrl-clicking one of that file type and choosing "Open With..." in the OS X Finder.
import sys
print("Program.py executing...")
for x in sys.argv:
print(x)
Produces the output
Program.py executing...
Program.py
something.tar
I'm windows, running this command:
python Program.py something.tar
I don't think anything changes from that command to OSX.
And as said by benjamin, the OS is responsible for determining what program is used for the file. I don't know what the command line from your os will be, but you can find out with the above program.

Easy way to launch Python scripts with the mouse in OS-X

I'd like to write cross platform Python scripts that are GUI frontends for command line programs. The problem is I know a few Mac users who think that using the terminal will have the same effect as throwing their computer off the top of a skyscraper. In Linux and Windows it's easy enough to setup a Python script so the user can double click an icon and the script will start without opening any extra windows. Is there an easy way to do this with OS-X? Would the user have to install a different Python than the one that comes with OS-X? I haven't been able to find a definitive answer.
You might want to look at Platypus. It's a freeware app for generating apps which wrap scripts.
Another way to do something like that is using Automator or even AppleScript Editor. Either can produce an application which just runs a script.
Update:
For Automator: Launch Automator, select the Application template, type "script" in the search field, double-click Run Shell Script, switch the shell pop-up menu to /usr/bin/python, type/paste your Python script into the text field. Or, leave the pop-menu on /bin/bash and just write an invocation of an external script in the text field. Save as an application.
You can also view help from its Help menu.
For AppleScript, launch AppleScript Editor, type the following as the script:
do shell script "/usr/bin/true"
Replace /usr/bin/true with the path to whatever script you like. Save as an application.
Again, there's help in the Help menu.
py2app does this with aplomb. You make your Python script, use whatever dependencies you need (wx, Tkinter, etc.) and py2app makes you a standalone app bundle that will run in any modern OS X environment. It bundles Python too, so you can use any Python you want (not just the system default).
The downside is that the generated apps might be large, up to 50MB if you have a lot of dependencies (though that is somewhat of an extreme).
There are two ways to do this:
Click on a script.
Press command-i to open the "get info" window.
Expand the "Open With" section (if it isn't already).
Choose "Python Launcher" from the drop-down menu.
Click "Change All" if you would like ALL Python scripts to launch when double clicked.
Possibly open Python Launcher and uncheck "Run in a Terminal window"
This will work for this machine only, so it is less portable than the following. Why? Because the default for opening a document type varies depending on what is installed (XCode and/or IDLE will both take over opening a .py file).
Method Two:
Validate the Interpreter Directive, that's the first line of the file. I suggest using /usr/bin/env python3. This will run the first python3 interpreter that is on the users path.
Make the script executable chmod a+x <script_name> from the Terminal.
Change the extension from .py to .command (this will be opened by the Terminal).
Use zip or tar for distribution so that the permissions do not get mangled.
This method will open a Terminal window, but when the Python window is closed the terminal window will also close.
If your script has dependencies outside of the standard library, then you should provide a second .command file to install those. This may make things more complicated, but using pip3 install --user <list of dependencies> should minimize complications.

Categories

Resources