Python Requires changes itself

In experimenting with writing plugins, the python version requirements for mine changes itself when in use the gpiozero library.

I am just learning to write these plugins (I do have other programming background) and am starting very basic. I followed the official tutorial, and now want a physical button to respond on the dashboard.
Up to this point, I have specified my plugin requires python >=3,<4
When I add a line that calls a Button() from the gpiozero library, it changes itself to require python >=2.7, <3
This then will not run on my installation (fresh install, Octoprint 1.7.0, Python 3.7.3, OctoPi 0.18.0)
I've played with several different configurations, trying to debug this, and find that it is specifically when I call Button() that it changes.
I have delcared my Python requirements in init.py AND the additional_setup_parameters of setup.py. I found that is how the PhysicalButton plugin by LuxuSam did it, I was trying to copy that part of his.
If I comment out my Button() call, the python requirements return to where I declared them.
If it is helpful, https://github.com/aburtonProto/OctoPrint-Helloworld is my repository for this. Please note, I am just experimenting with things, so names and everything are not all cleaned up.
What is overriding my decision? How can I fix this?

Thanks for any pointers.

maybe specifying a python 3 only version? GitHub - gpiozero/gpiozero: A simple interface to GPIO devices with Raspberry Pi

Edit: Although the version 2 doesn't seem to exist...

Whenever you have an issue with your plugin the first place to look is the octoprint.log. It will tell you all the errors about your configuration.

OctoPrint-Helloworld/octoprint_helloworld at 58f6ca590dbddf454ddb07e01917c549fac1f45c · aburtonProto/OctoPrint-Helloworld · GitHubinit.py#L12

It looks like you might have an indentation problem if the button line was uncommented. It should line up with the line above it. If you don't have your indentation correct, Python will stop reading the file, which means the plugin Python compatibility entry is ignored and nothing from the plugin runs.

How did you notice that indentation error‽
I had used a tab instead of spaces on accident on that line.

The error logs said "can't be compiled under Python 3.7.3 due to invalid syntax". I guess when it can't compile for Python3, it assumes it must work in 2 and changes the requirement for you. But my system doesn't have 2, so it just disables the plugin and quits trying.
Tracking down the syntax error to using a tab where a space should have been.

Thank you for the pointers!

Just to make this clear, OctoPrint never changes any requirements for you. It doesn't edit code or anything like that. When a plugin can't be loaded under the current Python version, OctoPrint will stop trying to load it.

Interestingly, when I looked at it on my phone (when I replied to you initially) the tabs were showing the same size as 8 spaces, so it was really obvious. Looking at it on my PC now, it's the same size so I would have no hope.

1 Like

In the plugin manager it where it lists the Python version requirement, it was changed.
In the attached images you can see my bugged version lists >=2.7,<3, which I did not specify.
After fixing the error in my code it lists >=3,<4 as I specified.

So it may not be changing the requirements in my file, the error manifests as though the requirements had been changed. It doesn't change my code, but when it stops trying to load the plugin it does list different requirements instead of saying there was an error in the plugin manager. Granted, as Charlie_Powell suggested, the error was there in the octoprint.log.
I had been searching for solutions in the wrong place, as I had assumed OctoPrint changed my requirements.

For future people looking to see why they think their requirements are being changed, hopefully this helps.
I appreciate the speed and helpfulness of this community!

What's happened here is as it stopped trying to load the file, the __plugin_pythoncompat__ line was not read. The default (for backwards compatibility before that was introduced) is Python 2 only, so that's how it looked like that.

I tend to keep either the console log (from running octoprint serve) or the octoprint.log file open all the time when developing plugins, whenever anything goes wrong it is always in there :slight_smile:

2 Likes