How to work with pure python file (.py)

Hi,
I have a python project built on pycharm, which has a bunch of .py files. I would like to work on the same files from jupyter environment. Is there any best practice for doing the same? I dont want to create notebooks (.pynb) files and execute the code as cells.
Is there any extension to just ‘run’ a .py file from jupyter?
Thanks

Some simple options:

  1. Open a terminal in Jupyter, run your Python scripts in the terminal like you would in your local terminal.

  2. Make a notebook, and use %run <name of script.py> as an entry in a cell. See here. This is more full featured then using !python <name of script.py> in a cell .

Thanks!

  1. Open a terminal in Jupyter, run your Python scripts in the terminal like you would in your local terminal.

this would be like working from cmd prompt.

  1. Make a notebook, and use %run <name of script.py> . See here . This is more full featured then using %run <name of script.py> in a cell

I have many execution points in .py file, creating a notebook for each would take up time, Is there a way to just load the .py file and ‘run’ it like one can run a cell? Basically, trying to see if I can go without using an IDE for .py.

You may want to look into the %load command which works in a notebook too, see here and here.

Plus, in a cell you can do import <name of script> to import it into the notebook namespace. (Note you don’t need the .py).

As a different route, you may also be interested in Jupytext, see related answer here.

(Note: I had typos in my second item listed in my first answer. They’ve been fixed now).

Another option is to open the .py files in JupyterLab in the code editor. You can run snippets from the files interactively in an associated console.

Best practices of course depends on what you are trying to do with them.

1 Like