Skip to content

Feature Request: Support python package installation somehow #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
raman325 opened this issue Oct 30, 2020 · 5 comments · Fixed by #68
Closed

Feature Request: Support python package installation somehow #66

raman325 opened this issue Oct 30, 2020 · 5 comments · Fixed by #68

Comments

@raman325
Copy link
Contributor

raman325 commented Oct 30, 2020

I love that pyscript lets you import any package to use in automations, but I am somewhat limited in the packages I can use because I run HomeAssistant in a Docker container, and I would have to either manually install the packages each time I rebuilt the container or create my own Dockerfile that did the same. It would be great if there was a mechanism through pyscript to install packages if they are not found. I am not sure what the best way to achieve this would be, there are two problems to consider:

  1. How to determine what packages need to be installed and their versions? HomeAssistant achieves this through the use of manifest.json which lists the dependent package and version.
  2. How to install the package? You can probably use the code here as a reference https://github.com/home-assistant/core/blob/dev/homeassistant/requirements.py#L105

Open questions:

  1. How would pyscript deal with version mismatches between what HomeAssistant expects and what pyscript wants? My take is that we should always defer to HomeAssistant requirements to prevent issues with the core application

Thanks for the consideration!

@dlashua
Copy link
Contributor

dlashua commented Oct 30, 2020

This may be a difficult goal.

The only real solutions are 1) fully manual, and 2) pyscript installed

For fully manual, as you mentioned, you'd have to install the packages. I don't think it would be too difficult to alter the homeassistant docker image to allow user injectable requirements. And, most likely, a change like this in a PR to the homeassistant docker would most likely be accepted.

Another option in fully manual is to use hass_is_global, access that very method you linked to above, and have your pyscript install the modules. I don't know for SURE that pyscript will allow it, but, it might.

For pyscript installed, the simplest solution is to have a user editable requirements.txt file. Pyscript could scan this before starting up and ensure all modules were installed. An even better solution would involve special variables in the py files to indicate that modules are needed and pyscript code to check it and ensure it happens.

As far as version mismatches go, I don't think there is a solution other than "defer to Home Assistant". Anything else could potentially lead to a broken Home Assistant.

@raman325
Copy link
Contributor Author

raman325 commented Oct 30, 2020

I haven't tested this yet, but I looked into this and put together what I think would allow this to work for pyscript if it was desirable: master...raman325:install_packages

The logic here is that when loading scripts (either on initial setup or on reload), pyscript will check for a requirements.txt. If it's there, it will parse the requirements line by line, and if the package is already installed, it will skip it, regardless of the version, because we will defer to other application's dependencies (like HA). If a package is not already installed, it will install the package using HA's installation mechanism. I was originally worried that this would create a potential conflict in the future, e.g. I install packageA v2 through pyscript but then later add an integration in HA that requires packageA v1. At that point though, HA will downgrade the package, so there would just need to be a caveat in the docs that this could occur, and to refer to https://github.com/home-assistant/core/blob/dev/requirements_all.txt as to what version you should expect.

Thoughts?

@dlashua
Copy link
Contributor

dlashua commented Oct 31, 2020

I haven't check all of the code, but this approach seems pretty straight forward and lets code already in HASS core do the heavy lifting which is nice.

@raman325
Copy link
Contributor Author

I went through some iterations of testing and I'm pretty satisfied with the approach after some tweaks. I would like to add some tests at some point but I am going to go ahead and submit a PR to get Craig's feedback

@raman325
Copy link
Contributor Author

tests added!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants