How to avoid re-importing modules and re-defining large object every time a script runs
<p>This must have an answer but I cant find it. I am using a quite large python module called quippy. With this module one can define an intermolecular potential to use as a calculator in ASE like so: </p>

<pre><code>from quippy import *
from ase import atoms
pot=Potential("Potential  xml_label=gap_h2o_2b_ccsdt_3b_ccsdt",param_filename="gp.xml")
some_structure.set_calculator(pot)
</code></pre>

<p>This is the beginning of a script. The problem is that the <code>import</code> takes about 3 seconds and <code>pot=Potential...</code> takes about 30 seconds with 100% cpu load. (I believe it is due to parsing a large ascii xml-file.) If I would be typing interactively I could keep the module imported and the potential defined, but when running the script it is done again on each run. </p>

<p>Can I save the module and the potential object in memory/disk between runs? Maybe keep a python process idling and keeping those things in memory? Or run these lines in the interpreter and somehow call the rest of the script from there? </p>

<p>Any approach is fine, but some help is be appreciated!</p>

<p>You can either use raw files or modules such as <code>pickle</code> to store data easily.</p>

<pre><code>import cPickle as pickle
from quippy import Potential
try:  # try previously calculated value
    with open('/tmp/pot_store.pkl') as store:
        pot = pickle.load(store)
except OSError:  # fall back to calculating it from scratch
    pot = quippy.Potential("Potential  xml_label=gap_h2o_2b_ccsdt_3b_ccsdt",param_filename="gp.xml")
    with open('/tmp/pot_store.pkl', 'w') as store:
        pot = pickle.dump(pot, store)
</code></pre>

<p>There are various optimizations to this, e.g. checking whether your pickle file is older than the file generating it's value.</p>

python python-2.7 optimization import load