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?
Some simple options:
Open a terminal in Jupyter, run your Python scripts in the terminal like you would in your local terminal.
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 .
- 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.
- 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.
Plus, in a cell you can do
import <name of script> to import it into the notebook namespace. (Note you don’t need the
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).
Best practices of course depends on what you are trying to do with them.
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