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>I found one solution, but I'm interested in alternatives. You can divide the script into two parts: </p>

<p>start.py:</p>

<pre><code>from quippy import Potential
from ase import atoms
pot=Potential(...  etc...
</code></pre>

<p>body.py:</p>

<pre><code>for i in range(max_int):
      print "doing things"
# etc...
</code></pre>

<p>Then enter python interpreter and run the start-script only once but the body as much as needed:</p>

<pre><code>me@laptop:~/dir$ python
&gt;&gt;&gt; execfile('start.py')
&gt;&gt;&gt; execfile('body.py')
&gt;&gt;&gt; #(change code of "body.py" in editor)
&gt;&gt;&gt; execfile('body.py') # again without reloading "start.py"
</code></pre>

<p>So this means a terminal is occupied and the script is affected, but it works. </p>

python python-2.7 optimization import load