Best practices for bringing in requests module to a hook?

Sorry, something of a python newbie.

I’m modified the publisher hook to execute some commands using an external Rest API.

I want to use the requests module to confirm that it is functional before continuing. For other self-contained modules I’ve made I’ve been able to follow the advice here:

But requests has lots of dependencies… and I’m not sure the best way to handle that.

I was able to install requests (and all dependencies) in a venv and the copy the contents of that site-packages into the hooks folder, and then use sys.path.append(os.path.dirname(file)) to import them all. But this doesn’t seem right to me… and I’m worried I’d cause conflicts down the line.

appreciate the insight!

1 Like

Welcome to the community @shuwei

Im not a senior on python but, maybe, you could use another way to connect to that external Rest API you need, another module like urllib maybe

There isn’t much of a way of going around needing the module in the path (tk frameworks being one, since they are in a path managed by toolkit).
I actually went with the simplest solution, which is to add system Python to PATH when needed, and it works decently. Haven’t had conflicts yet, but it is not ideal, yes. venv definitely better.
You can do the same with a venv, don’t even need to copy site-packages, just use the path to it
sys.path.append("venv/.../site-packages"). This way you can install the same venv wherever needed.
Another solution is to add the path to site-packages in the PYTHONPATH environment variable, which you can control from elsewhere, if that suits you better.