import issue
import issue
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
-
- Posts: 169
- Joined: Fri Aug 19, 2016 11:55 am
Re: import issue
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…
And I just see that we have discussed this question before…