skip to Main Content

when using VSCode, I can run python files in cells/parts as if it was Jupyter notebook, without actually having a notebook.
https://code.visualstudio.com/docs/python/jupyter-support-py
It means, you can run a python file, part by part, iteratively, like in Jupyter Notebooks, but in .py file.

it helps me to keep the code organized as a python file. (screenshot attached)

I wonder if the same feature exists in PyCharm. I couldn’t find it.

I attach a screenshot of the feature in VsCode when I can run simple python file in interactive mode, part by part.

thanks.

enter image description here

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    ok, answer found:

    Magic Python in PyCharm PyCharm supports Magic Python cell execution. To use Magic Python, you need to enable Scientific Mode in the View menu. You can then use #%% to indicate the start and end of cells. Individual Cells can be executed in the console by pressing CTRL+Enter.

    In PyCharm, right-click on the root directory and select New > Python File. Give your file a meaningful name. Enter

    #%%
    print("This is the first cell")
    #%%
    print("This is not executed when the first cell is run")
    

    Enable Scientific Mode in the View menu. Run the first cell by placing you mouse in the cell and pressing CTRL+Enter. Run the second cell by clicking on the Play button (arrow) that appears in the gutter of the editor.

    enter image description here

    source(and credit to): https://www.kevinsheppard.com/teaching/python/course/lesson-1/


  2. The same feature exists in PyCharm.

    Just right-click and select Execute selection in Python Console.

    Selection

    Execution

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search