如何在记事本 + + 中执行 Python 脚本?

我更喜欢使用记事本 + + 来开发,

如何通过 Notepad + + 执行 Python 中的文件?

468662 次浏览

第一个选择: (最简单,推荐)

打开记事本 + + 。在菜单上转到: Run-> Run. . (F5)。输入:

C:\Python26\python.exe "$(FULL_CURRENT_PATH)"

现在,不要按 run,而是按 save 为其创建一个快捷方式。

笔记

  • 如果你有 Python 3.1: 输入 Python31而不是 Python26
  • 如果您希望在脚本完成之后命令行窗口保持打开状态,请添加 -i

第二个选择

使用运行 Python 脚本的批处理脚本,然后创建从 Notepad + + 到该脚本的快捷方式。

正如这里解释的: http://it-ride.blogspot.com/2009/08/notepad-and-python.html


第三种选择: (不安全)

代码打开“ HKEY _ CURRENT _ USER Software Python PythonCore”,如果该密钥存在,它将获取该密钥的第一个子密钥的路径。

检查此密钥是否存在,如果不存在,可以尝试创建它。

我使用 NPP _ Exec 插件(在插件管理器中找到)。安装完成后,打开控制台窗口(ctrl + ~)并键入:

cmd

这将启动命令提示符。然后键入:

C:\Program Files\Notepad++> **python "$(FULL_CURRENT_PATH)"**

执行正在处理的当前文件。

@ Ramiz Uddin 的回答绝对值得更多关注:

  • 打开记事本 + +
  • 菜单上显示: 快跑 & rarr; 跑. . (F5)
  • 输入: cmd /K python "$(FULL_CURRENT_PATH)"

之前提出的解决方案对我来说都不管用,需要稍微修改一下。

在记事本 + + 中点击 F5后,键入:

cmd /k "C:\Python27\python.exe $(FULL_CURRENT_PATH)"

命令提示符保持打开状态,以便您可以看到脚本的输出。

我希望人们在这里张贴步骤,而不仅仅是整体概念。我终于让 cmd/k 版本起作用了。

详细说明如下:

  1. 在 NPP 中,单击菜单项: Run
  2. 在子菜单中,单击: Run
  3. 在 Run... 对话框的“要运行的程序”字段中,删除所有现有文本并键入: cmd/K“ $(FULL _ CURRENT _ PATH)” 这个/K 是可选的,如果需要的话,它会在脚本运行时打开创建的窗口。
  4. 按下保存按钮。
  5. 快捷对话框打开,如果你想要一个快捷键,请填写它(不管是什么,上面有一个注释说“这将禁用加速器”,所以也许你不想使用这个快捷键,尽管在你不需要加速器的时候分配一个可能不会有什么坏处)。 我认为您必须告诉 NPP Python.exe 文件的位置(例如,对我来说: C: Python33 Python.exe)。我不知道你是在哪里或者怎么做到的,但是通过尝试各种方法,我做到了——我不记得是哪种尝试起作用了。

以下是对我有效的方法:

打开记事本 + + ,按 F5键,你会看到一个小弹出框:

Pop up box for entering the program to run

Type: < strong > C: Python 27 python.exe-i“ $(FULL _ CURRENT _ PATH)” for Python 2.7类型: < strong > C: Python 27 python.exe-i“ $(FULL _ CURRENT _ PATH)” for Python 2.7。

然后 另存为..,并选择自己的组合键启动它每次你想运行的东西

我最近开始使用 Notepad + + for Python,发现这个方法非常简单。准备好运行代码后,右键单击“记事本 + +”窗口中代码的选项卡,并选择“在 cmd 中打开包含文件夹”。这将把命令提示打开到存储当前程序的文件夹中。你现在需要做的就是执行:

巨蟒

这是在 Notepad + + (Build 10 Jan 2015)上完成的。

我不能添加截图,所以这里有一个博客帖子与截图 -http://coder-decoder.blogspot.in/2015/03/using-notepad-in-windows-to-edit-and.html

在 Notepad + + 中,转到 跑→跑..。,选择 Python 安装的路径和 idle.py文件:

C:\Python27\Lib\idlelib\idle.py

加一个空格:

"$(FULL_CURRENT_PATH)"

现在你来了!

视像示范:

Https://www.youtube.com/watch?v=sjipye1jt38

第一个 安装 Python https://www.python.org/downloads/

运行安装程序

我不知道 一定要两个都检查:

  • 为所有用户安装启动程序
  • 将 Python 3.6添加到 path

单击 install now 并完成安装。

打开记事本 + + 并从插件管理器安装 PyNPP 插件

将新文件保存为 new.py

输入 N + +

import sys


print("Hello from Python!")
print("Your Python version is: " + sys.version)

按 Alt + Shift + F5

就这么简单。

这里没有答案,或插件,我发现提供了我想要的。一个极简主义的方法来启动我的 Python 代码我写在记事本 + + 与一个快捷方式,最好没有插件。

我有 Python 3.6(64位) ,适用于 Windows 8.1 x86 _ 64和 Notepad + + 32位。在 Notepad + + 中编写 Python 脚本并保存之后,为 Run按 F5。然后写:

"C:\Path\to\Python\python.exe" -i "$(FULL_CURRENT_PATH)"

然后点击“运行”按钮。i标志强制终端在代码执行终止后保持静止,以便您检查它。这个命令将在一个 cmd 终端中启动脚本,而终端将仍然在那里,直到您键入 exit()关闭它。

为了方便起见,您可以将它保存到一个快捷方式(我的是 CTRL + SHIFT + P)。

在上述解决方案中,有一个问题没有得到解决。Python 将当前工作目录设置为解释器的开始位置。如果你需要当前工作目录与保存文件的目录相同,那么你可以按下 f5键并键入:

cmd /K cd "$(CURRENT_DIRECTORY)"&C:\Users\username\Python36-32\python.exe -i "$(FULL_CURRENT_PATH)"

除了您可以将 C: Users 用户名 Python 36-32 python.exe 替换为您机器上的 python 解释器的路径。

基本上,您要启动命令行,将目录更改为包含。要运行的 py 文件,然后再运行它。您可以使用’&’符号将任意多个命令行命令串在一起。

扩展 Reshure 的回答

  1. 从记事本 + + 中的菜单栏打开 跑→跑..。(快捷方式: F5)

  2. 在给定的空格中,输入:

    "$(FULL_CURRENT_PATH)"  -1
    
  3. Click Run

ta da!

Run-> Run 菜单选项的所有答案都与 cmd 的“/K”开关一起使用,因此终端保持打开状态,或者“-i”表示 python.exe,因此 python 强制执行交互模式——两者都保留输出供您观察。

然而在 cmd /k中,你必须键入 exit来关闭它,在 python -i-quit()中。如果这样的输入对您来说太多了(对我来说当然是:) ,那么要使用的 Run 命令是

cmd /k C:\Python27\python.exe  "$(FULL_CURRENT_PATH)" & pause & exit

C:\Python27\python.exe——显然是 Python 安装的完整路径(或者只是 python,如果你想用你用户路径中的第一个可执行文件)。

&是 Windows 中下一个命令的无条件执行-无条件的,因为它不管前一个命令的 RC 是什么(&&是“ and”-只有在前一个命令成功完成时才运行,||是“ or”)。

pause-打印“按任意键继续... ...”并等待任意键(如果需要,可以禁止输出)。

exit-好的,为您输入出口:)

最后,cmd运行 python.exe,它执行当前文件并保持窗口打开,pause等待您按下任何键,当您按下任何键时,exit最终关闭窗口。

在菜单上转到: “ Run”—— > “ Run...”(或者直接按 F5)。

对于 Python 2,输入:

py -2 -i "$(FULL_CURRENT_PATH)"

对于 Python 3,输入:

py -3 -i "$(FULL_CURRENT_PATH)"

参考文献:

为了更好地理解 py命令:

py -h

另一个了解 py命令的有用链接: 如何在 Windows7中运行 python2和3?

多亏了 重新安慰他的回答帮我找到了正确的方向。

我还希望直接从 Notepad + + 运行 python 文件。 最常见的在线选项是使用内置选项 Run。然后你有两个选项:

  1. 在控制台中运行 python 文件(在 Windows 中是 命令提示符) ,代码类似于 这个(链接: < img src = “ https://i.stack.imgur.com/l12hC.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> ) :

    C:\Path\to\Python\python.exe "$(FULL_CURRENT_PATH)"
    

    (如果您的控制台窗口在运行后立即关闭,那么您可以将 cmd /k添加到 密码。链接: < img src = “ https://i.stack.imgur.com/l12hC.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/l12hC.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/l12hC.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/MEBpd.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/MEBpd.png”alt = “ enter image description here”> )这个工作很好,你甚至可以通过将 -i添加到你的 -i0来运行 互动模式中的文件(链接: -i1 -i2 -i3 -i4 -i5 -i6 -i7 -i8)。

  2. 使用类似于 这个的代码在 闲着中运行 python 程序(链接: < img src = “ https://i.stack.imgur.com/l12hC.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/MEBpd.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/MEBpd.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/MEBpd.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/MEBpd.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> ,在这些链接中使用的是 C:\Path\to\Python\Lib\idlelib\idle.py,但是我使用的是 C:\Path\to\Python\Lib\idlelib\idle.bat,因为 idle.bat会自动设置正确的当前工作目录) :

    C:\Path\to\Python\Lib\idlelib\idle.bat "$(FULL_CURRENT_PATH)"
    

    实际上,这并不是在 闲置壳牌中运行程序,而是在 空闲编辑器中打开 Python 文件,然后您需要单击 Run Module(或单击 F5)来运行程序。所以它在 空闲编辑器中打开您的文件,然后您需要从那里运行它,这违背了从 Notepad + + 运行 python 文件的目的。

    但是,在网上搜索的时候,我发现了一个选项,在你的 密码中加入了‘-r’(链接: < img src = “ https://i.stack.imgur.com/l12hC.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/MEBpd.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/MEBpd.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> < img src = “ https://i.stack.imgur.com/rEwv4.png”alt = “ enter image description here”> ) :

    C:\Path\to\Python\Lib\idlelib\idle.bat -r "$(FULL_CURRENT_PATH)"
    

    这将在 闲置壳牌中运行 Python 程序,因为它是在 IDLE 中,所以默认情况下是在交互模式下。

通过内置的 Run选项运行 Python 文件的问题是 每次运行 python 文件时,都会打开新的控制台或 IDLE 窗口,并丢失以前执行的所有输出。这可能对某些人来说并不重要,但是当我开始在 python 中编程时,我使用的是 Python IDLE,所以我习惯了在同一个 空闲弹壳窗口中多次运行 python 文件。从 Notepad + + 运行 python 程序的另一个问题是,您需要手动保存文件,然后单击 Run(或按 F5)。要解决这些问题(AFAIK*) ,您需要使用记事本 + + 插件。从 Notepad + + 运行 python 文件的最佳插件是 行政长官办公室。(我也试了 PyNPPPython 脚本PyNPP在控制台中运行 python 文件,它可以工作,但是你可以通过内置的 Run选项在没有插件的情况下运行,而 Python 脚本用于运行与 Notepad + + 交互的脚本,所以你不能运行你的 python 文件要使用 行政会议插件运行 python 文件,你需要转到 Plugins -> NppExec -> Execute,然后输入类似 这个的内容(链接: Plugins -> NppExec -> Execute0 Plugins -> NppExec -> Execute1) :

C:\Path\to\Python\python.exe "$(FULL_CURRENT_PATH)"

使用 行政会议,你还可以在使用 npp_save 指挥官运行之前保存 python 文件,使用 cd "$(CURRENT_DIRECTORY)" 指挥官设置工作目录,或者使用 cd "$(CURRENT_DIRECTORY)"0 cd "$(CURRENT_DIRECTORY)"1在 互动模式运行 python 程序。我在网上找到了很多提到这些选项的链接(cd "$(CURRENT_DIRECTORY)"2 cd "$(CURRENT_DIRECTORY)"3 cd "$(CURRENT_DIRECTORY)"4 cd "$(CURRENT_DIRECTORY)"5 cd "$(CURRENT_DIRECTORY)"6) ,但是我在 cd "$(CURRENT_DIRECTORY)"8找到的运行 python 程序的最佳方式是使用 行政会议:

npp_console -  // disable any output to the Console
npp_save  // save current file (a .py file is expected)
cd "$(CURRENT_DIRECTORY)"  // use the current file's dir
set local @exit_cmd_silent = exit()  // allows to exit Python automatically
set local PATH_0 = $(SYS.PATH)  // current value of %PATH%
env_set PATH = $(SYS.PATH);C:\Python27  // use Python 2.7
npp_setfocus con  // set the focus to the Console
npp_console +  // enable output to the Console
python -i -u "$(FILE_NAME)"  // run Python's program interactively
npp_console -  // disable any output to the Console
env_set PATH = $(PATH_0)  // restore the value of %PATH%
npp_console +  // enable output to the Console

所有您需要做的就是复制这段代码,如果您使用的是其他 Python 版本(例如,*,我使用的是 python 3.4,所以我的目录是 C:\Python34) ,那么就修改您的 python 目录。这段代码工作得很完美,但是我在这段代码中添加了一个 台词,这样我就可以多次运行 python 程序而不会丢失以前的输出:

npe_console m- a+

a+ 是启用“ append”模式,该模式保留前一个控制台的文本,但不清除它。

m-关闭控制台的内部消息(那些是绿色的)

我在 NppExec 的 Execute 窗口中使用的最后一个代码是:

npp_console -  // disable any output to the Console
npp_save  // save current file (a .py file is expected)
cd "$(CURRENT_DIRECTORY)"  // use the current file's dir
set local @exit_cmd_silent = exit()  // allows to exit Python automatically
set local PATH_0 = $(SYS.PATH)  // current value of %PATH%
env_set PATH = $(SYS.PATH);C:\Python34  // use Python 3.4
npp_setfocus con  // set the focus to the Console
npe_console m- a+
npp_console +  // enable output to the Console
python -i -u "$(FILE_NAME)"  // run Python's program interactively
npp_console -  // disable any output to the Console
env_set PATH = $(PATH_0)  // restore the value of %PATH%
npp_console +  // enable output to the Console

您可以使用 Add/Modify0,并为这个 NppExec 脚本分配一个快捷键。(你需要打开 Add/Modify1,在 Add/Modify2下拉列表中选择你的脚本,按 Add/Modify3,重新启动记事本 + + ,转到记事本 + + ‘ es Add/Modify4-> Add/Modify5,选择你的脚本,点击 Add/Modify6和 Add/Modify7。我想把 F5作为我的快捷键,要做到这一点,你需要改变内置选项 Add/Modify8的快捷键为别的第一。)指向 Add/Modify9章节的链接,这些章节解释了如何保存 NppExec 的代码并分配一个快捷键: Settings0,Settings1。

使用 NppExec 插件,您可以添加 Highlight Filters(在 Console Output Filters...中找到)来突出显示某些行。我使用它来突出显示红色的错误行,为此需要添加 Highlight masks: *File "%FILE%", line %LINE%, in <*>Traceback (most recent call last):,如 这个

我的问题是,正如 copeland3300提到的,我的脚本是从记事本 + + 文件夹运行的,所以不可能找到其他项目文件,如数据库文件、模块等。我用标准的记事本 + + “ Run”命令(F5)解决了这个问题,然后输入:

cmd /k  "cd /d "$(CURRENT_DIRECTORY)" & python "$(FULL_CURRENT_PATH)""

Python 在我的 PATH 中。在脚本完成后 Cmd 窗口仍然打开。

如果有人对传递参数到 cmd.exe 和在虚拟环境中运行 python 脚本感兴趣,我将使用以下步骤:

在记事本 + +-> 运行-> 运行,我输入以下内容:

cmd /C cd $(CURRENT_DIRECTORY) && "PATH_to_.bat_file" $(FULL_CURRENT_PATH)

在这里,我 cd 到其中的目录。Py 文件存在,因此它可以访问位于。密码。

在. bat 文件中我有:

@ECHO off
set File_Path=%1


call activate Venv
python %File_Path%
pause

你可以通过 cmd 运行你的脚本,然后进入 script 目录:

cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)

我希望避免在 Notepad + + 宏中使用完整的 python 目录路径。我尝试了本页给出的其他解决方案,但都失败了。

在我电脑上工作的是:

在记事本 + + 中,按 F5。

复制/粘贴:

cmd /k cd /d "$(CURRENT_DIRECTORY)" && py -3 -i "$(FULL_CURRENT_PATH)"

进来。

我通常更喜欢在 python 本地 IDLE 交互式 shell 上运行 python 脚本,而不是从命令提示符或类似的地方。我试过了,对我很有效。打开“ Run > Run...”,然后粘贴下面的代码

python  -m idlelib.idle -r "$(FULL_CURRENT_PATH)"

之后,你可以用你的热键保存它。

必须确保在环境变量中添加并注册所需的 python。

除了有关使用系统范围内的 Python 解释器安装的许多其他答案之外,还有一个用于 Notepad + + 的 Python 插件。我用过很多次,效果很好。您甚至可以分配快捷键来运行特定的 Python 脚本。

它是开源和免费的(免费的)。

源代码和插件位于这里:
Https://github.com/bruderstein/pythonscript/