python - 是否可以在当前终端中运行 Python 选择?
问题描述
在VSCode
我们有命令Python: Run Selection in Python terminal
。
是否可以在current
终端中运行它而不是打开一个新终端?
在运行 Python 行之前,我需要在当前终端中运行一些脚本,这就是我要问的原因。
解决方案
简短的回答是肯定的,这是可能的。但是你需要一个shell命令。我不认为按下Run Selection in Python terminal
快捷方式会起作用。
例如,假设您在同一个目录中有两个脚本,命名为test1.py
并test2.py
具有各自的内容:
# test1.py
print("This is the 1st script")
和
# test2.py
print("This is the 2nd script")
要运行它们,您有以下选择(zsh
在正确目录内的当前终端上运行这些命令):
1按顺序运行脚本,因为其中一个取决于前一个脚本的成功:
% python test1.py && python test2.py
结果:
This is the 1st script
This is the 2nd script
2按顺序运行脚本,因为其中一个依赖于前一个脚本的失败:
% python test1.py || python test2.py
出现错误时的结果test1.py
:
File "/Users/Documents/test/test1.py", line 2
^
SyntaxError: unexpected EOF while parsing
This is the 2nd script
3与后台进程同时运行脚本:
% python test1.py & python test2.py &
结果:
[1] 29290
[2] 29291
test % This is the 2nd script
This is the 1st script
[2] + done python test2.py
test %
[1] + done python test1.py
您也可以将这些命令应用于不同数量的脚本,例如:
% python test1.py && python test2.py && python test3.py
% python test1.py || python test2.py && python test3.py
推荐阅读
- node.js - 如何处理竞争条件以更新会话?
- haskell - 为项目创建配置 Haskell 的默认目录
- angular - Angular 应用程序无法使用一个组件并注入服务
- php - 使用 FOSUserBundle 的 Symfony 迁移为空
- c# - C# 中的 FileStream 是否具有可以使用的最大文件大小?
- javascript - 将选择的背景图像保存到本地存储并在刷新站点时自动显示
- javascript - 从 URL 获取 html 数据属性
- css - Dropcap - :first-letter 伪类在 Firefox 中没有按预期工作
- docker - 如何将存储库中的文件复制到用于作业的 Docker 容器中,在 gitlab-ci.yml
- php - Laravel 显示:“unserialize(): Error at offset 0 of 6 bytes”当表列为空时