1

I am trying to load a remote .py file in Thonny editor but I get the following error:

File "/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/thonny/editors.py", line 193, in _load_local_file
    with open(filename, "rb") as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'https://raw.githubusercontent.com/thonny/thonny/3eaa0319a9722bcdac12b4401b0a69b47d2e9f00/thonny/first_run.py'

The file I am just using for testing purposes is the: https://github.com/thonny/thonny/blob/3eaa0319a9722bcdac12b4401b0a69b47d2e9f00/thonny/first_run.py

Any ideas why self.editor._load_file( ... ) does not understands that this is a remote file?

Here is my __init__.py of my plugin:

class Exercise:
    """
    Fetch exercise to the current loaded file.

    Description
    """

    def __init__(self) -> None:
        """Get the workbench to fetch and copy the fetched content to editor, later."""
        self.workbench = get_workbench()

    def load_exercise(self) -> None:
        """Handle the plugin execution."""
        self.editor = self.workbench.get_editor_notebook().get_current_editor()

        self.filename = self.editor.get_filename()
        if self.filename is not None and self.filename[-3:] == ".py":
            self.editor.save_file()

            # self.filename = "/Users/limitcracker/Documents/Projects/ThonnyPlugin/test.py"
            self.filename = "https://raw.githubusercontent.com/thonny/thonny/3eaa0319a9722bcdac12b4401b0a69b47d2e9f00/thonny/first_run.py"
            self.editor._load_file(self.filename, keep_undo=True)

        showinfo(title="Exercise Loader", message="OK")

    def load_plugin(self) -> None:
        """
        Load the plugin on runtime.

        Using self.workbench.add_command(), the plugin is registered in Thonny
        with all the given arguments.
        """
        self.workbench.add_command(
            command_id="load_exercise",
            menu_name="tools",
            command_label="Load Exercise",
            handler=self.load_exercise,
            default_sequence="<Control-Alt-c>",
            extra_sequences=["<<CtrlAltCInText>>"],
        )


if get_workbench() is not None:
    run = Exercise().load_plugin()
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
limitcracker
  • 2,208
  • 3
  • 24
  • 23
  • Where you got the idea from that [open](https://docs.python.org/3/library/functions.html#open) support this by default ? I can't find something related in the docs. – Thingamabobs Dec 24 '22 at 15:19
  • @Thingamabobs in `_load_file(...)` it has an `if` statement that calls `_load_local_file(...)` or `_load_remote_file(...)`. But although it's a URL, the distinction is not made correctly. Why? – limitcracker Dec 25 '22 at 15:16
  • Oh I see, I misunderstood your question because of the URL that is used as path. Did you made a common mistake to have your file already open or forgot to close the file somewhere in your code? – Thingamabobs Dec 25 '22 at 15:20
  • @Thingamabobs no worries. I think is been handled by `_load_file(...)` as an exception handler. – limitcracker Dec 25 '22 at 15:33
  • Can't [see](https://github.com/RPi-Distro/thonny/blob/master/thonny/code.py#L89) that in the source, which should use this [method](https://docs.python.org/3/library/tokenize.html#tokenize.detect_encoding). When your file is already open your program crashes. – Thingamabobs Dec 25 '22 at 16:08

0 Answers0