title: Can I tell Python to ignore the rest of a file and/or exit without raising an exception as if the end of file was reached?
body: <p>When I execute a Python script(*) and the end of the file is reached, I get back to the command prompt without any additional messages displayed. I would like to know whether there's a statement I can insert somewhere in the source file in order to &quot;exit the file&quot; (end interpretation &amp; compilation) at that point, <em>i.e.</em>, ignore its contents from there up to the end of the file (or alternatively, to ignore a large region as I could do in C using an <code>#if 0</code> preprocessor directive and the corresponding <code>#endif</code>), so I could leave notes and code snippets etc. below that point while I'm working on the earlier part of the file.</p>
  <p>When I use <code>quit()</code>, <code>exit()</code>, <code>raise SystemExit(...)</code> etc., all of these throw an exception leading to a &quot;Traceback&quot; message. Is there something like <code>\endinput</code> in (La)TeX which tells the interpreter to simply ignore the rest of the file, as if the end-of-file was already reached, or the <code>cpp</code> directives <code>#if 0 ... #endif</code>?</p>
  <p>The question &quot;How to exit... without Traceback...&quot; is many times asked and answered here on SO, but the answer is always &quot;you can't&quot;; &quot;it must be that way to allow cleanup / debugging ...&quot; (some mention <code>os._exit()</code> then others say &quot;no-no because of incomplete cleanup&quot;). Maybe this refers to exiting from &quot;within something&quot;, but maybe I'm in that case because my file is read by some other python function. So I'm rather looking for a command which says &quot;Here's the end of this file!&quot; or &quot;Ignore the subsequent lines of the input file up to the end (or up to the corresponding <code>#endignore</code> directive is such exists.)</p>
  <p>PS: I know that I could put the rest of the file within <code>&quot;&quot;&quot;...&quot;&quot;&quot;</code>, but that would work only up to where I already have a multi-line comment in my &quot;scratch area&quot;. Yes, I could [and do] use &quot;&quot;&quot; for multi-line commands and ''' to &quot;comment out&quot; the rest of the file. But I consider that as a workaround, and not answer to my question whether such a command or directive exists.</p>
  <p>(*) To explain further my need for such a solution: I have this problem on pythonanywhere.com which I have to use for professional (educational) reasons. Clicking on &quot;Run&quot; executes a command <code>_pa_run(&quot;filename&quot;)</code> on their server (probably a custom command made by them), maybe that's why <code>exit()</code> or <code>sys.exit()</code> yield that Traceback.</p>
  <p>EDIT: Upon request, I'll add a specific example (although unrealistic, for sake of brevity):</p>
  <pre><code>def Newton(f,x0,eps,N):
      &quot;&quot;&quot;here I implement Newton's method&quot;&quot;&quot;
      #(...) iterations go here (...)
      return x
  def f(x): #function for testing
     return ...
  
  quit() # Unfortunately this raises a Traceback, at least when the file is &quot;Run&quot; 
  # on pythonanywhere.com. I'd like to avoid this. I would simply like everything
  # to be ignored after this point, let's say &quot;for convenience&quot;, to simplify.
  # I'd like to know whether this is possible, in the given configuration.
  
  def f(x):# this is another function for testing
     return [something else]
  [...] # another formula to try. This is just a snippet and might not compile.
  def Newton(...):# the old version. Not yet trashed because maybe needed.
        # Would have to rename this if the whole file is read by the compiler.
  
  # Below follow more routines which are part of the final version of this file, 
  # but I don't want to compile all of them each time, while I fiddle around with
  # the latest addition, for now put the beginning of the file.
  # OTOH I would like to have that code in the same file for easier copy-paste in
  # case I need some parts of it while developing the new stuff at top of file.
  </code></pre>