Programming

메모장에서 파이썬 파일을 실행하는 방법 ++?

procodes 2020. 7. 28. 22:00
반응형

메모장에서 파이썬 파일을 실행하는 방법 ++?


개발에 메모장 ++를 사용하는 것을 선호합니다.

메모장 ++를 통해 Python에서 파일을 어떻게 실행합니까?


첫 번째 옵션 : (가장 쉬움, 권장)

메모장 ++을 엽니 다. 메뉴에서 실행-> 실행. (F5)으로 이동하십시오. 입력 :

C:\Python26\python.exe "$(FULL_CURRENT_PATH)"

이제 실행을 누르는 대신 저장을 눌러 바로 가기를 만듭니다.

노트

  • Python 3.1을 사용하는 경우 Python31대신에 입력하십시오Python26
  • -i스크립트가 끝난 후에 명령 행 창을 열어 두려면 추가하십시오.

두 번째 옵션

Python 스크립트를 실행하는 배치 스크립트를 사용하고 메모장 ++에서 바로 가기를 작성하십시오.

여기에 설명 된대로 : http://it-ride.blogspot.com/2009/08/notepad-and-python.html


세 번째 옵션 : (안전하지 않음)

코드는“HKEY_CURRENT_USER \ Software \ Python \ PythonCore”를 엽니 다. 키가 있으면이 키의 첫 번째 자식 키에서 경로를 가져옵니다.

이 키가 존재하는지 확인하고 존재하지 않는 경우 작성해보십시오.


@ Ramiz Uddin의 대답은 분명히 더 많은 가시성을 가질 가치가 있습니다.

  • 메모장 열기 ++
  • 메뉴에서 실행실행으로 이동 하십시오. (F5)
  • 입력 : cmd /K python "$(FULL_CURRENT_PATH)"

나를 위해 일한 것은 다음과 같습니다.

notepad ++를 열고 F5를 누릅니다. 작은 팝업 상자가 나타납니다.

실행할 프로그램을 입력하기위한 팝업 상자

: 입력 C를 : \ Python27 \ python.exe를 -i "$ (FULL_CURRENT_PATH)" 파이썬 2.7.

그런 다음 다른 이름으로 저장 ... 을 클릭하고 원하는 키 조합을 선택하여 무언가를 실행할 때마다 시작하십시오.


먼저 https://www.python.org/downloads/ 에서 Python을 설치 하십시오.

인스톨러를 실행합니다

** 중요 ** 다음 사항을 모두 확인하십시오.

  • 모든 사용자를위한 런처 설치
  • 경로에 Python 3.6 추가

지금 설치를 클릭하고 설치를 완료하십시오.

notepad ++를 열고 플러그인 관리자에서 플러그인 PyNPP설치하십시오 . N ++ 6.9.2를 사용하고 있습니다.

새 파일을 new.py로 저장

N ++로 입력

import sys

print("Hello from Python!")
print("Your Python version is: " + sys.version) 

Alt + Shift + F5를 누릅니다.

그렇게 간단합니다.


메뉴에서 "Run"-> "Run ..."으로 이동하십시오 (또는을 누르십시오 F5).

Python 2의 경우 다음을 입력하십시오.

py -2 -i "$(FULL_CURRENT_PATH)"

Python 3의 경우 다음을 입력하십시오.

py -3 -i "$(FULL_CURRENT_PATH)"

참고 문헌 :

py명령을 더 잘 이해하려면 다음을 수행하십시오.

py -h

py명령 을 이해하는 또 다른 유용한 링크 : Windows 7에서 Python 2와 3을 어떻게 실행합니까?

Reshure에게 감사의 말을 합니다.


Run-> Run 메뉴 옵션에 대한 모든 대답은 cmd의 "/ K"스위치와 함께 사용되므로 터미널은 열린 상태로 유지되거나 python.exe의 경우 "-i"는 대화식 모드를 강제 실행합니다. 관찰하다.

그러나 - 에서 닫으 cmd /k려면 입력 exit해야합니다 . 그것이 원하는대로 너무 많이 타이핑하는 경우 (확실히 :), 사용할 실행 명령은 다음과 같습니다.python -iquit()

cmd /k C:\Python27\python.exe  "$(FULL_CURRENT_PATH)" & pause & exit

C:\Python27\python.exe-분명히 파이썬 설치의 전체 경로 (또는 python사용자 경로의 첫 번째 실행 파일로 이동하려는 경우).

&Windows에서 다음 명령의 무조건 실행입니다. 이전 명령의 RC와 상관없이 실행되므로 무조건적 &&입니다 ( "and"-이전이 성공적으로 완료된 경우에만 실행 됨 ||- "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.


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.


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 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:

  1. In NPP, click on the menu item: Run
  2. In the submenu, click on: Run
  3. 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.
  4. Hit the Save... button.
  5. 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.

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:

  1. 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: 여기에 이미지 설명을 입력하십시오 여기에 이미지 설명을 입력하십시오 여기에 이미지 설명을 입력하십시오 여기에 이미지 설명을 입력하십시오 여기에 이미지 설명을 입력하십시오 여기에 이미지 설명을 입력하십시오 여기에 이미지 설명을 입력하십시오 여기에 이미지 설명을 입력하십시오).

  2. 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.


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


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

  1. Open Run → Run... from the menubar in Notepad++ (shortcut: F5)

  2. In the given space, enter:

    "$(FULL_CURRENT_PATH)"  -1
    
  3. Click Run

ta da!


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.


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


누군가 cmd.exe에 인수를 전달하고 가상 환경에서 python 스크립트를 실행하는 데 관심이있는 경우 다음 단계를 수행했습니다.

메모장 ++-> 실행-> 실행에서 다음을 입력하십시오.

cmd /C cd $(CURRENT_DIRECTORY) && "PATH_to_.bat_file" $(FULL_CURRENT_PATH)

여기에서는 .py 파일이있는 디렉토리로 들어가서 .py 코드 디렉토리에있는 다른 관련 파일에 액세스 할 수 있습니다.

그리고 .bat 파일에는 다음이 있습니다.

@ECHO off
set File_Path=%1

call activate Venv
python %File_Path%
pause

참고 URL : https://stackoverflow.com/questions/1702586/how-to-execute-a-python-file-in-notepad

반응형