import issue

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
pmulvey
Posts: 45
Joined: Sun Jul 29, 2018 8:12 am
Location: Athlone, Ireland

import issue

Post by pmulvey » Sat Jul 04, 2020 7:21 pm

I have a module that I import after "from machine import I2C". The module code does not recognise the i2c name unless I put this line in the module as well. What's the explanation?
Paul Mulvey

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: import issue

Post by Christian Walther » Sat Jul 04, 2020 8:10 pm

The explanation is that that’s how imports work. They import into the context of the current module, which is not shared with other modules. Every module has its own context, it doesn’t inherit the one from which it is imported. Your solution of putting the import into every module that needs it is the correct one. Are you used to “#include” in C/C++, or what makes you expect otherwise? Modules in Python work differently than compilation units in C/C++.

And I just see that we have discussed this question before…

Post Reply