program story

pycharm을 사용한 대화 형 쉘 디버깅

inputbox 2020. 10. 26. 07:59
반응형

pycharm을 사용한 대화 형 쉘 디버깅


저는 PyCharm을 처음 사용합니다. 오랫동안 IDLE을 사용해 왔습니다.

IDLE에서 스크립트 실행 후 Python 객체를 사용하는 것은 매우 편리합니다. PyCharm을 사용하여 대화 형 Python 쉘로 실행 한 후 스크립트 객체를 사용하는 방법이 있습니까?

예를 들어 'test.py'파일 하나가있는 'test'프로젝트가 있습니다.

a = '123'
print a

실행 후 결과를 얻을 수 있습니다.

123
Process finished with exit code 0

대화식 쉘에서 문자열 'a'를 어떻게 사용할 수 있습니까?


현재 디버그 세션을위한 내장 파이썬 셸

  1. 코드에서 관심있는 줄에 중단 점을 설정하고 (예 : 여백을 클릭하여) 디버그를 시작합니다 (편집기에서 마우스 오른쪽 버튼을 클릭 한 다음 Debug myfile.py ... 선택 ).
  2. 중단 점에 도달하면 디버그 > 콘솔 탭을 찾은 다음 명령 줄 표시 아이콘 을 클릭 합니다 (스크린 샷 참조).

그러면 >>>현재 범위의 모든 변수에 액세스 할 수 있고 Python 셸에서 일반적으로 수행하는 모든 작업을 수행 할 수있는 Python 셸 ( 스크린 샷 의 녹색 참조)이 활성화됩니다 .

최근 pycharm 버전에서는 일반 파이썬 셸 대신 전체 ipython 인터프리터를 얻습니다 (ipython이 설치된 경우).

디버깅 된 앱에 대해 Python 셸 활성화

식의 평가 창을

좀 더 편안한 대안으로, 일부 변수 만 검사하거나 범위에있는 개체의 멤버에 액세스하거나 메서드를 호출해야하는 경우 중단 점에 도달하면 현재 범위에서 식을 선택하고 마우스 오른쪽 단추로 클릭-> 식 평가 (또는 아래의 메뉴에 표시 핫키 RunEvaluate Expression...), 필요에 따라 편집 - 당신은 입력 할 수 있는 가능한 자동 완성 기능으로, 파이썬 식을 - 다음 키를 눌러 입력 (또는 클릭 평가 결과를 검사).

여러 줄 및 여러 줄식이 지원됩니다. 검사 대화 상자의 여러 줄 버전을 얻으려면 코드 조각 모드를 클릭 하거나 편집기에서 한 줄 이상의 코드를 선택한 다음 식을 평가합니다. 기존 변수에 값을 할당하고 (새 변수는 정의 할 수 없음) 전체 코드 청크를 실행할 수 있습니다.현재 범위에서 변수 검사

변수 위로 마우스를 가져갑니다.

디버그 모드에서 중단 점에 도달 한 후 변수 값을 보려면 변수 위에 마우스 포인터를 올려 놓으면 (1-2 초) 해당 값이 도구 설명에 표시됩니다.

여기에 이미지 설명 입력

힌트에는 아이콘 이 포함됩니다. 아이콘을 클릭하면 팝업에서 속성이 열립니다.

여기에 이미지 설명 입력

범위의 변수에 대한 인스펙터는 디버그 > 디버거 아래의 하단 패널에 표시됩니다 .

들어 pandas데이터 프레임 당신은 볼 것이다 보기 DataFrame로 표시됩니다 링크를 클릭 - 변수 관리자 패널이나 호버 팝업에 링크 데이터보기 패널의 테이블로 dataframe을 .


Piga-fetta, Games Brainiac 및 kobejohn의 이전 답변은 유용하지만 만족스럽지 않습니다 . 그래서 여기에 세 번째 옵션을 제공합니다.

선택한 코드를 콘솔에로드 (내 제안)

사용 Shift+ Alt+는 E선택한 코드 또는 커서가 콘솔에 배치하고 즉시 실행되는 라인을로드합니다. 여기에는 몇 가지 단점도 있습니다.

  • 전체 파일을 실행하려면 전체 파일을 선택해야합니다.
  • 코드는 오류가 발생하더라도 계속 실행됩니다.

그러나 그 대가로 우리는 IDLE보다 나은 기능을 얻습니다. (제 생각에는) 한 번에 하나의 코드를 선택할 수 있다는 것입니다.

여기에서 그것에 대해 읽어보십시오 .

중단 점 사용 및 식 평가 ( Alt- F8) (Piga-fetta에서 제안)

이는 코드의 특정 위치에서 디버그해야하는 대규모 애플리케이션에서 매우 유용하지만 대화 형 코딩에는 유용하지 않습니다. 그래서 이것은 우리가 원하는 것이 아닙니다.

도구 사용-> Python 콘솔 실행 (Games Brainiac 및 kobejohn에서 제안)

이것은 우리가 원하는 것이지만, 특히 우리가 실행하려는 모듈이 프로젝트의 루트 디렉토리에없는 경우 약간 번거 롭습니다.


PyCharm 2와 PyCharm 3에서 Python 콘솔을 사용하기 만하면됩니다. 프로젝트 루트가 이미 다음에 추가되었으므로 간단히 가져올 수 있습니다 PYTHONPATH.

몇 가지 스크린 샷을 통해 설명하겠습니다.

1. console.py루트 디렉토리에 파일 만들기

여기에 이미지 설명 입력

2. PyCharm에서 Python 콘솔 열기

여기에 이미지 설명 입력

3. console.py파일 에서 변수 가져 오기

여기에 이미지 설명 입력

그리고 거기에서 변수를 성공적으로 가져 왔습니다.


실행 후 명령 줄을 열어 둡니다.

여전히이 문제가있는 사용자 : Run / Debug 메뉴로 이동하여 Edit Configuration을 선택하고 'Show command line'상자를 선택하면 >>> 프롬프트에서 콘솔에 매개 변수를 입력하고 기능을 테스트 할 수 있습니다.

현재 파일 만 설정 변경

글로벌 구성

To make this change apply to all your .py files (as this check box only applies to the current file you're working on) go to: Edit configuration, in the pop up you will see a menu tree on the left, select Defaults, then Python, then check the 'Show command line' box, this will make it the default setting whenever you open a .py file, (this feature should really be on by default!)

모든 Python 파일에 대한 설정 변경


I found the best answer in: Interacting with program after execution

Quoting the answer below:

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

From output of python --help:

-i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x

To set interpreter option in PyCharm go to Run|Edit Configuration

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I tried it and it works - simply use "-i" (without quotation marks) as "Interpreter options". Note that if you only change the Defaults in the Run/Debug Configurations, it may not take immediate effect for scripts you've already run before; you will need to edit the configurations of those scripts one by one.


Not mentioned above:

If you want to use a variable during execution, e.g. to when you set a breakpoint and then experiment with calling functions on objects in current scope, PyCharm has an 'Evaluate Expression (Alt-F8)' popup window.

In this window, you can call functions and see the output. Code completion also works. This window also has a "code fragment mode", I am just researching what it means - can you define temporary functions here?.

(I am using PyCharm 3.0.1 Community Edition)


*update

From your update, I think this SO question provides at least one perfect answer.

Let me write it here a little more step by step than the answer I linked.

  • Tools --> Run Python Console
  • import your script import myscript (without .py) (if you want a short name, use import myscript as m
  • (you may need to run main() if you have an if __name__ == '__main__' block)
  • as in your question, a is available in myscript.a
  • if you want to, run the script again with myscript = reload(myscript)

*original

In PyCharm 3.0 you can do the following:

  • Tools --> Run Python Console
  • Tools --> Open Terminal (from which you can start python or do whatever)

Is that what you are looking for? If not, please try them and let me know how that is different from what you want.


A further alternative is to simply use the same command that Spyder uses to "interactively" run a script:

>>> runfile('myscript.py')

Then you can open the variable explorer for the interactive console and rerun the script by running the above command again. Very similar to the Spyder workflow. All the other above methods will leave you with an interactive console prompt but not the option to open a variable explorer so if you are looking for that kind of feature, try the above.


In addition to the suggestion I made on Ramkins's answer, you can run the file directly with the console by right clicking inside the file and selecting Run File in Console.

참고 URL : https://stackoverflow.com/questions/19329601/interactive-shell-debugging-with-pycharm

반응형