Updates in scripts are not loading

Each time when I am making any changes in scripts I am working on (Python) I need to restart KLayout so changes appear during runtime.
The only changes that are loaded in right way are for the entry point script, but any script which is used through import is frozen and I cant see changes until restarting client.

Is there proper way to save these changes during work so they appear within current session?

Comments

  • Is there no way to force a "reload"? In other tools I've seen
    "trigger all callbacks" or "refresh" but not always easy to
    find.

    Or maybe you have to close the script editing session
    and reopen it from within the existing klayout session?

  • @dick_freebird , I have tried many things: closing and opening Macro Development window, editing script using external editor and reloading it in Macro Development window (in this case it does see that script was changed, but I dont see any changes during runtime though)...
    Still, the only way is rebooting klayout...

  • @alexlpn Sorry, I don't fully understand. You say you're changing a script in a text editor separately and then expect KLayout to load it somehow?

    This will not work for a variety of reasons. The main reason is that KLayout embeds a Python and Ruby interpreter and both do not re-execute files automatically.

    Re-executing a file from the command line ("Macro editor console") is possible:

    In Python 3:

    exec(open("/path/to/file.py").read())
    

    In Ruby:

    load("/path/to/file.rb")
    

    You cannot load ".lym" files this way. But .lym files are intended for use with the internal macro IDE anyway.

    I don't know what kind of scripts you are developing. Depending on the things you are doing there may be smarter ways. For example, I got reports by people who use PyCharm and configure KLayout as Python interpreter. So you don't need to restart KLayout - PyCharm does it for you.

    Matthias

  • @Matthias thank you for the reply!
    I am using built-in KLayout text editor for working with scripts and changes from it are not appearing without restarting client.
    I will try "PyCharm way", thanks for the hint

  • So maybe I need to understand the expectation here: what do you mean by "appearing"? You mean becoming effective?

    So like if you define a function and call it from the console, right? So you edit the text and executed the function again - and it did not show the new behaviour?

    In this case you'll need to execute the script. This will actually redefine the function. By editing it alone the function definition will not change.

    Is that what you mean?

    Matthias

  • I have the same issue. When I use the KLayout built-in editor to create or edit a python PCell definition, for example, I run it (by clicking the green arrow button at the top) register it with the .lym macro (then run this by clicking the green arrow button at the top), and I see nothing change in the library list associated with the open layout I was working with. If I quit KLayout, then reopen KLayout, I see the changes in the library list, and my edits have been incorporated properly. (The .lym macro is set to run when KLayout opens, from the properties window.) Is there some trick to getting KLayout to recognize edits to python files or associated .lym macros without having to restart KLayout? The green arrow buttons don't seem to do anything as far as I can tell.
    Thank you for your time and attention,
    Mike

  • Basically, running a macro should update the library list.

    But I need to be more specific: it may be important which file to execute and there are two buttons for running macros.

    First, you need to execute the file which actually installs the library (does "register_library"). If your code is separated into multiple files doing "import/load", you usually need to run the top most file. But beware: if you use Python "import", the files usually get imported only once and rerunning does not execute the imported files again. Same for Ruby "require". Only Ruby "load" executes the loaded files over and over again.

    Second, when you use the Run buttons, the first one (the one without the exclamation mark) will run the "active" script. This is the one with the green arrow in the tab. The second Run button (the one with the exclamation mark) makes the current script active before running it.

    You should also try using the latest version, because in the past, sometimes the re-registration of a library failed or in some cases crashed the application.

    Maybe that explains what you are observing?

    Matthias

  • Thank you very much for your reply. I could see one potential overlap with what you have mentioned. The .lym file that does "register_library" imports from plain .py files the PCells that get registered (these plain .py files don't import anything beyond standard python modules). If I edit one of the .py files, save it, and then run the .lym file to register it with the edits, it seems possible the .lym file would not re-import the .py file after it has been changed. In this case, I would need to put the "register_library" method in the same file that contains my PCell code. Here is the code in an example .lym file...

    import pya
    from TLMC_py import TLMC  # TLMC is a PCell class
    from ViaChain_py import ViaChain  # ViaChain is a PCell class
    from SerpComb_py import SerpComb  # SerpComb is a PCell class
    
    class T_Macros(pya.Library):
    
      def __init__(self):
    
        # Set the description
        self.description = "Test Macros"
    
        # Create the PCell declarations
        self.layout().register_pcell("CircularTLM", TLMC())
        self.layout().register_pcell("ViaChain", ViaChain())
        self.layout().register_pcell("SerpComb", SerpComb())
    
        # If a library with that name already existed, it will be replaced then.
        self.register("T_Macros")
    
    
    # Instantiate and register the library
    T_Macros()
    

    I will try embedding this in with my PCell code file so I don't need to do the import.
    Or... I am running version 0.27.7 -- maybe it would help if I upgrade to 0.27.8 and add a T_Macros.refresh after instantiating the library??

  • I tried updating to 0.27.10, and didn't have success with just doing that. I couldn't find a way to make .refresh do anything. I was able to get something to work by embedding the library instantiation into the PCell code file so it wouldn't need an import. So, it seems the problem is indeed just related to python not re-importing once the code has been run previously.

    There is a python module called importlib with a reload function that appears to properly force a reload and solve the issue. If I change the above code to the following, I am able to simply run the .lym macro and my _py file changes are recognized (for the one module TLMC that I modified here):

    import pya
    ### from TLMC_py import TLMC  # TLMC is a PCell class
    import TLMC_py  # <-- new, replacing the above statement
    from ViaChain_py import ViaChain  # ViaChain is a PCell class
    from SerpComb_py import SerpComb  # SerpComb is a PCell class
    import importlib  # <-- new
    importlib.reload(TLMC_py)  # <-- new
    
    class T_Macros(pya.Library):
    
      def __init__(self):
    
        # Set the description
        self.description = "Test Macros"
    
        # Create the PCell declarations
        ### self.layout().register_pcell("CircularTLM", TLMC())
        self.layout().register_pcell("CircularTLM", TLMC_py.TLMC())  # <-- new, replacing the above statement
        self.layout().register_pcell("ViaChain", ViaChain())
        self.layout().register_pcell("SerpComb", SerpComb())
    
        # If a library with that name already existed, it will be replaced then.
        self.register("T_Macros")
    
    
    # Instantiate and register the library
    T_Macros()
    
  • Yes, exactly. KLayout uses a Python interpreter the same way the Python shell does. So once imported, modules are not automatically re-imported again. You need to tell the interpreter to discard the loaded module and reload.

    The solution actually depends on the Python version. I found this discussion which lists the solution for different versions: https://stackoverflow.com/questions/684171/how-to-re-import-an-updated-package-while-in-python-interpreter

    Regards,

    Matthias

Sign In or Register to comment.