Is there an easy way to access variables in the IPython interactive namespace. While implementing a project that has a slow load command, I would like to run a script to load the data into the interactive work space, then call a second script that uses the data, like is possible with MATLAB.
In this simple case, what I want to do is
In [20]: a=5
In [21]: run tst
where tst.py is just
print a
The idea is that I want to run the loading script once, then just work on tst.py.
Thanks!
Try using the -i option on IPython's magic run command; it makes the script run using the current interactive namespace, e.g. with
load.py:
a = 5
tst.py:
print a
From IPython I get;
In [1]: from load import *
In [2]: run -i tst
5
There is no easy or smart way to do this. One way would be to have a main function in your test function and then pass in the globals from your environment to update the globals in the caller. For example:
tst.py
def main(borrowed_globals):
globals().update(borrowed_globals)
print a
And then in iPython:
In [1]: a = 5
In [2]: import tst
In [3]: tst.main(globals())
5
Related
I am creating a DSL by doing some pre-processing on a string, and then using exec to call the pre-processed code using the python interpreter. I would like to be able to step through the pre-processed code using pdb, and be able to show the actual line of python code (after pre-processing) that I'm stepping through.
For example:
In [1]: s = '''print "hello"
...: print 'world'
...: '''
In [2]: s
Out[2]: 'print "hello"\nprint \'world\'\n'
In [3]: import pdb
In [4]: pdb.run(s)
> <string>(1)<module>()
(Pdb) list
[EOF]
I would like the list command in pdb to output the code and show what line I'm one, the same way that it does when I stop at a breakpoint in a regular python file. Any hints on how to do this, or an alternative approach/mindset would be greatly appreciated!
[edit]
I pass in a bunch of complicated objects to the exec using the optional globals positional argument to exec, so writing out the string to a file and then running that in pdb won't work. For example:
s = '''some_complicated_stateful_object.method(foo)'''
exec(s, {'some_complicated_stateful_object': an_object,
'foo': some_other_object})
Thanks to for the good suggestion though!
As my notebook gets longer, I want to extract some code out, so the notebook would be easier to read.
For example, this is a cell/function that I want to extract from the notebook
def R_square_of(MSE, kde_result):
# R square measure:
# https://en.wikipedia.org/wiki/Coefficient_of_determination
y_mean = np.mean(kde_result)
SS_tot = np.power(kde_result - y_mean,2)
SS_tot_avg = np.average(SS_tot)
SS_res_avg = MSE
R_square = 1 - SS_res_avg/SS_tot_avg
return R_square
How can I do it effectively?
My thoughts:
It's pretty easy to create a my_helper.py and put the code above there, then from my_helper import *
The problem is that, I may use other package in the method (in this case, np, i.e. numpy), then I need to re-import numpy in my_helper.py. Can it re-use the environment created in ipython notebook, hence no need for re-importing?
If I change the code in my_helper.py, I need to restart the kernel to load the change(NameError: global name 'numpy' is not defined), this makes it difficult to change code in that file.
Instead of importing your other file, you could instead run it with the %run magic command:
In [1]: %run -i my_helper.py
-i: run the file in IPython’s namespace instead of an empty one. This is useful if you are experimenting with code written in a text editor which depends on variables defined interactively.
I'd still take the opportunity to recommend writing the file as a proper python module and importing it. This way you actually develop a codebase usable outside of the notebook environment. You could write tests for it or publish it somewhere.
So let's say I have a script script1. Is there a way to interact with script1's variables and functions like an interpreter after or during its runtime?
I'm using IDLE and Python 2.7, but I'm wondering if I could do this in any interpreter not just IDLE's.
Say in my script, get = requests.get("example.com"). I'd like to hit F5 or whatever to run my script, and then instead of the console unloading all of the variables from memory, I'd like to be able to access the same get variable.
Is this possible?
That's a serious question. You might need to consult this page:
https://docs.python.org/2/using/cmdline.html#miscellaneous-options
Note the -i option, it makes interpreter enter interactive mode after executing given script.
you can do like this:
#file : foo.py
import requests
def req():
get = requests.get("example.com")
return get
and then run the script from a console
import foo
get = foo.req()
I´m starting in python. I have four functions and are working OK. What I want to do is to save them. I want to call them whenever I want in python.
Here's the code my four functions:
import numpy as ui
def simulate_prizedoor(nsim):
sim=ui.random.choice(3,nsim)
return sims
def simulate_guess(nsim):
guesses=ui.random.choice(3,nsim)
return guesses
def goat_door(prizedoors, guesses):
result = ui.random.randint(0, 3, prizedoors.size)
while True:
bad = (result == prizedoors) | (result == guesses)
if not bad.any():
return result
result[bad] = ui.random.randint(0, 3, bad.sum())
def switch_guesses(guesses, goatdoors):
result = ui.random.randint(0, 3, guesses.size)
while True:
bad = (result == guesses) | (result == goatdoors)
if not bad.any():
return result
result[bad] = ui.random.randint(0, 3, bad.sum())
What you want to do is to take your Python file, and use it as a module or a library.
There's no way to make those four functions automatically available, no matter what, 100% percent of the time, but you can do something very close.
For example, at the top of your file, you imported numpy. numpy is a module or library which has been set up so it's available any time you run python, as long as you import it.
You want to do the same thing -- save those 4 functions into a file, and import them whenever you want them.
For example, if you copy and paste those four functions into a file named foobar.py, then you can simply do from foobar import *. However, this will only work if you're running Python in the same folder where you saved your code.
If you want to make your module available system-wide, you have to save it somewhere on the PYTHONPATH. Usually, saving it to C:\Python27\Lib\site-packages will work (assuming you're running Windows).
If you decide to put them anywhere in your project folder don`t forget to create a blank init.py file so python can see them. A better answer can be provided here : http://docs.python.org/2/tutorial/modules.html
Save them in a file - this makes them a module.
If you put them in a file called mymod.py, in python you can load them as follows
from mymod import *
simulate_prizedoor(23)
Quick solution, without having to explicitly create a file - relies on IPython and its storemagic
IPython 4.0.1 -- An enhanced Interactive Python.
details.
In [1]: def func(a):
...: print a
...:
In [2]: func = _i #gets the previous input
In [3]: store func #store(magic) the input
#(auto-magic enabled or would need '%store')
Stored 'func' (unicode)
In [4]: exit
IPython 4.0.1 -- An enhanced Interactive Python.
In [1]: store -r func #retrieve stored string
In [2]: exec func #execute string as python code
In [3]: func(10)
10
Once you had stored all your functions just once, then you can restore them all with store -r, and then exec func once for each function, in each new session.
(Came across this question while looking for a solution for 'quick saving' functions (most convenient way) while in an interactive python session - adding my current best solution for future readers)
Sometimes I need to test my python code in shell, so I have to edit the code, save and quit and run the code. Then reopen the file to modify my code if anything goes wrong. Then save and quit .... I am wondering is there a handy feature in VI to easily test the code inside VI?
No, but if you have Ipython, it has a handy bit of magic called %autoreload.
For example: test.py
class A(object):
def __init__(self):
self.a = 0
Then I run ipython -i test.py
In [2]: a=A()
In [3]: a.a
Out[3]: 0
Ooops, the initial 'a' value should be 21... I better fix that.
class A(object):
def __init__(self):
self.a = 21
Now in ipython I do, again this is in the SAME INSTANCE OF IPYTHON:
%load_ext autoreload
%autoreload
This will reload the class A object into python for me, so now.
In [4]: a.a
Out[4]: 21
This can really help when debugging a large class, and are only making small changes.
My workflow typically consists of having two terminal windows open, one with vim for editing the .py files, and one running ipython to test and debug the changes.
Sure, I do this all the time.
In your vimrc put:
command R ! python ./%
Then :R will execute the python code. I also have this for debug though it's not as useful as I would have liked:
command D ! python -m ipdb ./%
It runs it in debug, but our environment typically doesn't dig on the perpetual rerun in the same instance.
I might be interpreting your questions incorrectly but this is my suggestion. Maybe you can open more than one terminal. On one terminal, write/edit your code and save it. I'm assuming with ':w' and leave the terminal open. Then on the other terminal, compile your code.