我更喜欢使用Notepad++进行开发,
那么如何通过Notepad++执行Python文件呢?
打开Notepad++。在菜单中选择:运行 -> 运行.. (F5)。键入:
C:\Python26\python.exe "$(FULL_CURRENT_PATH)"
现在,不要按运行按钮,而是按保存按钮来创建一个快捷方式。py -3 -i "$(FULL_CURRENT_PATH)"
。了解py -3
命令的有用链接:https://dev59.com/8WUo5IYBdhLWcg3wmQZN - Gabriel Staples@Ramiz Uddin的回答绝对值得更多的关注:
cmd /K python "$(FULL_CURRENT_PATH)"
python "$(FULL_CURRENT_PATH)"
来工作。 - StevoisiakC:\Python27\python.exe
必须变化以反映他们自己安装的Python的位置,但第二部分-i "$(FULL_CURRENT_PATH)"
是需要按原样输入的魔法。 - Anne GunnRun
。然后你有两个选择:
Run python file in console (in Windows it is Command Prompt) with code something like this (links:
):
C:\Path\to\Python\python.exe "$(FULL_CURRENT_PATH)"
(If your console window immediately closes after running then you can add cmd /k
to your code. Links:
) This works fine, and you can even run files in interactive mode by adding
-i
to your code (links:
).
Run python program in IDLE with code something like this (links:
, in these links
C:\Path\to\Python\Lib\idlelib\idle.py
is used, but I am using C:\Path\to\Python\Lib\idlelib\idle.bat
instead, because idle.bat
sets the right current working directory automatically):
C:\Path\to\Python\Lib\idlelib\idle.bat "$(FULL_CURRENT_PATH)"
Actually, this doesn't run your program in IDLE Shell, but instead it opens your python file in IDLE Editor and then you need to click Run Module
(or click F5) to run the program. So it opens your file in IDLE Editor and then you need run it from there, which defeats the purpose of running python files from Notepad++.
But, searching online, I found option which adds '-r' to your code (links:
):
C:\Path\to\Python\Lib\idlelib\idle.bat -r "$(FULL_CURRENT_PATH)"
This will run your python program in IDLE Shell and because it is in IDLE it is by default in interactive mode.
运行
选项运行Python文件的问题是,每次运行Python文件时,都会打开新的控制台或IDLE窗口,并且会失去先前执行的所有输出。对于某些人来说,这可能并不重要,但当我开始使用Python编程时,我使用Python IDLE,所以我习惯在同一个IDLE Shell窗口中多次运行Python文件。从Notepad ++运行Python程序的另一个问题是,您需要手动保存文件,然后单击运行
(或按F5)。解决这些问题(据我所知*)需要使用Notepad ++插件。最适合从Notepad ++运行Python文件的插件是NppExec。(我还尝试过PyNPP和Python Script。PyNPP在控制台中运行Python文件,它可以工作,但您可以通过内置的运行
选项完成而无需插件,而Python Script用于运行与Notepad ++交互的脚本,因此您无法运行Python文件。)要使用NppExec插件运行Python文件,您需要转到插件-> NppExec -> 执行
,然后输入类似于this的内容(链接:C:\Path\to\Python\python.exe "$(FULL_CURRENT_PATH)"
npp_save
command之前保存Python文件,使用cd "$(CURRENT_DIRECTORY)"
command设置工作目录,或者使用-i
command在交互模式下运行Python程序。我在网上找到了许多链接(4.6.4. Running Python & wxPython
章,其中包含以下code: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(例如*我正在使用Python 3.4,所以我的目录是C:\ Python34
)。这段代码完美地工作,但是我添加了一行line到这段代码中,以便我可以多次运行Python程序而不会丢失先前的输出:
npe_console m- a+
a+
是为了启用“追加”模式,它会保留先前的控制台文本并不清除。
m-
关闭控制台内部消息(这些消息是绿色的)。
我在NppExec的执行窗口中使用的最终代码是:
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
关联脚本
下拉列表中选择您的脚本,按添加/修改
,重新启动Notepad++,转到Notepad++的设置
-> 快捷键映射器
-> 插件命令
,选择您的脚本,单击修改
并分配一个快捷键。 我想将F5
作为我的快捷键,为此,您需要先将内置选项运行
的快捷键更改为其他内容。 链接到NppExec手册中解释如何保存NppExec代码和分配快捷键的章节:NppExec的“执行…”
,NppExec脚本
。
P.S.*:使用NppExec插件,您可以添加高亮过滤器
(在控制台输出过滤器...
中找到),以突出显示某些行。我用它来以红色突出显示错误行,为此您需要添加高亮掩码
:*File“%FILE%”,line%LINE%,in <*>
和Traceback(most recent call last):
像这样。
首先从 https://www.python.org/downloads/ 下载Python并安装。
运行安装程序。
**重要:** 确保你勾选了以下两项:
点击“立即安装”并完成安装。
打开Notepad++并从插件管理器中安装插件PyNPP。我使用的是N++ 6.9.2版本。
保存一个新文件并命名为new.py。
在Notepad++中输入代码。
import sys
print("Hello from Python!")
print("Your Python version is: " + sys.version)
按下Alt+Shift+F5即可。
就这么简单。
在菜单中选择:"运行" --> "运行..."(或者直接按下 F5)。
py -2 -i "$(FULL_CURRENT_PATH)"
py -3 -i "$(FULL_CURRENT_PATH)"
为了更好地理解py
命令:
py -h
了解py
命令的另一个有用链接:如何在Windows 7中运行Python 2和3?
感谢Reshure的回答,让我找到正确的解决方案。
CTRL + ALT + P
快捷键。 - akinuri所有与“运行-> 运行”菜单选项有关的答案都使用cmd的“/K”开关,这样终端会保持打开状态,或者对于python.exe是“-i”,以便强制启用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中无条件执行下一条命令的符号——不管上一条命令是否成功执行都会继续执行下一条命令(&&
是“与”——只有在上一条成功执行后才会执行下一条,||
是“或”)。
pause
会输出“按任意键继续. . .”,然后等待你按下任何键(这个输出可以被抑制)。
exit
会为你输入“exit”以退出命令行界面。
所以最终,cmd
会运行python.exe
来执行当前的文件,并保持窗口打开状态,pause
等待你按下任意键,最后exit
关闭窗口。
之前提出的解决方案都对我没有用,需要进行小的修改。
在 Notepad++ 中按下 F5 键后,输入:
cmd /k "C:\Python27\python.exe $(FULL_CURRENT_PATH)"
我使用NPP_Exec插件(在插件管理器中找到)。 安装完成后,打开控制台窗口(ctrl + ~)并输入:
cmd
C:\Program Files\Notepad++> **python "$(FULL_CURRENT_PATH)"**
执行当前正在处理的文件。