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 .

2 Likes

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.

1 Like

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

1 Like

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.

2 Likes

coming late to this but … you can import python files into a notebook, but be careful to reimport them each time you modify the source python files.

I have code to do this each time like this:

from cases import datelib_spec
import importlib
importlib.reload(datelib_spec) # every run
1 Like

You can try with the jupyterlab-executor

pip install jupyterlab-executor

It is an extension that you can run scripts from the jupyterlab file browser directly.

1 Like

Hello I installed the executor but when I right click and execute and provide the args it does not invoke the py file , does the executor only work with the desktop setup of Jupyter and not on the browser?