简体   繁体   中英

Get current path when running Python from Notepad++

I run Python scripts in Notepad++ using this command

cmd.exe /K "C:\InstallPython\python.exe" "$(FULL_CURRENT_PATH)" 

it works, but it works not great. When I run

exec(open("raw_ticker_list.lua").read())

it doesn't see the file, but it is in the same folder where the script lies. When I run

import os
print(os.getcwd())

it prints

在此处输入图片说明

How can I make python see files in current folder?

Use this command instead:

cmd.exe /K "cd /D "$(CURRENT_DIRECTORY)" & python "$(FULL_CURRENT_PATH)""

To recap, open the "Run" menu, select the "Run" entry, and enter the above command as "The Program to Run". Possibly "Save…" it and assign a name (and keyboard shortcut) so that it permanently appears in the "Run" menu going forward.

What it does is, it opens a command window, changes the working directory to that of the script currently active in the editor (across hard drives, hence the /D parameter), then runs the Python interpreter on the script, but keeps the command window open afterwards (the /K parameter).

Use the full path to python.exe instead of just python in case it's not on the Windows search path for executables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM