While there is nothing wrong with writing and executing python code in IDLE, python’s bundled integrated development environment, sometimes you might prefer to write in a more standard editor. This article demonstrates how to set up Notepad++ to load and run your python document into the python interpreter with the flick of a custom keystroke.
NOTE: This article is designed to complement the Python: The Basics course offered by IT Training at IU, but serves as an independent resource as well.
NOTE 2: This article is directed toward Windows users.
From the Run menu on the menu bar in Notepad++ click “Run…”
1. Copy and paste the following command into the field:
cmd /K D:\Python26\python.exe -i -c “execfile(‘$(FULL_CURRENT_PATH)’)”
2. You could just click “Run” at this point, but we anticipate we’re probably going to be using this command many times in the process of creating our python files, so we will save it to a shortcut instead.
Let’s briefly look over the components of the command above:
- cmd launches the command line
- /K is a flag or “option” that tells the command line to run the text following it, as well as to keep the command line window open after it is complete
- D:\Python26\python.exe runs the python interpreter
- -i is the option that puts the command line into the “interactive mode” of python after the code is run, which will make the workflow resemble IDLE better.
- -c is the option that allows you to run the python code in double quotes immediately following the option flags.
- “execfile(‘$(FULL_CURRENT_PATH)’)” is the python function that loads and runs the python file you are editing, while $FULL_CURRENT_PATH is Notepad++’s way of passing the file path of the file you were editing when you ran this command.
A new window should appear:
1. Call the command name anything you like, preferably something relevant like “Run in Python,” as seen in the image.
2. Choose any modifier key that you want to be part of the keystroke, and then select from the drop-down list to assign a hotkey. For example, you can choose ‘F6′ to be the key that runs the command from the previous window.
3. Hit ‘OK’
You’ll be taken back to the previous window, at which point you can either hit “Run” to test the command now, or hit “Cancel” to test it later with the keystroke you assigned it. You’re all set! Try the keystroke you just created to make sure everything works.
If you get an error finding the file stating that “there is no such file or directory,” then you may have incurred a strange but common problem associated with executing code this way. The problem occurs when any of the directories in the file path, as well as the file name itself, start with the following lower-case letters: a, b, f, n, r, t, v, or x. The reason behind this seems to be that these are reserved Microsoft escape sequences that get triggered when the file path string is being sent from Notepad++ because it finds something like ‘\a’, ‘\b’, etc.
If you didn’t quite understand why the bug occurs, then just remember that if you are going to be using lower-case letters in your folder names and file name, avoid the specific letters mentioned above.
If it worked, then great! It is now stored in the Notepad++ program to be used again whenever. However, if you are running into the folder/filename problem mentioned above and you find yourself in the position where it would be difficult or impossible to rename files and folders, because of your username, your workplace settings, or what-have-you, then read on to the next part of the article.
Creating a virtual drive to control your file paths
If your file path is causing you grief over getting this Notepad++ shortcut to work, or you simply want to organize your workflow to access your project files more quickly, then you can point a virtual drive directly to the folder containing your project files. In order to do this, we will create a simple “batch file” to script the assignment.
First, copy the path to your project folder:
Open your project folder in Windows Explorer (Windows’ built-in file browser), and click to the right of the directory path in the address bar. Then perform the Ctrl + ‘C’ keystroke to copy the path.
Open notepad and type
subst P: “<pasted file path>”
- “subst” meaning “substitute” is the command that assigns a drive letter to a virtual drive that perfectly mirrors any directory on your physical data drive (hard drive or solid state drive).
- “P:” is the letter you wish to assign your virtual drive. You can choose any letter, just make sure it is not the same as the letter assigned to an existing drive you have on your computer. “P” in this case denotes “Python” to remind us the purpose of the virtual drive.
- “<pasted file path>” is the file path you should paste in that you copied in the step above. Be sure to put the path in double quotes.
Save the file and give it any name along with a “.bat” file extension, eg, “mapPythonDrive.bat” or something similarly relevant. You may save this wherever you like, including your desktop for convenience.
Find the file on your drive, and double-click it. If necessary, click “Yes” to the User Account Control dialog box that may appear. The script will execute, and you are now done!
To confirm it worked, hold the Windows key and press ‘E’ to open up your drive listings, and you should see a new ‘P’ drive (or whatever letter you chose). Go inside, run your python file in Notepad++, and try the F6 shortcut again; this time the Notepad++ run command should work if it didn’t before.
NOTE: Keep in mind that this drive is virtual and mirrors the actual file directory on your physical drive. This means that if you save your work or create new files in this ‘P’ directory, it will also automatically save to your project directory on your physical drive. This also applies to deleting files. If you delete a file in the virtual drive, it will delete it off your hard drive or solid state drive as well.
The one problem with this fix is that every time you restart your computer, the virtual drive will disappear and you’ll have to run the batch file you created again. You could have the batch file in a convenient location, like the desktop, to run whenever you need to, or you can have the batch file run automatically every time you start Windows.
Running your custom batch file automatically when Windows starts
Let’s say you are planning on working with your python project files frequently on your computer. To avoid having to run your new batch file every time you restart Windows, you can have the file automatically run when Windows starts.
To do this, first make a shortcut for the batch file by right-clicking it and click “Create Shortcut.” Copy the shortcut, by clicking on it and hitting Ctrl + ‘C.’
Then, hit the Windows key to bring up the start menu. Click “All Programs” and locate the “Startup” folder in your programs directory. Right-click the Startup folder and click “Open.” Paste the copied shortcut for your batch file in the Startup folder.
Once you’ve done that, you’re all set! Any shortcut that is in the Startup folder will run every time Windows starts. Now when you restart Windows, your ‘P’ drive should appear, ready for you to access your project files.