IronPython global name '__transformers__' is not defined

Hey all, I’m making some network request on R8 using python 2.7. I noticed that if the network request is made immediately when the plugin starts to run, I have a chance of running into this exception particularly on Windows:

2024-10-19 08:43:56,961 - __main__ - ERROR - Traceback (most recent call last):
  File "G:\My Drive\test.py", line 850, in thread_function
    result = self._make_api_request(path, payload_dict, method)
  File "G:\My Drive\test.py", line 831, in _make_api_request
    response_data = response.read()
  File "<string>", line 53, in __ironpy_tracefunc__
NameError: global name '__transformers__' is not defined

If I retry this again in 1s, it will work. Here is the snippet:

        # Get the response
        response = urllib2.urlopen(req)

        # Read the response and parse JSON
        response_data = response.read()
        response_dict = json.loads(response_data)

These code are running on a separate thread. Any ideas? Thanks!

Hard to tell without a minimal script that reproduces this.

1 Like

Thanks Nathan, here is the full snippet:

class APIHandler:
    def _make_api_request(self, path, payload_dict, method):
        url = API_ENDPOINT + "/" + path

        if method == "POST":
            json_data = json.dumps(payload_dict)
            req = urllib2.Request(url, json_data)
        elif method == "GET":
            params = urllib.urlencode(payload_dict)
            url += "?" + params
            req = urllib2.Request(url)
        else:
            raise RuntimeError("Unable to handle method {}".format(method))

        response = urllib2.urlopen(req)
        response_data = response.read()
        response_dict = json.loads(response_data)

        # Processing response.

    def threaded_api_request(self, path, payload_dict, method, on_complete=None, on_error=None):
        def thread_function():
            from System import Action, Func, Object

            import time
            time.sleep(5)
            
            try:
                result = self._make_api_request(path, payload_dict, method)
                if on_complete and result:
                    Rhino.RhinoApp.InvokeOnUiThread(Func[Object, Object](on_complete), result)
            except Exception as e:
                import traceback
                full_traceback = traceback.format_exc()

                logger.error("Error occurred during api request: " + str(e))

                if on_error:
                    Rhino.RhinoApp.InvokeOnUiThread(Func[Object, Object](on_error), e)

        thread = threading.Thread(target=thread_function)
        thread.start()
        return thread


def _make_api_request(self, path, payload, method, on_complete):
    api_handler = APIHandler()

    def on_error(e):
        # processing...

    thread = api_handler.threaded_api_request(path,
                                                payload,
                                                method,
                                                on_complete=on_complete,
                                                on_error=on_error)

``

The `_make_api_request` is called immediately in the main function.

This isn’t a script I can load and just run…

Hey Nathan, sorry this seems to be an issue only if I have a breakpoint in the script editor. Otherwise, it works fine. I will focus on finishing the plugin and work on a snippet later this week. Thanks!