https://medium.com/never-too-late-to-machine-learn/how-to-step-by-step-setup-notepad-to-run-python-scripts-e1ce3b3ac7fe
I am reading from this tutorial linked above.
What I am doing is trying to run python for the first time on Notepad++. I've downloaded python, etc.
What I am lost on is this:
"Just copy the location of the python exe file, and let’s go back to Notepad++. The execute screen is waiting for us.
In the execute screen, paste the location of Python followed with “$(FILE_NAME)” "
What is the execute screen, where am I pasting the location to?
I hope someone can help me. Thank you.
first, you need "$(FULL_CURRENT_PATH)" for full path of file, the $(FILE_NAME) is for file name only or relative path
then you can paste like
C:\Python39\python.exe "$(FULL_CURRENT_PATH)"
for execute screen, new name for the menu is Execute NppExec Script.. see image below
to run your python script in notepad++ is quite simple:
make sure your python is correctly installed, open your console and type python, you should see something similar to the following (it should say Python X.Y.Z accordingly to the version you have installed)
now that we know that python is correctly installed, go to notepad++, open on it your desire script and go to the Run menu and select Run...
enter the following command python -i "$(FULL_CURRENT_PATH)" and press Run
You should see the following
And that is all.
-
Now that the previous worked, lets make it more reusable, repeat step 1 and 2, but instead of Run press Save..., give it a name (like python for example) and if you so desire also a keyboard binding (a key combination to press to run this command)
Now in step 1, you can pick python (or whatever you name it) instead or "Run..." to run you script (or press you key combination if you give it one)
now some additional explanation about the previous command python -i "$(FULL_CURRENT_PATH)"
the -i option is to enter in interactive mode, that is run you script and then continue executing python so you can do more python stuff with everything for your script loaded in there too, that way the console doesn't just close intermediately after your script is done running.
$(FULL_CURRENT_PATH) is a command for notepad++ to said to substitute there for the full path of your given script
Alternative command: cmd /K CD "$(CURRENT_DIRECTORY)" & python -i "$(FILE_NAME)"
This command is the equivalent to opening your console (cmd /K), move with cd to the folder where your script is (CD "$(CURRENT_DIRECTORY)") and then run your script with python in interactive mode (python -i "$(FILE_NAME)") and & is to separate both instructions (the /K is like python's -i but for the console) (the "-i" is now optional, but I prefer it with it to test other stuff in the script without need to put it on the script because doing so make little sense...)
Why you might want to use this over the other? two reason:
the first is when dealing files so you don't have to type the full path of a given file that sit next to your script so you can use just the name of said file, aka set the working directory to that where the script is located, otherwise it will be that where notepad++ is located (usually it might be like "C:\Program Files\Notepad++").
In case of an error, the windows will remain open so you can know what was the error instead of closing abruptly.
This question already has answers here:
Executing Python with Gvim
(4 answers)
Closed 9 years ago.
Is it possible to run Python code from within the vim editor?
What is necessary to install the support along with Python syntax highlighting?
How would I install "python.vim : Enhanced version of the python syntax highlighting script" ?
I did not automatically create ~/.vim/syntax and I'm using a Mac, all I downloaded was the .app file, an executable that I don't know of its purpose and a readme file.
I've tried also creating a folder for the python.vim file, but that didn't work out either.
Personally:
When inside Vim editing my Python scripts, I simply hit CtrlZ so as to return in console mode.
Run my script with command $ python my_script.py.
When done, I enter $ fg in the command line and that gets me back inside Vim, in the state I was before hitting CtrlZ. (fg as in foreground)
Edit
Recently I have started using the :terminal mode of vim much more frequently.
I tend to prefer it to CtrlZZ because it may happen that I forget that I used Ctrl-z and open an additional vim session: it may become messy. Also, having a terminal pane is easier for dealing with line number in errors message, since the two views are available at the same time.
So the workflow I'm using nowadays has become:
:terminal (in my case I have a vim mapping with leader key) <leader>tm :terminal<cr> so that I don't even type :terminal manually)
Run my script with command $ python my_script.py.
$ exit in the bash command line if I want to close the terminal pane
You don't have to install any plugins to get syntax highlighting for any version of Python in Vim. The python-syntax plugin might have more features, but it's absolutely not needed and not important if you're new to Vim and Python. To enable syntax highlighting add this to your ~/.vimrc:
filetype plugin on
syntax on
Adding to Kent's answer you can also send arguments to the script you're running if you want to.
[args] is something you'd normally add after python script.py.
:w !python - [args]
Personally I prefer to have a seperate shell open in Tmux for running my scripts, and possibly play around with bpython. If I'm not using a Tmux session :sh works fine too, giving you a normal shell. You can get back to vim by doing exit or ctrld.
As for using Python with vim autocomplete is a big part of it, and I can recommend jedi-vim for this. If you want error checking/magic Syntastic is the tool for the job.
This might be a little overwhelming if you're new to both Vim and Python, but I suggest you take it as it comes, step by step. The first step for learning Vim is to do the Vimtutor. Run it by entering vimtutor in a shell.
If you are asking how to run Python code through vim, try this:
EDIT
thank #Zyx for pointing out the problems in my original line, I leave this line here, in order to let reader know where are the problems.
nmap <F9> :!python %<cr>
you could create this au (I saved has("autocmd") part...) in your vimrc:
autocmd FileType python nnoremap <buffer> <F9> :exec '!python' shellescape(#%, 1)<cr>
put this in your .vimrc, then after you editing your .py file, press <f9> vim will try to compile and execute it (via external python interpreter).
how to install that plugin:
https://github.com/hdima/python-syntax#how-to-install
I'd like to write cross platform Python scripts that are GUI frontends for command line programs. The problem is I know a few Mac users who think that using the terminal will have the same effect as throwing their computer off the top of a skyscraper. In Linux and Windows it's easy enough to setup a Python script so the user can double click an icon and the script will start without opening any extra windows. Is there an easy way to do this with OS-X? Would the user have to install a different Python than the one that comes with OS-X? I haven't been able to find a definitive answer.
You might want to look at Platypus. It's a freeware app for generating apps which wrap scripts.
Another way to do something like that is using Automator or even AppleScript Editor. Either can produce an application which just runs a script.
Update:
For Automator: Launch Automator, select the Application template, type "script" in the search field, double-click Run Shell Script, switch the shell pop-up menu to /usr/bin/python, type/paste your Python script into the text field. Or, leave the pop-menu on /bin/bash and just write an invocation of an external script in the text field. Save as an application.
You can also view help from its Help menu.
For AppleScript, launch AppleScript Editor, type the following as the script:
do shell script "/usr/bin/true"
Replace /usr/bin/true with the path to whatever script you like. Save as an application.
Again, there's help in the Help menu.
py2app does this with aplomb. You make your Python script, use whatever dependencies you need (wx, Tkinter, etc.) and py2app makes you a standalone app bundle that will run in any modern OS X environment. It bundles Python too, so you can use any Python you want (not just the system default).
The downside is that the generated apps might be large, up to 50MB if you have a lot of dependencies (though that is somewhat of an extreme).
There are two ways to do this:
Click on a script.
Press command-i to open the "get info" window.
Expand the "Open With" section (if it isn't already).
Choose "Python Launcher" from the drop-down menu.
Click "Change All" if you would like ALL Python scripts to launch when double clicked.
Possibly open Python Launcher and uncheck "Run in a Terminal window"
This will work for this machine only, so it is less portable than the following. Why? Because the default for opening a document type varies depending on what is installed (XCode and/or IDLE will both take over opening a .py file).
Method Two:
Validate the Interpreter Directive, that's the first line of the file. I suggest using /usr/bin/env python3. This will run the first python3 interpreter that is on the users path.
Make the script executable chmod a+x <script_name> from the Terminal.
Change the extension from .py to .command (this will be opened by the Terminal).
Use zip or tar for distribution so that the permissions do not get mangled.
This method will open a Terminal window, but when the Python window is closed the terminal window will also close.
If your script has dependencies outside of the standard library, then you should provide a second .command file to install those. This may make things more complicated, but using pip3 install --user <list of dependencies> should minimize complications.
This obviously an extremely novice question, but I've installed Python 2.7 and started reading the manual. However I looked and looked, and couldn't understand how to start programming a file rather than writing in interactive mode. One book that was online suggested quit(), which surprise -- quit the program.
Should coding be done in a different program? I'm using IDLE (Python GUI). Can coding not be done within that program?
Yes, coding should be done in a different program. The interactive shell is very useful but it's not an editor.
You write Python code line by line (as you would on Python interactive mode) in a text editor such as vim, emacs etc...
Then you run these line by line code using the Python interpreter by giving it the name of your script.
$ python myscript.py
I like to use a different directory for each project. Suppose I decide to use W:/mytest as my directory. First I create the directory.
Then I start Idle. I type the following:
import os
os.chdir("W:/mytest")
This makes W:/mytest the current directory for Idle.
import sys
sys.path.append(".")
This changes the path so that when I "import", it will look in the current directory.
Next I do File / New Window to open an editor window, and in that new window I select File / Save As. It starts in the Python home directory so I have to navigate to W:/mytest. I save this (empty) file as "test1.py".
I type this into my test1.py file and save it again:
""" test1.py is my test
"""
print ("This is test1.")
class Test1:
def __init__(self):
print ("Constructed")
This is a contrived example that can be run as a script or imported as a module.
So I have two windows now; an editor window and the Idle "Python Shell". I can do this in the Python Shell:
>>> execfile("test1.py")
This is test1.
>>> import test1
This is test1
>>> tt = test1.Test1()
Constructed
Push new to start making your own script file. Then when you are ready to test click run and then you can watch the results in the interactive mode, and even try new things as if you were adding code to the end of your script file, its a very useful app for debugging, testing and trying new things.
Also in the options you can change the way python opens your scripts when you click edit from windows, you can set it so that it opens the interactive shell or just the editor.
use new window tool in the file icon,in the python idle itself to write a program
To start coding in a file, just open a new file and start typing.
I prefer using Notepad++ for developing,
How do I execute the files in Python through Notepad++?
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.
#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)"
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
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 Associated script drop-down list, press the Add/Modify, restart Notepad++ , go to Notepad++'es Settings -> Shortcut Mapper -> Plugin commands, select your script, click Modify 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.
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.
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.
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.
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.
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.
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.
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.
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).
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.
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.
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!
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
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
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
You can run your script via cmd and be in script-directory:
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.
In addition to the many other answers about using a system-wide installation of a Python interpreter, there is also a Python plugin for Notepad++. I've used it many times, and it works quite well. You can even assign shortcut keys to run specific Python scripts.
It is open-source and gratis (free of charge).
The source code and plugin are located here:
https://github.com/bruderstein/PythonScript/