无需打开单独的 shell 即可运行 python 程序 [英] Run python programs without opening a separate shell

查看:65
本文介绍了无需打开单独的 shell 即可运行 python 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 powershell 中,当我使用以下命令运行 python 程序时:

In powershell, when I run a python program with:

> python hello.py

该程序直接在我正在使用的 powershell 窗口中运行并打印任何输出.但是当我尝试在不显式调用 python 的情况下执行此操作时:

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

它打开了一个单独的窗口.我该如何解决这个问题,使它的行为与我显式调用 python 时的行为相同?

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

推荐答案

如果将 .PY 添加到 PATHEXT 环境变量中,您应该能够运行 .\hello.py 或在当前控制台中只是 .\hello .否则,它将ShellExecute 关联的Python.File 命令(检查ftype Python.File),从而启动一个新的控制台.我通过临时修改环境变量来检查这一点:

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"

这篇关于无需打开单独的 shell 即可运行 python 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆