Jump to content

Built-in python library urllib not working in plugin?


Recommended Posts

Hey y'all,

 

I am trying to use the urllib standard python library to parse a string to make it safe to put in a URL. This works in a custom Marionette node I made before, except I had to put the imports inside runNode() for it to work properly.

 

It is not working at all in the plug-in command or the plug-in object I'm trying to integrate the code snippet into. I get " AttributeError: module 'urllib' has no attribute 'parse' " whenever I run the plug-in. I checked, and everything's running on Python 3.9. I don't understand why it would work inside a marionette node and not inside a plugin when other libraries are working fine.

 

Here is essentially what the code snippet looks like. Does anybody have any idea what the problem could be?

 

import urllib
import webbrowser

run():
  #do some stuff
  url = urllib.parse.quote(body, safe="")
  webbrowser.open(url)

 

Link to comment

Okay, I've solved my problem, but this is really strange... So I've been doing a little troubleshooting, and it seems like the module "urllib" does get loaded in both Marionette and Plugins/Scripts, but the problem is that urllib doesn't contain any functions in itself, it contains submodules, which are where the problem lies. 

 

The submodules do not get loaded in with the import statement in either scripts or plug-ins. Weirdly, they do get loaded in inside Marionette nodes, which is why I had the inconsistent behaviour.

 

The simple way to get around it is to directly import the submodule. In my case:

from urllib import parse
url = parse.quote(body, safe="")

 

It's just really strange that it behaves this way, and I wonder if anybody more Python knowledgeable knows why. It seems like a bug.

 

If you're interested, here are the relevant results of using inspect.getmembers on the imported modules, they are identical, other than just not including the submodules:

 

in a script or plugin there are no submodules:

Quote

('__cached__', '/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/__pycache__/__init__.cpython-39.pyc'),
('__doc__', None),
('__file__', '/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/__init__.py'),
('__loader__', <_frozen_importlib_external.SourceFileLoader object at 0x4b1d84b80>),
('__name__', 'urllib'),
('__package__', 'urllib'),
('__path__', ['/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib']),
('__spec__', ModuleSpec(name='urllib', loader=<_frozen_importlib_external.SourceFileLoader object at 0x4b1d84b80>, origin='/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/__init__.py', submodule_search_locations=['/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib']))]

 

 

In a marionette node there are the correct submodules:

Quote

 ('__cached__', '/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/__pycache__/__init__.cpython-39.pyc'),
('__doc__', None),
('__file__', '/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/__init__.py'),
('__loader__', <_frozen_importlib_external.SourceFileLoader object at 0x4b1d86fd0>),
 ('__name__', 'urllib'),
('__package__', 'urllib'),
('__path__', ['/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib']),
('__spec__', ModuleSpec(name='urllib', loader=<_frozen_importlib_external.SourceFileLoader object at 0x4b1d86fd0>,
origin='/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/__init__.py', submodule_search_locations=['/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib'])),
('error', <module 'urllib.error' from '/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/error.py'>),
('parse', <module 'urllib.parse' from '/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/parse.py'>),
('request', <module 'urllib.request' from '/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py'>),
('response', <module 'urllib.response' from '/Applications/Vectorworks 2023/Vectorworks 2023.app/Contents/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/response.py'>)]

 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...