Popen Command Prompt weird behaviour - python

I was fiddling with the subprocess.Popen and got to a weird situation I just can't explain. Leaving here in the hope someone can make some sense of this.
Description
Spawning a cmd.exe subprocess from the python process, results in the prompt switching from python to cmd. And when the python prompt is shown the command is executed in cmd.exe. On the other hand when the cmd.exe prompt is shown, issuing a command executes in the python shell.
Getting to this weird back and forth prompt. An example as follows:
Microsoft Windows [Version 10.0.17134.829]
(c) 2018 Microsoft Corporation. All rights reserved.
C:\Users\Pedro Rodrigues>help
Type help() for interactive help, or help(object) for help about object.
>>>
Apparently a Windows Command Prompt. But, not only the help command is python's, afterwards we appear to be dropped in the python shell.
But, if we do it again. Its actually the other way around. This is now the Windows Command Prompt, but we are presented with the python prompt!
>>> help
For more information on a specific command, type HELP command-name
ASSOC Displays or modifies file extension associations.
... supressed ...
WMIC Displays WMI information inside interactive command shell.
For more information on tools see the command-line reference in the online help.
C:\Users\Pedro Rodrigues>
And this keeps on going, the terminal is essentially useless at this point, as far as I could tell.
Steps to Reproduce
Open a python shell, either:
Open Command Prompt, and type python.
Open a python shell.
Run the following code:
import subprocess
subprocess.Popen('cmd.exe')
Type help, press ENTER.
Repeat previous step.
What is going on?
I'm surprised by the result. With that said, I just cannot explain what is happening here.
On Linux
The behaviour is a bit different, although it also renders the terminal useless. I haven't played a lot with it on Linux, but did various times end up with a working terminal actually.
import subprocess
subprocess.Popen('/bin/bash')
But most of the times, got a working bash shell. Which stopped the python process every time.
I understand this is an esoteric scenario, yet what happens on Windows puzzles me.

Related

Python Newbie – How do I make VS Code running Python 3 only display the result of the code?

Whenever I run python code in the PyCharm IDE it simply and cleanly shows the output. But when I run python code in the Microsoft VS Code editor it shows the following:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\MY ACTUAL NAME\Desktop\Pythonista> python -u "c:\Users\MY ACTUAL NAME \Desktop\Pythonista\Exercise_3.py"
"RESULT OF CODE HERE"
PS C:\Users\MY ACTUAL NAME\Desktop\Pythonista>
The code executes underneath the Terminal label; to the left of the Problems, Output, and Debug Console headers. Why can’t it just show the result of my code without all the other details. The reason I ask is because I am trying to record myself doing the work, but I do not want my name broadcasted and I just want the output to look cleaner. Is this a thing? Thanks.
Edit: put code in a code block.
Press Crtl + J to open a terminal (or Powershell in your case), then run your code with the command python3 file.py.
Do this as opposed to clicking "Run" in the IDE, which is what most people do.
Note: when you open the terminal up initially, if it is full of jargon you don't want displaying, type clear (or the equivalent on Windows, which I think is cls).
you can use try and except so the errors won't appear in the terminal
example:
try:
# put your code here
except Exception:
pass
The right question that I needed to ask was, how do I shorten my path in VS Code and how do I get rid of the extra text that appears just before my code in the terminal after execution. After doing some research, it appears these questions have been asked many times. Below is how it is done for any other newbie like myself that stumbles upon this.
First, go to VS Code, File, Preferences, Settings, click button on the top right corner to open settings.json. Paste this,
"terminal.integrated.shellArgs.windows": ["-NoLogo"],
Next, while it is impossible to hide your path in VS code from the code output, you can certainly make your output look cleaner by moving the folder location from where the piece of code is stored. All together, when I executed my code, this is what it looked like before:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\Users\MY NAME\Desktop\Pythonista> python -u "c:\Users\MY NAME\Desktop\Pythonista\Exercise_3.py"
Hello StackOverflow
And now, after moving my code file location closer to C:, and editing settings.json, when I execute my code, it looks like this,
PS C:\Pythonista> python -u "c:\Pythonista\Exercise_3.py"
Hello StackOverflow
Much cleaner, I hope this helps anyone else.

NameError: name 'ls' is not defined

I am running Windows 7 and just installed Python 2.7.4 and all I get are SyntaxError:'s. For example when I type:
>>>ls
I get
Traceback (most recent call last):
file "<stdin>", line 1, in <module>
NameError: name 'ls' is not defined.
I am new to Python and have no idea what the problem is.
Math calculations work as well as code seems to work. It's just commands like cd, ls, sudo apt-get update (and upgrade). Any guidance would be appreciated.
I think it's unfortunate that you're being downvoted so heavily here. If you're new to programming, this can be an honest mistake to make since a lot of tutorials assume that you're already familiar with the command line.
The main problem you have (as others have pointed out) is distinguishing between the operating system shell (which is also known as the console, terminal, command line, or command prompt) and the Python shell.
The operating system shell/command prompt is one of the fundamental ways you can interact with your computer. When you open the command prompt, you run arbitrary programs by typing in their name, run commands such as ls, sudo, cd, and apt-get.
If you type in python, for example, it'll cause the command prompt to start the Python shell. From there, you can run all of the Python commands, as usual. However, this is an entirely separate program with an entirely separate set of rules. The things you can do in the command prompt will not work in Python, and vice-versa.
Now, another point of confusion is distinguishing between the Windows command prompt and the Linux command prompt. You can install and run Python in both, but certain commands (in particular, sudo and apt-get) are available only on Linux operating systems. Unfortunately, the rules for using the Windows command prompt and the Linux command prompt differ somewhat, meaning that some instructions you find will be applicable only in one kind of operating system.
So no wonder you're confused! You're trying to learn about Python, but a good number of tutorials/articles will mention this second, entirely different system with hardly a word of introduction, and expect you to already know how to use it.
If you're curious about learning more about using the command line, here's a good crash course you can read. It'll teach you what you need to know for all 3 major operating systems (Windows, Linux, and Mac) and will hopefully make what you read online more clear.
I see from your profile you are learning about Raspberry Pi. A Raspberry Pi can use Debian (or Ubuntu, which is based on Debian) as the Linux distribution. The commands you are trying in Python are shell commands to run on that operating system.
Python is another command you can run on Raspberry Pi, but that doesn't mean that installing Python on Windows gives you those other commands on Windows too. You have two entirely different things confused here.
it's just commands like cd, ls, sudo apt-get update (and upgrade). Any
guidance would be appreciated
Python doesn't support those commands. Those are operating system level commands, not python level commands.
The Root of my problem was that I was not asking the right question. I was trying to run scripts in the interactive-mode and entering 'ls' and 'dir' to see where I was in the directory. With everyone's guidance I finally found the part in the Docs about Executable Python Scripts that said Windows automatically associates the 'py' extension with python.exe.
here. Then it all came together in my head. I had associated the 'py' extension with my editor instead of python and that I should have been at the Windows command prompt.
Thanks for everyone's help and quick responses.
The answers above are correct, but they don't help you with your issue!
One solution that will work in Python is:
%ls

Python curses Redirection is not supported

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.

Command Prompt Python

What is the benefit of running code through the command prompt/terminal vs an ide?
I've noticed recently when using the progressbar module of python that the progress text is updated on the same line in the command prompt window while the ide prints each text on the next line. Why are these different? Are they not running though the same interpreter?
The IDE adds an extra layer of software between the program and the python interpreter.
What you are seeing is probably that the IDE's output window is not a complete terminal emulator, and doesn't understand or ignores the commands that the progressbar module uses. to keep the output on the same line.
Have a look at ipython. It is a very nice environment for testing and running python code.
Each IDE is infact interacting via the command line and redirecting streams into it's implementation of showing those outputs, Each IDE has it's own way of doing this, command prompt is more powerful if you are expeirienced and easy to try one off scripts, try ipython which is great for beginners and learners alike for fast access to the programming environment and trying out module.

Using Windows Python from Cygwin

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()

Categories