I have a script named "poc.py" that takes one positional command line argument "inputfile.txt". The poc.py script uses argparse to handle the positional command line argument and then pass the args dict to main(). Once in main(), I read the input file, do some processing, create a pandas DataFrame, and finally plot the data. I am having difficulty manipulating my DataFrame and controlling the exact formatting of the resulting plot, so I'd like to try ipython to explore this interactively and see if I can get a better grasp on the "pythonic" ways to handle pandas/matplotlib.
So I tried experimenting with ipython and running a script, but I can't get ipython to keep my script's namespace.
I have tried this:
$ ipython --pylab -i poc.py inputfile.txt
Which runs my script just fine, and displays the plots (even without the blocking plt.show() call), but when the script is finished, the ipython who and whos commands say Interactive namespace is empty. Likewise if I first enter the ipython shell and then do:
In [2]: run poc.py inputfile.txt
when the script is done (again, plenty of output, plots show up) I get the same result: an empty interactive namespace.
What am I missing in terms of understanding how to run an external and use ipython to interactively explore the data/objects in my script?
Here is a barebones example of how my script (poc.py) is setup:
import numpy as np
import matplotlib as plt
import pandas as pd
# etc ...more libraries and custom functions here...
def main(args):
data = np.genfromtxt(args.inputfile)
# (omitted)...more data processing / manipulation...
pdata = pd.DataFrame(data)
# (omitted)...more data processing / manipulation...
plt.plot(pdata)
# (omitted)...some formatting of the matplotlib/axes/figure objects
plt.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='''some program...''')
parser.add_argument('inputfile', help='path to input file')
args = parser.parse_args()
main(args)
np,plt,pd,main,parser, andargs. In particular,dataandpdataare local variables from a function call that already finished. Is your problem just that you can't seepdwithout re-importing it or thatargsis missing? Or that you can't seedataandpdata? - abarnertdataandpdata. So I guess what I want is somehow to have IPython "stop" before exitingmain()? - tbcmain, those values won't even have been created yet. But if that's what you want, that's easy: Justimportthe module instead of running it; then you can callmainat your leisure (and put in debugger breakpoints or whatever). - abarnertmainto actually run, but leave its values behind, it has to either make the variablesglobalor, better,returnthem, and have your top-level script store them. - abarnert