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
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.
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
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
(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):
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: ):
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: ):
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
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.
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.
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:
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).
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.
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:
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:
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)
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.