I've been using Cygwin on Windows recently. I want to use the Windows installation of Python, so during testing I'm using /cygdrive/c/Python26/python.exe myfile.py rather than python myfile.exe.
This is working almost perfectly, except for printing. When I run the Windows Python from Cygwin the output doesn't print until execution finishes. It works fine running in Windows Python from explorer.exe or cmd.exe, and it works in Cygwin using the Cygwin-installed Python (/bin/python.exe).
Is there a workaround for this? The important thing is to be able to run the Windows version, but I'd like to do it all from with Bash.
The real problem is that when you run a command in any of the Cygwin terminal programs like mintty, they don't act as Windows Consoles. Only Windows Console-based ones like CMD or Console2 do that. So, with Cygwin terminals the Windows python.exe doesn't think it is talking to an interactive console.
That leads to buffering output instead of flushing buffers on every line as is done in interactive sessions. That is why Amro's adding the flush() on every line fixes the symptom, but means changing the code.
One solution without changing the code is to turn off buffering in Python using the '-u' flag on the command line or setting the PYTHONUNBUFFERED environment variable.
export PYTHONUNBUFFERED=1
/cydrive/c/Python27/python.exe foo.py
or
/cydrive/c/Python27/python.exe -u foo.py
or run in interactive mode
/cydrive/c/Python27/python.exe -i foo.py
You will also not be able to run the Windows python.exe interactive mode in the Cygwin terminal. It will not bring up an interactive session, but will just hang. I find the best solution seems to be to use 'cygstart' (better than using the '-i' option):
cygstart /cygdrive/c/Python27/python.exe
And that seems to work with ipython as well (if installed):
cygstart /cygdrive/c/Python27/Scripts/ipython.exe
Not answering the initial question, but for those who want to use Python interactive session from within Cygwin terminal (for example in mintty) - start Python with "-i" option to tell it explicitly that it needs to run in interactive mode:
$ python -i
The neat way is also to create an alias in your .bashrc (knowing that it is only read for interactive terminal sessions anyway):
alias python='python -i'
Otherwise, Python will not know that it runs in the console, because all Cygwin pty-based terminals (mintty, rxvt and xterm) are recognized as pipes by Windows, not as the console. Therefore, Python thinks there is no console and enters non-interactive mode. So, if you still want interactive mode instead, you need to explicitly tell Python to use it. However, it still won't behave as it normally should - one still won't be able to use HOME or LEFT ARROW keys, and so on.
Perhaps if you flush the output
import sys
V = range(100000)
for x in V:
print x
sys.stdout.flush()
Related
I'm using mingw with msys and mintty on windows. I have a problem that msys and mintty are somehow not flushing output until a command is finished. This means I can't really run any interactive programs.
For example, if I have in C:
printf("Test\n");
the output won't appear until the program has terminated. However, if I have:
printf("Test\n"); fflush(stdout);
then the output appears immediately. If I use msys without mintty or the windows console, then everything works normally.
So my question, what's going on with msys and mintty?
This can be an issue when msys uses the rxvt shell under a number of scenarios. In cases where I see this problem, I ask msys to use the native Windows shell for its console. For example:
C:\MinGW\msys\1.0\msys.bat --no-rxvt
I thought that modern MSYS installations default to using the native shell as MSYS developers seem to prefer it. I have other issues with the native shell that drive me to use the rxvt shell, so I do infrequently run into this issue.
C:\MinGW\msys\1.0\msys.bat --rxvt
I find that the rxvt shell usually works fine except for certain applications that are built as "console" utilities meant to run from a command-line.
The only thing that worked for me was to precede the command with winpty ...
$ winpty java ClassName
It causes unbuffered output to be 3x slower and buffered output to be 5x slower (in my case, with Java).
To always have a command invisibly invoked by winpty ...
$ cd ~
$ pwd -W
... add the following line to .bashrc ...
alias java="winpty java"
... then restart terminal and ignore the (one-time) warning message.
I was going through Google's Python instructions and it quotes:
The commands above are the simplest way to run python programs. If the
"execute bit" is set on a .py file, it can be run by name without
having to type "python" first. Set the execute bit with the "chmod"
command like this:
Where it proceeds to give me this code to run on my terminal:
~/google-python-exercises$ chmod +x hello.py
~/google-python-exercises$ ./hello.py ## now can run it as ./hello.py
Hello World
However when I type in ./hello.py, I get:
So is this something to do with me using Python 3 when this could be a command for Python 2?
EDIT
I just realised that this can only be run on a Linux/Mac. How does one go about doing this on a Windows?
You need to associate the file extension with Python properly. This should happen by default if you install system wide (which requires administrator privileges), but if you're using a third party distribution, it's not guaranteed, and it may require a reboot even so.
For more details running Python on Windows, see the Python on Windows docs. It looks like if you omitted the launcher from your Python install, it won't have registered the file extensions; it may be worth reinstalling properly to fix that.
Otherwise, if you can't get file extension associations working, just run:
python hello.py
or starting with 3.3 and later, you can use:
py hello.py
which lets you provide an argument to py.exe to change which installed version of Python to use (if desired).
You wouldn't use ./hello.py on Windows at all, because that's the wrong directory separator (Windows sometimes allows forward slashes, but in DOS prompts, that looks like trying to launch a program named . with the switch /hello.py). You'd use plain hello.py (Windows always searches the current working directory) or .\hello.py if you want to be explicit.
Do you have a shebang as the first line in hello.py?
something like:
#!/usr/bin/env python3
I am trying to use Curses in PyDev in Eclipse in Win7.
I have installed Python 3.2 (64bit) and curses-2.2.win-amd64-py3.2. When I input the following testing codes into PyDev:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
It did not show any syntax error, so I think the curses was installed correctly.
However, when I ran it as Python Run, the output showed: Redirection is not supported. I do not know where this problem comes from. I googled a lot but can't find related information.
Recent PyCharm versions (I am currently running 2017.2, not sure when this option was added, or if it has been there the entire time) have the option "Emulate terminal in output console". Curses works with this option checked.
You cannot expect to use curses with a non-terminal.
Probably you get this because you are running the script from inside an IDE, like PyCharm or any other.
All IDEs do provide consoles that are not terminals, so that's where the problem comes from.
For a Pycharm user the solution given by codeape works fine :
Snapshot
You can't use any IDE to run python files with the curses package. I used to run in pycharm and naturally couldn't run.
Change to the command line to run:
for testing follow my following steps
on desktop open notepad and copy paste the code and save it as filename.py
open command line change directory to desktop use below command cd Desktop and hit enter type python example.py and hit enter, your program will definitely run
My workaround is to create a Run Configuration that calls a curses script. The little overhead is worth not having to switch to the terminal and manually run the script hundreds of times a session. I use Intellij but I imagine the process should be similar in PyCharm.
The desired result is the convenience of a button to run the script:
First create a script that calls the entry script, for instance:
ptyhon name-of-script.py
Then, to create a configuration for each script:
Go to Edit configuration.
Click the plus button and add a Shell Script.
Enter the path to a shell script.
Here is a picture of a directory with a couple of sample scripts.
I use this process to view my progress. My curses scripts are very modest so fortunately I can live without a debugger.
If I use M-x shell and run the interactive Python interpreter, Emacs on Windows does not return any IO.
When I discovered M-x python-shell, I regained hope. However, instead of running the interactive Python shell, I want to run a specific Python script that features an interactive CLI. (See Python's cmd module for details).
Is there a way of launching a Python script in Emacs that is interactive? (stdout, stdin, stderr)
I grabbed one of the cmd examples from here: http://www.doughellmann.com/PyMOTW/cmd/index.html
Seemed like all the input lines in the example had a (Cmd) prompt - not sure if that's always the case.
Edited the python-shell definition in python.el to include "(Cmd) "
(setq comint-prompt-regexp "^>>> \\|^[.][.][.] \\|^(pdb) \\|^\\(Cmd\\) ")
re-evaluated the python-shell region, started the shell and loaded the script using:
>>> execfile("script.py")
Input/output seemed to work... not sure about stderr or if this would work with a more complicated script.
You should look into the other shell modes. TERM-MODE and ANSI-MODE. I believe they can support interactive command line programs.
You can run python in interactive mode by typing python.exe -i
Alternatively, you can download ipython and just run ipython.exe
How do I run a Python file from the Windows Command Line (cmd.exe) so that I won't have to re-enter the code each time?
Wouldn't you simply save your Python code into a file, and then execute that file using Python?
Save your code into a file called Test.py.
And then run it?
$ C:\Python24\Python.exe C:\Temp\Test.py
If you don't want to install an IDE, you can also use IDLE which includes a Python editor and a console to test things out, this is part of the standard installation.
If you installed the python.org version, you will see an IDLE (Python GUI) in your start menu. I would recommend adding it to your Quick Launch or your desktop - whatever you are most familiar with. Then right-click on the shortcut you have created and change the "Start in" directory to your project directory or a place you can mess with, not the installation directory which is the default place and probably a bad idea.
When you double-click the shortcut it will launch IDLE, a console in which you can type in Python command and have history, completion, colours and so on. You can also start an editor to create a program file (like mentioned in the other posts). There is even a debugger.
If you saved your application in "test.py", you can start it from the editor itself. Or from the console with execfile("test.py"), import test (if that is a module), or finally from the debugger.
If you put the Python executable (python.exe) on your path, you can invoke your script using python script.py where script.py is the Python file that you want to execute.
Open a command prompt, by pressing Win+R and writing cmd in that , navigate to the script directory , and write : python script.py
A good tool to have is the IPython shell. Not only can it run your program (%run command), but it offers also many tools for using Python interactively in an efficient manner (automatic completion, syntax coloring, quick access to the documentation, good interaction with Matplotlib,…). After you install it, you'll have access to its shell in the Start menu.
You need to create environment variables. Follow the instructions here: http://www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables
In DOS you can use edit to create/modify text files, then execute them by typing python [yourfile]