9

In powershell, when I run a python program with:

> python hello.py

The program runs and prints any output directly in the powershell window I'm working in. But when I try to do it without explicitly invoking python:

> hello.py

it opens up a separate window. How can I fix that so it behaves the same way it does when I invoke python explicitly?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • Powershell is working as if you double clicked a file in a folder. Is is much trouble to write "python" ? – JBernardo Nov 02 '12 at 00:48
  • @JBernardo: No, it's not much trouble to type it. But that's beside the point. I'm working on setting up my environment in such a way that I can have programs/scripts written in any of a variety of languages and I can just execute them without knowing what language they are written in. This is one of the steps in that goal. – Benjamin Lindley Nov 02 '12 at 00:53
  • AFAIK, Windows do not support hash bangs as other do. But it isn't any hard to open a file and check the first line for something like `#!python` and use the proper interpreter. Maybe you can even do it by the `.py` extension – JBernardo Nov 02 '12 at 00:56
  • You may need to change the default application for .py files to python.exe instead of whatever it is… but that may prevent double-clicking from opening a cmd window. Is that a reasonable tradeoff for you? – abarnert Nov 02 '12 at 01:11
  • @abarnert: The default application for .py files *is* python.exe – Benjamin Lindley Nov 02 '12 at 01:20
  • OK, then there must be something else you need to change. (That's why I wrote a comment rather than an answer…) Anyway, the point is, if typing "hello.py" into Powershell does the same thing as double-clicking "hello.py" in Explorer, then it's pretty likely that whatever the fix is will also affect double-clicking files. Is that a reasonable tradeoff, or not? – abarnert Nov 02 '12 at 01:25
  • @abarnert: Yeah, that's fine. – Benjamin Lindley Nov 02 '12 at 01:28
  • I think you probably have a the `.py` extension associated with pythonw.exe. Change to be associated with python.exe. – Keith Nov 02 '12 at 01:34
  • @Keith: [Nope](http://postimage.org/image/mg2k1oa2j/) – Benjamin Lindley Nov 02 '12 at 01:40
  • 2
    @BenjaminLindley well, next suggestion: use Linux. ;-) – Keith Nov 02 '12 at 01:45
  • 1
    @Keith: I tried that too, for about a year and a half. It has a host of other problems that I got sick of dealing with. – Benjamin Lindley Nov 02 '12 at 01:47
  • 1
    @JBernardo: [pylauncher](https://bitbucket.org/vinay.sajip/pylauncher) adds shebang support. It installs with Python 3.3. – Eryk Sun Nov 02 '12 at 08:18

1 Answers1

20

If you add .PY to the PATHEXT environment variable, you should be able to run .\hello.py or just .\hello in the current console. Otherwise it will ShellExecute the associated Python.File command (check ftype Python.File), which launches a new console. I checked this by temporarily modifying the environment variable:

$env:pathext = $env:pathext + ";.PY"
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • Thanks. That answered my next question too, which was about how I could get it to execute python programs without typing the extension. – Benjamin Lindley Nov 02 '12 at 11:07
  • 1
    Your next question has already been answered in [Making Python scripts run on Windows without specifying “.py” extension](http://stackoverflow.com/q/9037346/95735). I'm pretty sure this question has already been answered too. – Piotr Dobrogost Nov 02 '12 at 13:36
  • @PiotrDobrogost: [You're right](http://superuser.com/q/437790/33682); I should have searched Stack Exchange before answering. To be clear, however, it doesn't open a `CMD` window as that question states. `python.exe` has an attached console. If it runs in a new window, you get a new console, but not a new `cmd.exe` shell. – Eryk Sun Nov 02 '12 at 19:04