MrYsLab / pseudo-microbit

A pseudo implementation of the micro:bit micropython API.
Other
16 stars 4 forks source link

Allow to import the microbit package and modules in python 3.8 #1

Closed kopp closed 4 years ago

kopp commented 4 years ago

Problem description

When trying to import microbit with python3.8 I get import errors. This pull request

  1. restructures the code to allow the import
  2. creates some proper objects (instead of just declarations) similar to what can be found on a microbit

Error Logs

% python                                                   :(
Python 3.8.0 (default, Oct 23 2019, 18:51:26) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import microbit
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "X/pseudo-microbit/microbit/__init__.py", line 31, in <module>
    from . import (display as display, uart as uart, spi as spi, i2c as i2c,
  File "X/pseudo-microbit/microbit/display.py", line 12, in <module>
    from . import Image
ImportError: cannot import name 'Image' from partially initialized module 'microbit' (most likely due to a circular import) (X/pseudo-microbit/microbit/__init__.py)

% python
Python 3.8.0 (default, Oct 23 2019, 18:51:26) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from microbit import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "X/pseudo-microbit/microbit/__init__.py", line 31, in <module>
    from . import (display as display, uart as uart, spi as spi, i2c as i2c,
  File "X/pseudo-microbit/microbit/display.py", line 12, in <module>
    from . import Image
ImportError: cannot import name 'Image' from partially initialized module 'microbit' (most likely due to a circular import) (X/pseudo-microbit/microbit/__init__.py)

% python
Python 3.8.0 (default, Oct 23 2019, 18:51:26) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from microbit import spi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "X/pseudo-microbit/microbit/__init__.py", line 31, in <module>
    from . import (display as display, uart as uart, spi as spi, i2c as i2c,
  File "X/pseudo-microbit/microbit/display.py", line 12, in <module>
    from . import Image
ImportError: cannot import name 'Image' from partially initialized module 'microbit' (most likely due to a circular import) (X/pseudo-microbit/microbit/__init__.py)
MrYsLab commented 4 years ago

Thanks for taking the time to investigate and supplying the pull request. I have no objections in accepting a pull request, but using Python 3.8 on Windows with Pycharm 2019.3.1 Professional Edition, I do not get any exceptions. Could you please provide some additional detail as to how I can recreate the exceptions you are seeing? Thanks.

kopp commented 4 years ago

All I did was trying to do an import of the code in a running instance of python. In case your editor/tool of choice actually wants to execute a python script importing microbit you'll see this error as well.

MrYsLab commented 4 years ago

Again I appreciate your efforts, but I think you are trying to use this package in a way it was not intended to be used. When it was released there were no editors specifically available for the micro:bit and so I developed this package for folks to be able to use the Pycharm IDE to provide type checking and type hints while editing code. It also adds tools to PyCharm to allow for minimizing the size of the source code and for uploading the code to the micro:bit

The micro:bit uses a flavor of MicroPython that supports micro:bit specific hardware and MicroPython that itself is not python 3.8 compatible. This package has also been incorporated into the Pycharm plugin for general MircoPython support. The package still works with PyCharm in conjunction with Python 3.8 without any exceptions, so it still works as "advertised".

I am not questioning the worth or use of your proposed changes, but they would radically alter things in a way that would have this package depart from its original intent. What I would suggest is that you create a package of your own. If you are unfamiliar with creating a Python package, or the mechanics of GitHub, I would be glad to help you to achieve your goal, but I am going to deny merging your changes. I hope you understand my reasoning. If you have any comments or questions they are always welcome and you may enter them here and I will see them even though I am closing the pull request.

kopp commented 4 years ago

I totally understand your reasoning and I would like to thank you for the polite way of replying!

Out of curiosity: Would the pseudo-microbit package be a candidate for a microbit-stubs package as defined in PEP 561 (with file names changed .py -> .pyi)?

MrYsLab commented 4 years ago

I am not terribly experienced with the PEP, but it looks like the PyCharm extension for micro: bit, which is based upon my code, uses this scheme. The extension is actually written in Kotlin, but uses the Python code and may be able to give you some hints on how to organize things. Looking at some of my older releases of pseudo-microbit, I also had some .pyi files but apparently I abandoned them for straight .py files at some point. I don't remember why I did that. Perhaps for consistency or because the .pyi files caused some issues. Sorry that I could not be of more help on this subject. Again, if you have any questions, I would be happy to answer if I can.

kopp commented 4 years ago

I am currently struggeling with .pyi files myself. For future reference, one of the issues I encountered is the lack in tooling support by e.g. pylint as in this issue.

MrYsLab commented 4 years ago

I am not sure if you have seen this article or if it may be useful, but I am passing it on, just in case it can help.

There is also some information about additional types in Python 3.8 in this article.