Imagine you have your developing code file you were editing with your text editor elswhere. We'll make myobj.py
to simulate that:
s = '''class Myobj(object):
def __init__(self,arg1,arg2):
self.a1 = arg1
self.a2 = arg2
def doSomething(self):
from IPython import embed; embed() # breakpoint()
test = Myobj(2,3)
test.doSomething()'''
%store s >myobj.py
Writing 's' (str) to file 'myobj.py'.
Now in Jupyter, say you define your settings and want to test with your developing code. Let's define the settings to test by running the next cell:
j = 1
t = 'test string'
Now to test the current script we can run it attached to this active kernel using the -i
flag, see %run documentation:
%run -i myobj.py
Python 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:35:26) [GCC 10.4.0] Type 'copyright', 'credits' or 'license' for more information IPython 8.5.0 -- An enhanced Interactive Python. Type '?' for help.
Out[1]: 1
Out[2]: 'test string'
Out[3]: 2
That should give you what you want. Imagine that is a notebook itself. Just those last two cells!
Now Imagine over in your text editor you change the code. To simulate that run the next cell:
s = '''class Myobj(object):
def __init__(self,arg1,arg2):
self.a1 = arg1
self.a2 = arg2
def doSomething(self):
print('THIS IS A CHANGED VERSION')
print(' ')
print(' ')
from IPython import embed; embed() # breakpoint()
test = Myobj(2,3)
test.doSomething()'''
%store s >myobj.py
Writing 's' (str) to file 'myobj.py'.
Now if we really started with the imaginary notebook that just had the two cells, we could just hit the keyboard shortcut for Restart Kernel and Run All cells. But we'll imagine that by running the code again and querying the state:
%run -i myobj.py
THIS IS A CHANGED VERSION Python 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:35:26) [GCC 10.4.0] Type 'copyright', 'credits' or 'license' for more information IPython 8.5.0 -- An enhanced Interactive Python. Type '?' for help.
Out[1]: 1
Out[2]: 'test string'
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In [3], line 1 ----> 1 self.ai AttributeError: 'Myobj' object has no attribute 'ai'
Out[4]: 2