docstr: The TracerPythonScript class enables to easily run a python script.
  
  When subclassing this class, you can configure your own :class:`~lightning.app.utilities.tracer.Tracer`
  by :meth:`~lightning.app.components.python.tracer.TracerPythonScript.configure_tracer` method.
  
  The tracer is quite a magical class. It enables you to inject code into a script execution without changing it.
  
  Arguments:
      script_path: Path of the python script to run.
      script_path: The arguments to be passed to the script.
      outputs: Collection of object names to collect after the script execution.
      env: Environment variables to be passed to the script.
      kwargs: LightningWork Keyword arguments.
  
  Raises:
      FileNotFoundError: If the provided `script_path` doesn't exists.
  
  **How does it work?**
  
  It works by executing the python script with python built-in `runpy
  <https://docs.python.org/3/library/runpy.html>`_ run_path method.
  This method takes any python globals before executing the script,
  e.g., you can modify classes or function from the script.
  
  Example:
  
      >>> from lightning.app.components.python import TracerPythonScript
      >>> f = open("a.py", "w")
      >>> f.write("print('Hello World !')")
      22
      >>> f.close()
      >>> python_script = TracerPythonScript("a.py")
      >>> python_script.run()
      Hello World !
      >>> os.remove("a.py")
  
  In the example below, we subclass the  :class:`~lightning.app.components.python.TracerPythonScript`
  component and override its configure_tracer method.
  
  Using the Tracer, we are patching the ``__init__`` method of the PyTorch Lightning Trainer.
  Once the script starts running and if a Trainer is instantiated, the provided ``pre_fn`` is
  called and we inject a Lightning callback.
  
  This callback has a reference to the work and on every batch end, we are capturing the
  trainer ``global_step`` and ``best_model_path``.
  
  Even more interesting, this component works for ANY PyTorch Lightning script and
  its state can be used in real time in a UI.
  
  .. literalinclude:: ../../../../examples/app/components/python/component_tracer.py
      :language: python
  
  
  Once implemented, this component can easily be integrated within a larger app
  to execute a specific python script.
  
  .. literalinclude:: ../../../../examples/app/components/python/app.py
      :language: python
docstr_tok: ["The","TracerPythonScript","class","enables","to","easily","run","a","python","script",".","When","subclassing","this","class",",","you","can","configure","your","own",":","class",":","`","~lightning.app.utilities.tracer.Tracer","`","by",":","meth",":","`","~lightning.app.components.python.tracer.TracerPythonScript.configure_tracer","`","method",".","The","tracer","is","quite","a","magical","class",".","It","enables","you","to","inject","code","into","a","script","execution","without","changing","it",".","Arguments",":","script_path",":","Path","of","the","python","script","to","run",".","script_path",":","The","arguments","to","be","passed","to","the","script",".","outputs",":","Collection","of","object","names","to","collect","after","the","script","execution",".","env",":","Environment","variables","to","be","passed","to","the","script",".","kwargs",":","LightningWork","Keyword","arguments",".","Raises",":","FileNotFoundError",":","If","the","provided","`","script_path","`","does","n't","exists",".","*","*","How","does","it","work","?","*","*","It","works","by","executing","the","python","script","with","python","built-in","`","runpy","<","https",":","\/\/docs.python.org\/3\/library\/runpy.html",">","`","_","run_path","method",".","This","method","takes","any","python","globals","before","executing","the","script",",","e.g.",",","you","can","modify","classes","or","function","from","the","script",".","Example",":",">",">",">","from","lightning.app.components.python","import","TracerPythonScript",">",">",">","f","=","open","(","``","a.py","''",",","``","w","''",")",">",">",">","f.write","(","``","print","(","'Hello","World","!","'",")","''",")","22",">",">",">","f.close","(",")",">",">",">","python_script","=","TracerPythonScript","(","``","a.py","''",")",">",">",">","python_script.run","(",")","Hello","World","!",">",">",">","os.remove","(","``","a.py","''",")","In","the","example","below",",","we","subclass","the",":","class",":","`","~lightning.app.components.python.TracerPythonScript","`","component","and","override","its","configure_tracer","method",".","Using","the","Tracer",",","we","are","patching","the","``","__init__","``","method","of","the","PyTorch","Lightning","Trainer",".","Once","the","script","starts","running","and","if","a","Trainer","is","instantiated",",","the","provided","``","pre_fn","``","is","called","and","we","inject","a","Lightning","callback",".","This","callback","has","a","reference","to","the","work","and","on","every","batch","end",",","we","are","capturing","the","trainer","``","global_step","``","and","``","best_model_path","``",".","Even","more","interesting",",","this","component","works","for","ANY","PyTorch","Lightning","script","and","its","state","can","be","used","in","real","time","in","a","UI",".","..","literalinclude",":",":","..","\/","..","\/","..","\/","..","\/examples\/app\/components\/python\/component_tracer.py",":","language",":","python","Once","implemented",",","this","component","can","easily","be","integrated","within","a","larger","app","to","execute","a","specific","python","script",".","..","literalinclude",":",":","..","\/","..","\/","..","\/","..","\/examples\/app\/components\/python\/app.py",":","language",":","python"]
code: def __init__(
      self,
      script_path: str,
      script_args: Optional[Union[list, str]] = None,
      outputs: Optional[List[str]] = None,
      env: Optional[Dict] = None,
      code: Optional[Code] = None,
      **kwargs: Any,
  ):
      """The TracerPythonScript class enables to easily run a python script.
  
      When subclassing this class, you can configure your own :class:`~lightning.app.utilities.tracer.Tracer`
      by :meth:`~lightning.app.components.python.tracer.TracerPythonScript.configure_tracer` method.
  
      The tracer is quite a magical class. It enables you to inject code into a script execution without changing it.
  
      Arguments:
          script_path: Path of the python script to run.
          script_path: The arguments to be passed to the script.
          outputs: Collection of object names to collect after the script execution.
          env: Environment variables to be passed to the script.
          kwargs: LightningWork Keyword arguments.
  
      Raises:
          FileNotFoundError: If the provided `script_path` doesn't exists.
  
      **How does it work?**
  
      It works by executing the python script with python built-in `runpy
      <https://docs.python.org/3/library/runpy.html>`_ run_path method.
      This method takes any python globals before executing the script,
      e.g., you can modify classes or function from the script.
  
      Example:
  
          >>> from lightning.app.components.python import TracerPythonScript
          >>> f = open("a.py", "w")
          >>> f.write("print('Hello World !')")
          22
          >>> f.close()
          >>> python_script = TracerPythonScript("a.py")
          >>> python_script.run()
          Hello World !
          >>> os.remove("a.py")
  
      In the example below, we subclass the  :class:`~lightning.app.components.python.TracerPythonScript`
      component and override its configure_tracer method.
  
      Using the Tracer, we are patching the ``__init__`` method of the PyTorch Lightning Trainer.
      Once the script starts running and if a Trainer is instantiated, the provided ``pre_fn`` is
      called and we inject a Lightning callback.
  
      This callback has a reference to the work and on every batch end, we are capturing the
      trainer ``global_step`` and ``best_model_path``.
  
      Even more interesting, this component works for ANY PyTorch Lightning script and
      its state can be used in real time in a UI.
  
      .. literalinclude:: ../../../../examples/app/components/python/component_tracer.py
          :language: python
  
  
      Once implemented, this component can easily be integrated within a larger app
      to execute a specific python script.
  
      .. literalinclude:: ../../../../examples/app/components/python/app.py
          :language: python
  
      """
      super().__init__(**kwargs)
      self.script_path = str(script_path)
      if isinstance(script_args, str):
          script_args = script_args.split(" ")
      self.script_args = script_args if script_args else []
      self.original_args = deepcopy(self.script_args)
      self.env = env
      self.outputs = outputs or []
      for name in self.outputs:
          setattr(self, name, None)
      self.params = None
      self.drive = code.get("drive") if code else None
      self.code_name = code.get("name") if code else None
      self.restart_count = 0
  
imports: import os
  import signal
  import sys
  from copy import deepcopy
  from typing import Any, Dict, List, Optional, Union
  from typing_extensions import TypedDict
  from lightning.app.core.work import LightningWork
  from lightning.app.storage.drive import Drive
  from lightning.app.storage.payload import Payload
  from lightning.app.utilities.app_helpers import Logger, _collect_child_process_pids
  from lightning.app.utilities.packaging.tarfile import clean_tarfile, extract_tarfile
  from lightning.app.utilities.tracer import Tracer
label_desc: Use image node_id 4 to create a new TracerPythonScript object from inherited base classes: LightningWork with example: obj = TracerPythonScript(script_path, script_args, outputs, env, code)