Saturday, January 1, 2011

Python in Notepad++

UPDATE: It's easier to follow the advice in the comments.

Here's the command I added to Notepad++'s "settings.xml" to allow me to run python scripts from the program:

<command name="Python 3.1" ctrl="yes" alt="no" shift="no" key="116">cmd /k C:\Python31\python.exe "$(FULL_CURRENT_PATH)"</command>

cmd /k opens a command window and keeps it open so you can see the output.

There's a settings.xml in the Notepad install directory, then also one in C:\Users\(username)\AppData\Roaming\Notepad++\

I think the latter is the one where you want to toss this in, right at the end of the list of "UserDefinedCommands." This sets it to ctrl+F5, you can adjust this easily (without memorizing all the key codes) by just opening Notepad++ after the command is loaded, then Run -> Modify shortcut.

12 comments:

  1. Now I think it's "shortcuts.xml" just in the install directory... for v5.9+

    ReplyDelete
  2. Hmm, not sure that worked either.

    Now I just pull up the Run dialog, (Run -> Run... or F5), then copy and paste this in:

    cmd /k C:\Python31\python.exe "$(FULL_CURRENT_PATH)

    Then hit Save, make it something like "Python 3.1", then bind it to ctrl+f5, then you're ready to go.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. cmd /k C:\Python32\python.exe "$(FULL_CURRENT_PATH)

    for the current release

    ReplyDelete
  5. getcwd() will unfortunately treat the notepad++ directory as the current directory, not the individual script you are running.

    So you have to use this menu option from within N++:

    Plugins > NppExec > Follow $(CURRENT DIRECTORY)

    ReplyDelete
  6. Also, I use this:

    Settings > Preferences > Language Menu / Tab Settings > check "Replace by space"

    You can set this option just for Python, or make it the default for all text files in N++ by selecting [default] or Python from the list before checking the box. This will prevent python from complaining about mixing tabs and spaces.

    ReplyDelete
  7. I also add 'c:\python32\' to my PATH environment variable.

    In XP, right click on My Computer and click Properties. Click on the Advanced tab. Click the Environment Variables section. Edit the Path variable to include your current python directory (right now mine is 'C:\Python32\'). Directories are separated by semicolons; no trailing semicolon after the last entry is necessary. Do this for your system path variable, I don't think it's necessary for your user path variable.

    The path variable tells windows what directories to check when it's looking for commands and executables. Now, when I'm on the command line in any directory, I can just use 'python' as a command, and Windows will find the current python.exe.

    You have to keep this setting current when python updates, and make sure there aren't old versions in either your user variable or your system wide variable. For the longest time I would get errors that my system couldn't find python31.dll... I had set my user variable correct, but left C:\python31\ in my system path variable... watch out for that. Maybe just stick this in your system path variable, and update that, to avoid confusion.

    ReplyDelete
  8. (Setting the path variable will require a system restart to take effect on XP.)

    I imagine doing this on 7 just involves something like Win key then typing 'environment variables' or 'path' or something... not sure yet.

    ReplyDelete
  9. Tabs to spaces carried over into the upgrade to Npp v6, but I had to redo the current_working_directory fix (so that when you use get_cwd in a script it looks in the directory that script sits in, not in the npp.exe directory).

    First, reinstall NppExec:
    Plugins > Plugin Manager > Show Plugin Manager
    Check the box next to NppExec, then click "Install"

    Then:
    Plugins > NppExec > Follow $(CURRENT_DIRECTORY)

    ReplyDelete
  10. a) At least by version 3.3, the python installer includes a checkbox to automatically add the Python directory (C:\python33\, say) to the system PATH variable.

    With that set, you can safely change your Run command in Notepad++ to:
    cmd /k python "$(FULL_CURRENT_PATH)

    That way you don't have to keep updating the version directory. This might cause trouble if you've also added Python 2.7 to your PATH variable, Notepad++ might run an unexpected version of Python.

    I recommend just putting your Python 3 directory in your PATH variable, then anytime you're invoking Python 2, just use the full path to the executable.

    But if you really need both to be quickly accessible, you can rename the python.exe and pythonw.exe to python27.exe and pythonw27.exe respectively, to differentiate them from the python 3 executables.

    ReplyDelete
  11. a) At least by version 3.3, the python installer includes a checkbox to automatically add the Python directory (C:\python33\, say) to the system PATH variable.

    With that set, you can safely change your Run command in Notepad++ to:
    cmd /k python "$(FULL_CURRENT_PATH)

    That way you don't have to keep updating the version directory. This might cause trouble if you've also added Python 2.7 to your PATH variable, Notepad++ might run an unexpected version of Python.

    I recommend just putting your Python 3 directory in your PATH variable, then anytime you're invoking Python 2, just use the full path to the executable.

    But if you really need both to be quickly accessible, you can rename the python.exe and pythonw.exe to python27.exe and pythonw27.exe respectively, to differentiate them from the python 3 executables.

    ReplyDelete
  12. Everything I did for 3.4 / 2.7.4 (early April releases):
    1) uninstall each version from add/remove programs
    2) typed "path" in start menu, then "edit environment variables" in both system and account to make sure there were no lingering "pythons" set. There was a python32 directory in my system variables, I ditched it.
    3) downloaded new versions
    4) checked "add to path" in v3.3.1
    5) Notepad++, Run, used:
    cmd /k python "$(FULL_CURRENT_PATH)

    Saved as ctrl+alt+shift+F5 (will simplify after I delete the shortcut for python32, currently giving me an error.)
    got error, realized I need to reboot before path takes effect.

    ReplyDelete