Open CharlesFauman opened 1 year ago
@CharlesFauman one possible workaround is to alias the import:
%%python
import random as pyrandom
Is the expected behavior re-definition?
in the mojo tutorial, python imports are just variables.
my initial expectation was that a python module imported in a python block wouldnt be in the mojo namespace at all, because i didn't assign anything. Though after a bit of thought, I think it makes sense it would be included, because the import acts as an assignment
after knowing that it is included, i would expect that attempting to import a mojo module of the same name would fail. For example, the following in mojo:
import time
import math as time
produces an error: error: Expression [1]:6:8: invalid redefinition of 'time' import math as time ^
Expression [1]:5:8: previous definition here import time ^
expression failed to parse (no further compiler diagnostics)
If that doesn't fail, then i would expect that module to now be the new mojo module.
Here's an example not doing anything between python and mojo, but where i think the mojo import is doing something unexpected
%%python
time = "hi"
import time
print(time)
prints <module 'time' (built-in)>
but in mojo,
time = "hi"
import time
print(time)
prints hi
which imo is inconsistent in an unexpected way.
Yep, this is related to the fact that mojo currently lacks true top level code support. The REPL/Jupyter environments fake it a little bit, but it produces some oddities like you note above. In the REPL/Jupyter environments right now, writing:
time = "hi"
import time
print(time)
is closer in actuality to:
import time
def main():
time = "hi"
print(time)
When we finish out support for top-level code, these issues should naturally go away. It's just a bit weird until then, sorry about that!
Bug description
importing and using a mojo module of the same name as one used in a %%python block results in mojo attempting to use the python module
Steps to reproduce
Example 1: cell 1:
cell 2:
Response:
Error: An error occurred in Python.
Example 2: cell 1:
cell 2:
Response:
Error: An error occurred in Python.
System information