I am trying to make an executable python program on MAC OSX. I used the build applet program and it runs, but I had some data printing in the shell window and the executable file does not open a window. Is there a way to open a shell window with an executable python program?
Thanks
Not sure about generating another shell window, but do you need to have an entire shell open? What about getting the information to the user in a different way, such as:
use another Toplevel window and insert the output into a Text or Listbox, rather than simply printing it. This could also make it easier for users to copy the output (if that's something they might find useful).
write out a data/log file.
If you are using Automator to simply run your python script, and you really need it to open a shell window, here is a cheap hack:
Using a simple application with Run Shell Script action:
open -a Terminal /path/to/python/script.py
But if all your application is doing is printing output, all that output would be visible in the Console.app.
Related
I have installed Python and written a program in Notepad++.
Now when I try to type the Python file name in the Run window, all that I see is a black window opening for a second and then closing.
I cant run the file at all, how can run this file?
Also I want to tell that I also tried to be in the same directory as a particular Python file but no success.
I assume you are running the script with command python file_name.py.
You can prevent closing of the cmd by getting a character from user.
use raw_input() function to get a character (which probably could be an enter).
It sounds like you are entering your script name directly into the Windows Run prompt (possibly Windows XP?). This will launch Python in a black command prompt window and run your script. As soon as the script finishes, the command prompt window will automatically close.
You have a number of alternatives:
First manually start a command prompt by just typing cmd in the Run window. From here you can change to the directory you want and run your Python script.
Create a Windows shortcut on the desktop. Right click on the desktop and select New > Shortcut. Here you can enter your script name as python -i script.py, and a name for the shortcut. After finishing, right click on your new shortcut on the desktop and select Properties, you can now specify the folder you want to run the script from. When the script completes, the Python shell will remain open until you exit it.
As you are using Notepad++, you could consider installing the Notepad++ NppExec plugin which would let you run your script inside Notepad++. The output would then be displayed in a console output window inside Notepad++.
As mentioned, you can add something to your script to stop it completing (and automatically closing the window), adding the line raw_input() to the last line in your script will cause the Window to stay open until Enter is pressed.
Try to open in Command Prompt instead of run window. The syntax is:
py filename.py
If it doesn't work, try to reconfigure Python. Did you set environment variables? If not, this could help you
I have a program called ftpgrab.py. At the command prompt to run it I type:
c:\path\to\python\dir\python.exe ftpgrab.py
Is there a way on Windows 8 to create an icon which I can double-click to run this?
You can either create a batch file that will launch the program, or use something like pyinstaller to transform your script into an executable that can be run directly.
create a file named foo.bat;
copy your command to that file and save it;
double click foo.bat...
Assuming that you used one of the standard installers for python on windows, .py is already registered and it should just work. Copy it to your desktop and double-click. A console running the program should appear and run as normal. Its still a console app - the the customer wants a gui app, that's a different story.
btw, you shouldn't even have to type c:\path\to\python\dir\python.exe ftpgrab.py, just a plain ftpgrab.py or ftpgrab should do.
I use the Windows version of Python. I have a Python script using Pyside (nothing complicated, a kind of "hello world").
When I click on my script file or if I launch it from a command line, it executes perfectly and I have a GUI appearing.
However, I would like to avoid having a GUI if the script is launched from a textual terminal (cmd.exe, cygwin, ...). A kind of script which automatically knows if it should have a GUI output or a textual output.
Is there an easy and simple way to do that? I want to be able to do that with the Windows version of Python (not the one coming with Cygwin packages).
An obvious way would be to add a kind of "--no-gui" parameter when I launch the script from a textual terminal, but I wonder if Python (or some Python libraries) already provide tools for that.
Moreover I have an SSH server (Cygwin-base) on this computer, I can execute the script at distance but no GUI appear (of course) and I have no error message. It is a case where it is very interesting to know if the script failed because of the lack of Windows graphical support or if the script should adapt its output for a textual terminal.
I know that you can run file as .py file or as .pyw file. The second option is used for GUI applications and it does not open the console window. To distinguish these to two cases you can check isatty method of sys.stdout.
import sys
if sys.stdout.isatty():
# .py file is running, with console window
pass
else:
# .pyw file is running, no console
pass
EDIT:
I tried to run that with putty+ssh on linux box - it returns True.
I tried to use msys bash shell on windows box - it returns True (.py file)
I tried to use cygwin bash shell with cygwin python - it returns True (.py file)
Unfortunately, I have no possibility to try putty + windows cygwin ssh server.
I have created a script in python and saved it as .pyw file so that I don't need console to execute the script. I am wondering how to stop the execution once it is running. What I am doing right now is opening the IDLE and closing it and it seems to work. I am sure there is a better way of doing this. Can somebody advise me about this?
As it says in the docs
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.
The .pyw extension is really meant to be used by Python GUIs. Since they create their own windows, the console window isn't required. If a .pyw script doesn't create any GUI windows, killing the python process is the only way to stop execution.
If you don't want to use the console to run your script (using the .py extension), just double-click on it.
So I want to make a Python file that runs all the code within, but the window is invisible. So the user won't see the window in the task bar, or really anywhere on his screen.
How would I do something like that?
To make a script not open the terminal window change the extension of your script to .pyw which will cause the script to be executed by pythonw.exe by default. This suppresses the terminal window on startup.
If you would like all scripts to open like this you should read up on Executing Scripts.
Maybe you'd want to use start pythonw.exe