I have a python script which I can run from pythonwin on which I give the arguments.
Is it possible to automate this so that when I just click on the *.py file, I don't see the script and it asks for the path in a dos window?
You're running on Windows, so you need an association between .py files and some binary to run them. Have a look at this post.
When you run "assoc .py", do you get Python.File? When you run "ftype Python.File", what do you get? If "ftype Python.File" points at some python.exe, your python script should run without any prompting.
Rename it to *.pyw to hide the console on execution in Windows.
You can also wrap it in a batch file, containing:
c:\path to python.exe c:\path to file.py
You can then also easily set an icon, run in window/run hidden etc on the batch file.
how does your script ask for or get its parameters? If it expects them from the call to the script (i.e. in sys.argv) and Pythonwin just notices that and prompts you for them (I think Pyscripter does something similar) you can either run it from a CMD window (commandline) where you can give the arguments as in
python myscript.py argument-1 argument-2
or modify your script to ask for the arguments itself instead (using a gui like Tkinter if you don't want to run from commandline).
Related
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 associate .py files to open CMD with py then the filename? Maybe in a .bat file?
sorry about my poor English, and if I insist on subjects you already master, it's my first constructive answer here ;p
I'm not sure about what you want to achieve but from your question and its tags I assume tha you want to :
run ".py" file containing a python script from the file explorer by double clicking it
have a cmd.exe window open after this action with your python script interpreted
have a way to review this scipt output without relying on superman eyes able to gasp 65536 characters per millisecond
So basically, if you have a script printing "Hello World !", you want to click on it, and see in a cmd.exe window the text "Hello World !" displayed to validate that your script is working properly ? To make it short you are RIGHT, a .bat file will be enough to do the trick, even if there is a whole bunch of alternatives including executable generation to embed a full python interpreter (see http://www.py2exe.org/), or simply adding a wait loop at the end of your script, but having a batch script associated is probably the lightest and easiest solution in your case.
As you figured out, associating .py files with the python interpreter will run your scripts but the console window will dissapear immediatly on completion without letting you the time to consider the output. You just need to associate .py files (right click -> open with, if you want to do it programatically it's possible to set this in the windows registry) with a .bat script that will do the job, that is, run the script and wait until you are ready to "leave".
This batch script will take the python script you clicked on as an argument, run it with your python interpreter and pause it's execution, waiting for your input before leaving. Since the default windows file association will execute your target program and pass it the file executed (should it be a click or a "start XXX" command) it's pretty straightforward, the bricks to do this in batch are :
program_name argument : to directly call an external command, so "python my_script.py" will run the python.exe program (no need to add the ".exe" or "'.com" part since it's an obvious case for windows) with my_script.py as argument, provided that your python executable directory is in your PATH environment variable, otherwise you will have to provide the full path, ie: "C:\Python27\python.exe my_script.py" .
%X : to reference command line arguments sent to your script (%1 for the first one, then %2 etc.,)
pause : a command that will display the message "Press any key to continue ...", and obviously wait for any key before leaving your script
evantually, #echo off : to avoid printing each batch command before its execution
So, assuming that your python interpreter is installed in C:\Python27 (please replace with whatever version / location for your python.exe, or just "python" if it's in your PATH) your batch script could look like something like this :
#echo off
C:\Python27\python.exe %1
pause
Save it somewhere, associate it with .py files, and you are done. HTH
You can do it in two separate ways:
First, you can rename your .py file to .pyw and just open it and the script would be executed immediately (with pythonw.exe) but this is not showing you a console output.
Or you can simple associate your .py files with standard python.exe which will show the console output.
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.
Is it possible to make a program in Python that, when run, does not actually open any window (including command prompt)?
For example, opening the program would appear to do nothing, but in reality, the program is running in the background somewhere.
Thanks!
Are you running the python program by double clicking *.py file in Windows?
Then, rename the *.py file to *.pyw.
Run it with pythonw.exe instead of python.exe.
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.