问题:如何在记事本++中执行Python文件?
我更喜欢使用记事本++进行开发,
如何通过Notepad ++在Python中执行文件?
I prefer using Notepad ++ for developing,
How do I execute the files in Python through Notepad++?
回答 0
第一种选择:(最简单,推荐)
打开记事本++。在菜单上转到:运行->运行。(F5)。输入:
C:\Python26\python.exe "$(FULL_CURRENT_PATH)"
现在,不用按“运行”,而是按“保存”为其创建快捷方式。
笔记
- 如果您拥有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”,如果该密钥存在,它将从该密钥的第一个子密钥获取路径。
检查此密钥是否存在,如果不存在,则可以尝试创建它。
First option: (Easiest, recommended)
Open Notepad++. On the menu go to: Run -> Run.. (F5). Type in:
C:\Python26\python.exe "$(FULL_CURRENT_PATH)"
Now, instead of pressing run, press save to create a shortcut for it.
Notes
- If you have Python 3.1: type in
Python31
instead of Python26
- Add
-i
if you want the command line window to stay open after the script has finished
Second option
Use a batch script that runs the Python script and then create a shortcut to that from Notepad++.
As explained here: http://it-ride.blogspot.com/2009/08/notepad-and-python.html
Third option: (Not safe)
The code opens “HKEY_CURRENT_USER\Software\Python\PythonCore”, if the key exists it will get the path from the first child key of this key.
Check if this key exists, and if does not, you could try creating it.
回答 1
@ Ramiz Uddin的答案绝对值得更多关注:
- 打开记事本++
- 在菜单上转到:运行 → 运行。(F5)
- 输入:
cmd /K python "$(FULL_CURRENT_PATH)"
@Ramiz Uddin’s answer definitely deserves more visibility :
- Open Notepad++
- On the menu go to: Run → Run.. (F5)
- Type in:
cmd /K python "$(FULL_CURRENT_PATH)"
回答 2
这是为我工作的东西:
打开记事本++,然后按F5。您会看到一个小弹出框:
输入:C:\ Python27 \ python.exe -i“ $(FULL_CURRENT_PATH)”对于Python 2.7。
然后另存为…,并选择您自己的键组合,以在每次您要运行某些内容时启动它
Here is what’s worked for me:
Open notepad++ and press F5. You’ll get a little popup box:
Type: C:\Python27\python.exe -i “$(FULL_CURRENT_PATH)” for Python 2.7.
and then Save As…, and pick your own key combo to start it each time you want to run something
回答 3
在菜单上转到:“运行”->“运行…”(或按F5)。
对于Python 2,输入:
py -2 -i "$(FULL_CURRENT_PATH)"
对于Python 3,输入:
py -3 -i "$(FULL_CURRENT_PATH)"
参考文献:
为了py
更好地理解命令:
py -h
另一个有助于理解该py
命令的链接:如何在Windows 7中运行python 2和3?
感谢Reshure的回答,这使我走上了正确的道路以解决这一问题。
On the menu go to: “Run” –> “Run…” (or just press F5).
For Python 2 type in:
py -2 -i "$(FULL_CURRENT_PATH)"
For Python 3 type in:
py -3 -i "$(FULL_CURRENT_PATH)"
References:
To understand the py
command better:
py -h
Another helpful link to understand the py
command: How do I run python 2 and 3 in windows 7?
Thanks to Reshure for his answer that got me on the right track to figure this out.
回答 4
首先从 https://www.python.org/downloads/ 安装Python
运行安装程序
** 重要提示 **请确保同时选中以下两项:
- 为所有用户安装启动器
- 将Python 3.6添加到路径
单击立即安装并完成安装。
打开notepad ++,然后从插件管理器安装插件PyNPP。我正在使用N ++ 6.9.2
将新文件另存为new.py
输入N ++
import sys
print("Hello from Python!")
print("Your Python version is: " + sys.version)
按Alt + Shift + F5
就那么简单。
First install Python from https://www.python.org/downloads/
Run the installer
** IMPORTANT ** Be sure you check both :
- Install launcher for all users
- Add Python 3.6 to path
Click install now and finish the installation.
Open notepad++ and install plugin PyNPP from Plugin Manager. I’m using N++ 6.9.2
Save a new file as new.py
Type in N++
import sys
print("Hello from Python!")
print("Your Python version is: " + sys.version)
Press Alt+Shift+F5
Simple as that.
回答 5
“运行”->“运行”菜单选项的所有答案都与cmd的“ / K”开关一起使用,因此终端保持打开状态,对于python.exe则为“ -i”,因此python强制采用交互模式-两者都为您保留输出观察。
然而,在cmd /k
你必须输入exit
将其关闭,在python -i
– quit()
。如果您喜欢输入太多内容(对我来说肯定是:),则要使用的“运行”命令为
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
最终关闭窗口。
All the answers for the Run->Run menu option go with the “/K” switch of cmd, so the terminal stays open, or “-i” for python.exe so python forces interactive mode – both to preserve the output for you to observe.
Yet in cmd /k
you have to type exit
to close it, in the python -i
– quit()
. If that is too much typing for your liking (for me it sure is :), the Run command to use is
cmd /k C:\Python27\python.exe "$(FULL_CURRENT_PATH)" & pause & exit
C:\Python27\python.exe
– obviously the full path to your python install (or just python
if you want to go with the first executable in your user’s path).
&
is unconditional execution of the next command in Windows – unconditional as it runs regardless of the RC of the previous command (&&
is “and” – run only if the previous completed successfully, ||
– is “or”).
pause
– prints “Press any key to continue . . .” and waits for any key (that output can be suppressed if need).
exit
– well, types the exit for you :)
So at the end, cmd
runs python.exe
which executes the current file and keeps the window opened, pause
waits for you to press any key, and exit
finally close the window once you press that any key.
回答 6
I also wanted to run python files directly from Notepad++. Most common option found online is using builtin option Run
. Then you have two options:
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.
Problem with running your python files via builtin Run
option is that each time you run your python file, you open new console or IDLE window and lose all output from previous executions. This might not be important to some, but when I started to program in python, I used Python IDLE, so I got used to running python file multiple times in same IDLE Shell window. Also problem with running python programs from Notepad++ is that you need to manually save your file and then click Run
(or press F5). To solve these problems (AFAIK*) you need to use Notepad++ Plugins. The best plugin for running python files from Notepad++ is NppExec. (I also tried PyNPP and Python Script. PyNPP runs python files in console, it works, but you can do that without plugin via builtin Run
option and Python Script is used for running scripts that interact with Notepad++ so you can’t run your python files.) To run your python file with NppExec plugin you need to go to Plugins -> NppExec -> Execute
and then type in something like this (links: ):
C:\Path\to\Python\python.exe "$(FULL_CURRENT_PATH)"
With NppExec you can also save your python file before run with npp_save
command, set working directory with cd "$(CURRENT_DIRECTORY)"
command or run python program in interactive mode with -i
command. I found many links ( ) online that mention these options, but best use of NppExec to run python programs I found at NppExec’s Manual which has chapter 4.6.4. Running Python & wxPython
with this 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
All you need to do is copy this code and change your python directory if you use some other python version (e.g.* I am using python 3.4 so my directory is C:\Python34
). This code works perfectly, but there is one line I added to this code so I can run python program multiple times without loosing previous output:
npe_console m- a+
a+
is to enable the “append” mode which keeps the previous Console’s text and does not clear it.
m-
turns off console’s internal messages (those are in green color)
The final code that I use in NppExec’s Execute window is:
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
You can save your NppExec’s code, and assign a shortcut key to this NppExec’s script. (You need to open Advanced options of NppExec’s plugin, select your script in the drop-down list, press the , restart Notepad++ , go to Notepad++’es -> , select your script, click and assign a shortcut key. I wanted to put F5
as my shortcut key, to do that you need to change shortcut key for builtin option Run
to something else first.) Links to chapters from NppExec’s Manual that explain how to save you NppExec’s code and assign a shortcut key: NppExec's "Execute..."
, NppExec's script
.
P.S.*: With NppExec plugin you can add Highlight Filters
(found in Console Output Filters...
) that highlight certain lines. I use it to highlight error lines in red, to do that you need to add Highlight masks
: *File "%FILE%", line %LINE%, in <*>
and Traceback (most recent call last):
like this.
回答 7
先前提出的解决方案均不适合我。需要稍作修改。
在Notepad ++中按F5后,键入:
cmd /k "C:\Python27\python.exe $(FULL_CURRENT_PATH)"
命令提示符保持打开状态,因此您可以查看脚本的输出。
None of the previously proposed solutions worked for me. Slight modification needed.
After hitting F5 in Notepad++, type:
cmd /k "C:\Python27\python.exe $(FULL_CURRENT_PATH)"
The command prompt stays open so you can see the output of your script.
回答 8
我使用NPP_Exec插件(在插件管理器中找到)。安装完成后,打开控制台窗口(ctrl +〜)并输入:
cmd
这将启动命令提示符。然后输入:
C:\Program Files\Notepad++> **python "$(FULL_CURRENT_PATH)"**
执行您正在使用的当前文件。
I use the NPP_Exec plugin (Found in the plugins manager). Once that is installed, open the console window (ctrl+~) and type:
cmd
This will launch command prompt. Then type:
C:\Program Files\Notepad++> **python "$(FULL_CURRENT_PATH)"**
to execute the current file you are working with.
回答 9
我希望这里的人们会发布一些步骤,而不仅仅是整体概念。我最终得到了cmd / k版本的支持。
分步说明是:
- 在NPP中,单击菜单项:运行
- 在子菜单中,单击:运行
- 在“运行…”对话框中的“要运行的程序”字段中,删除所有现有文本并键入:cmd / K“ $(FULL_CURRENT_PATH)” / K是可选的,它将保持打开脚本运行时创建的窗口,如果您想要的话。
- 点击保存…按钮。
- 快捷方式对话框打开;如果您想要键盘快捷键,请填写它(有一条注释说“这将禁用加速器”,无论如何,所以也许您不想使用键盘快捷键,尽管在您分配键盘快捷键时不会有任何伤害不需要加速器)。我认为您必须告诉NPP Python.exe文件在哪里(例如,对我来说:C:\ Python33 \ python.exe)。我不知道您在何处或如何执行此操作,但是在这里尝试各种操作时,我能够做到这一点-我不记得哪个尝试成功了。
I wish people here would post steps instead of just overall concepts. I eventually got the cmd /k version to work.
The step-by-step instructions are:
- In NPP, click on the menu item: Run
- In the submenu, click on: Run
- In the Run… dialog box, in the field The Program to Run, delete any existing text and type in: cmd /K “$(FULL_CURRENT_PATH)” The /K is optional, it keeps open the window created when the script runs, if you want that.
- Hit the Save… button.
- The Shortcut dialogue box opens; fill it out if you want a keyboard shortcut (there’s a note saying “This will disable the accelerator” whatever that is, so maybe you don’t want to use the keyboard shortcut, though it probably doesn’t hurt to assign one when you don’t need an accelerator). Somewhere I think you have to tell NPP where the Python.exe file is (e.g., for me: C:\Python33\python.exe). I don’t know where or how you do this, but in trying various things here, I was able to do that–I don’t recall which attempt did the trick.
回答 10
这里没有答案,或者我找到的插件提供了我想要的。启动我在Notepad ++上编写的python代码的极简方法,方法是按下快捷键,最好没有插件。
我有适用于Windows 8.1 x86_64和Notepad ++ 32位的Python 3.6(64位)。在Notepad ++中编写Python脚本并将其保存后,请按F5键Run
。然后写:
"C:\Path\to\Python\python.exe" -i "$(FULL_CURRENT_PATH)"
然后点击运行按钮。该i
标志强制终端在代码执行终止后保持静止,以供您检查。此命令将在cmd终端中启动脚本,并且该终端仍将位于此处,直到您通过键入将其关闭exit()
。
您可以将其保存到快捷方式中以方便使用(我的是CTRL + SHIFT + P)。
No answer here, or plugin i found provided what i wanted. A minimalist method to launch my python code i wrote on Notepad++ with the press of a shortcut, with preferably no plugins.
I have Python 3.6 (64-bit), for Windows 8.1 x86_64 and Notepad++ 32bit. After you write your Python script in Notepad++ and save it, Hit F5 for Run
. Then write:
"C:\Path\to\Python\python.exe" -i "$(FULL_CURRENT_PATH)"
and hit the Run button. The i
flag forces the terminal to stay still after code execution has terminated, for you to inspect it. This command will launch the script in a cmd terminal and the terminal will still lie there, until you close it by typing exit()
.
You can save this to a shortcut for convenience (mine is CTRL + SHIFT + P).
回答 11
上述解决方案中没有解决一个问题。Python将当前工作目录设置为您从其启动解释器的任何位置。如果您需要当前工作目录与保存文件的目录相同,则可以按F5键并输入以下内容:
cmd /K cd "$(CURRENT_DIRECTORY)"&C:\Users\username\Python36-32\python.exe -i "$(FULL_CURRENT_PATH)"
除非您将C:\ Users \ username \ Python36-32 \ python.exe替换为计算机上python解释器的路径。
基本上,您是在启动命令行,将目录更改为包含要运行的.py文件的目录,然后再运行它。您可以使用’&’符号将任意多个命令行命令串在一起。
There is one issue that I didn’t see resolved in the above solutions. Python sets the current working directory to wherever you start the interpreter from. If you need the current working directory to be the same directory as where you saved the file on, then you could hit F5 and type this:
cmd /K cd "$(CURRENT_DIRECTORY)"&C:\Users\username\Python36-32\python.exe -i "$(FULL_CURRENT_PATH)"
Except you would replace C:\Users\username\Python36-32\python.exe with whatever the path to the python interpreter is on your machine.
Basically you’re starting up command line, changing the directory to the directory containing the .py file you’re trying to run, and then running it. You can string together as many command line commands as you like with the ‘&’ symbol.
回答 12
我的问题是,如copeland3300所述,我的脚本从notepad ++文件夹运行,因此无法找到其他项目文件,例如数据库文件,模块等。我使用标准notepad ++“运行”命令解决了该问题( F5)并输入:
cmd /k "cd /d "$(CURRENT_DIRECTORY)" & python "$(FULL_CURRENT_PATH)""
Python在我的PATH中。脚本完成后,Cmd窗口保持打开状态。
My problem was, as it was mentioned by copeland3300, that my script is running from notepad++ folder, so it was impossible to locate other project files, such as database file, modules etc. I solved the problem using standard notepad++ “Run” command (F5) and typing in:
cmd /k "cd /d "$(CURRENT_DIRECTORY)" & python "$(FULL_CURRENT_PATH)""
Python WAS in my PATH. Cmd window stayed open after script finished.
回答 13
扩展Reshure的答案
打开运行→运行…从记事本+ +的菜单栏(快捷键:F5)
在给定的空间中,输入:
"$(FULL_CURRENT_PATH)" -1
点击运行
da!
Extending Reshure’s answer
Open Run → Run… from the menubar in Notepad++ (shortcut: F5)
In the given space, enter:
"$(FULL_CURRENT_PATH)" -1
Click Run
ta da!
回答 14
我想避免在Notepad ++宏中使用完整的python目录路径。我尝试了此页面中提供的其他解决方案,但均失败了。
在我的PC上运行的是:
在记事本++中,按F5。
复制/粘贴此:
cmd /k cd /d $(CURRENT_DIRECTORY) && py -3 -i $(FULL_CURRENT_PATH)
输入。
I would like to avoid using full python directory path in the Notepad++ macro. I tried other solutions given in this page, they failed.
The one working on my PC is:
In Notepad++, press F5.
Copy/paste this:
cmd /k cd /d $(CURRENT_DIRECTORY) && py -3 -i $(FULL_CURRENT_PATH)
Enter.
回答 15
I started using Notepad++ for Python very recently and I found this method very easy. Once you are ready to run the code,right-click on the tab of your code in Notepad++ window and select “Open Containing Folder in cmd”. This will open the Command Prompt into the folder where the current program is stored. All you need to do now is to execute:
python
This was done on Notepad++ (Build 10 Jan 2015).
I can’t add the screenshots, so here’s a blog post with the screenshots – http://coder-decoder.blogspot.in/2015/03/using-notepad-in-windows-to-edit-and.html
回答 16
In Notepad++, go to Run → Run…, select the path and idle.py
file of your Python installation:
C:\Python27\Lib\idlelib\idle.py
add a space and this:
"$(FULL_CURRENT_PATH)"
and here you are!
Video demostration:
https://www.youtube.com/watch?v=sJipYE1JT38
回答 17
如果有人有兴趣将参数传递给cmd.exe并在虚拟环境中运行python脚本,这些是我使用的步骤:
在Notepad ++-> Run-> Run上,输入以下内容:
cmd /C cd $(CURRENT_DIRECTORY) && "PATH_to_.bat_file" $(FULL_CURRENT_PATH)
在这里,我进入.py文件所在的目录,以便可以访问.py代码目录中的所有其他相关文件。
在.bat文件中,我有:
@ECHO off
set File_Path=%1
call activate Venv
python %File_Path%
pause
In case someone is interested in passing arguments to cmd.exe and running the python script in a Virtual Environment, these are the steps I used:
On the Notepad++ -> Run -> Run , I enter the following:
cmd /C cd $(CURRENT_DIRECTORY) && "PATH_to_.bat_file" $(FULL_CURRENT_PATH)
Here I cd into the directory in which the .py file exists, so that it enables accessing any other relevant files which are in the directory of the .py code.
And on the .bat file I have:
@ECHO off
set File_Path=%1
call activate Venv
python %File_Path%
pause
回答 18
您可以通过cmd运行脚本,并位于脚本目录中:
cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)
You can run your script via cmd and be in script-directory:
cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)
回答 19
我通常更喜欢在python本地IDLE交互式shell上运行我的python脚本,而不是从命令提示符下运行。我已经尝试过了,它对我有用。只需打开“运行>运行…”,然后粘贴以下代码
python -m idlelib.idle -r "$(FULL_CURRENT_PATH)"
之后,您可以使用热键保存它。
您必须确保已添加所需的python并将其注册到环境变量中。
I usually prefer running my python scripts on python native IDLE interactive shell rather than from command prompt or something like that. I’ve tried it, and it works for me. Just open “Run > Run…”, then paste the code below
python -m idlelib.idle -r "$(FULL_CURRENT_PATH)"
After that, you can save it with your hotkey.
You must ensure your desired python is added and registered in your environment variables.
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。