Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run python programs without opening a separate shell

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?

like image 391
Benjamin Lindley Avatar asked Nov 02 '12 00:11

Benjamin Lindley


People also ask

How do I run a Python .PY file in idle?

To execute a file in IDLE, simply press the F5 key on your keyboard. You can also select Run → Run Module from the menu bar. Either option will restart the Python interpreter and then run the code that you've written with a fresh interpreter.

How do I run a Python file directly?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World! If everything works okay, after you press Enter , you'll see the phrase Hello World!

How do I run a Python program in shell script?

Running a ScriptOpen the terminal by searching for it in the dashboard or pressing Ctrl + Alt + T . Navigate the terminal to the directory where the script is located using the cd command. Type python SCRIPTNAME.py in the terminal to execute the script.


1 Answers

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"
like image 122
Eryk Sun Avatar answered Sep 19 '22 06:09

Eryk Sun