0

I'm trying to modify an existing Python script even though I'm very inexperience in Python.

The script is a __init__.py file and is a Plex Plugin for a custom Agent.

The exiting relevant section of code looks like:

class CustomAgent(Agent.Movies):
    def update(self, metadata, media, lang):
        uri = "http://domain.tld/file?id=%s" % (urllib.quote(metadata.id))
        data = JSON.ObjectFromURL(uri)

Instead of reaching out to a website I'm trying to have the agent open up a tsv file to get values for various fields.

class CustomAgent(Agent.Movies):
    def update(self, metadata, media, lang):
        with open("db.tsv", "r") as fp:
            line = fp.readline()
            while line:
                ...

My problem is I'm getting the "NameError: global name 'open' is not defined" error. At first I thought I didn't import something that I needed to import but couldn't find any relevant libraries(?).

Searching online suggests (if I'm interpreting it right) that it's because the script is shutting down before the relevant code gets executed. But I don't understand how to resolve the issue.

I think the issue might be because it's an __init__.py file but I didn't choose the name, its what the Plex Plugin Framework has as the name and I don't think it's something I can change.

How can I open another file in Python?

deceze
  • 510,633
  • 85
  • 743
  • 889
GFL
  • 1,278
  • 2
  • 15
  • 24
  • 2
    That's an odd error. `open()` is a built-in function name, so it should always be available. At any point in the code, do you ever assign to that name? i.e. does the code have `open = ...` anywhere? – John Gordon May 08 '23 at 00:27
  • 1
    It would have to be `del open` somewhere to cause this error, merely reassigning the name would cause a different problem. – jasonharper May 08 '23 at 00:32
  • @JohnGordon "open" is not referenced anywhere else in the short script and no other scripts are included in it, though I don't know what kind of environment Plex is running the script in when it gets called. – GFL May 08 '23 at 00:48
  • If you choose to avoid it directly, use "Path.open()?" like:`Path("db.tsv").open() as fp: ...` – 911 May 08 '23 at 03:10
  • @911: Interestingly, it appears that Plex may not provide any of the built-ins (see links in my answer). If that's the case, any library function that tries to use a built-in (like `Path().open` if it uses `open`) will probably fail as well. It also like they greatly restrict what you can do in agents, such as providing comms-based methods to communicate with the Plex core but not much else. – paxdiablo May 08 '23 at 07:54

1 Answers1

1

A few possibilities to check:

  • Make sure you have actually typed open and not some weird Unicode-similar thing. A hex or octal dump should be able to tell you that, it will be 157 160 145 156 in octal or 6f 70 65 6e in hex.

  • Make sure you haven't somehow modified or deleted the open binding, by using print(open). This should give you something like <built-in function open>. If it's something else (or it doesn't exist), you have an issue. You can also see what "open" in __builtins__.__dict__ returns, it should normally be true.

  • Check if the environment your Python script is running in a a "proper" one. For example, if it's running on a PC and using the Plex module to just connect to the Plex server over the network, it's probably okay. However, if it's running on the Plex server, there could be some funky modifications to the Python interpreter to stop you accessing the file system (that's one possibility, there could be all sorts of other limitations it may impose).


On that second point, I don't believe you can del open (or any other built-in) but you can do something like del __builtins__.open or del globals["__builtins__"]["open"].

When I do that and then attempt to open a file, I get an error without the word global in it, though that's most likely just a difference in output between Python versions. It looks like the global word was removed from that message in Python 3.4 (alpha 1), meaning you're using a version prior to that. See issue 17032 for detail.


On that third point, it looks like Plex does actually run in a limited environment. This question seems to indicate no built-ins are available.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I did copy and paste "open" from an online script and that has messed me up before with some hidden non-printable characters. I just deleted and retyped the line but I still get the same error. I also searched the rest of the script and "open" is not referenced at all. – GFL May 08 '23 at 00:46
  • @GFL, you would also have to check all modules/packages that are imported by the script as well. And recursively. What did you see when you printed it (the second bullet point)? – paxdiablo May 08 '23 at 00:54