0

I have a little python script that takes a single filename as a command line argument and writes a converted file. Not rocket science, it's 10s of lines long.

I can run that on windows from a cmd prompt simply by typing the name of the script. So for example:

C:\> CD \wheremyscriptis
C:\wheremyscriptis> myscript.py

and it runs fine. Without any arguments it spits out a little Usage message. Quite conventional.

Now we're using Powershell more and more and the first thing I notice in powershell is it won't run at all without an explicit directory, so in the above example I'd need to type .\myscript.py.

That's odd but you could get used to it, not a crisis.

But what happens now is the script runs in another window, which flashes up and disappears before youc an read the usage message.

Given the behavior is inconsistent across these contexts, what can we do inside the script to make the powershell context more usable. Is there a way to detect if we're running in some window that powershell threw up (which is itself weird) and then if so, pause before exiting to give a user a chance to read the usage message before the window disappears?

Bernd Wechner
  • 1,854
  • 1
  • 15
  • 32
  • The flashing window didn't happen when run from the command prompt? And you can run it without *calling* python or python -m first? – wwii Jan 17 '20 at 05:20
  • Nope. The standard command prompt (`cmd.exe`) runs it much like bash does or any "normal" shell. The `print()` output (so standard output) is written to the console and visible before the next prompt is presented. Only Powershell seems to want to run it in a sub window for some reason. – Bernd Wechner Jan 17 '20 at 05:23
  • Add an input statement - with a prompt. Or just run it as `python -m whatever` – wwii Jan 17 '20 at 05:26
  • Does this answer your question? [Python file opens and immediately closes](https://stackoverflow.com/questions/23656062/python-file-opens-and-immediately-closes) – wwii Jan 17 '20 at 05:32
  • Or more likely: [Run python programs without opening a separate shell](https://stackoverflow.com/questions/13187641/run-python-programs-without-opening-a-separate-shell) – wwii Jan 17 '20 at 05:47
  • Having to run ".\myscript.py" is quite the norm in shells. CMD is the odd shell out here, with its MS-DOS behavior. Starting with Windows Vista, CMD (as well `CreateProcessW`) will not implicitly include the current directory when searching for files if you define the environment variable [`NoDefaultCurrentDirectoryInExePath`](https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-needcurrentdirectoryforexepathw) to any value. When this variable is defined, you can still include the working directory by explicitly adding "." to the end of `PATH`, which is at least safer. – Eryk Sun Jan 17 '20 at 07:47

0 Answers0