modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
23.29k stars 2.59k forks source link

[BUG]: python imports clashing with mojo imports #525

Open CharlesFauman opened 1 year ago

CharlesFauman commented 1 year ago

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:

%%python
import time

cell 2:

import time

time.now()

Response: Error: An error occurred in Python.

Example 2: cell 1:

%%python
import random

cell 2:

import random

random.random_float64()

Response: Error: An error occurred in Python.

System information

11:hugetlb:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7cf598c5_64de_4b6d_803d_5a4a22cb1439.slice/cri-containerd-9878852f50dd1712f2e73453397e5c57d051d7be3034834f5059f8fe68a50a37.scope
10:devices:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7cf598c5_64de_4b6d_803d_5a4a22cb1439.slice/cri-containerd-9878852f50dd1712f2e73453397e5c57d051d7be3034834f5059f8fe68a50a37.scope
9:pids:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7cf598c5_64de_4b6d_803d_5a4a22cb1439.slice/cri-containerd-9878852f50dd1712f2e73453397e5c57d051d7be3034834f5059f8fe68a50a37.scope
8:blkio:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7cf598c5_64de_4b6d_803d_5a4a22cb1439.slice/cri-containerd-9878852f50dd1712f2e73453397e5c57d051d7be3034834f5059f8fe68a50a37.scope
7:cpuset:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7cf598c5_64de_4b6d_803d_5a4a22cb1439.slice/cri-containerd-9878852f50dd1712f2e73453397e5c57d051d7be3034834f5059f8fe68a50a37.scope
6:perf_event:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7cf598c5_64de_4b6d_803d_5a4a22cb1439.slice/cri-containerd-9878852f50dd1712f2e73453397e5c57d051d7be3034834f5059f8fe68a50a37.scope
5:cpu,cpuacct:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7cf598c5_64de_4b6d_803d_5a4a22cb1439.slice/cri-containerd-9878852f50dd1712f2e73453397e5c57d051d7be3034834f5059f8fe68a50a37.scope
4:net_cls,net_prio:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7cf598c5_64de_4b6d_803d_5a4a22cb1439.slice/cri-containerd-9878852f50dd1712f2e73453397e5c57d051d7be3034834f5059f8fe68a50a37.scope
3:memory:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7cf598c5_64de_4b6d_803d_5a4a22cb1439.slice/cri-containerd-9878852f50dd1712f2e73453397e5c57d051d7be3034834f5059f8fe68a50a37.scope
2:freezer:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7cf598c5_64de_4b6d_803d_5a4a22cb1439.slice/cri-containerd-9878852f50dd1712f2e73453397e5c57d051d7be3034834f5059f8fe68a50a37.scope
1:name=systemd:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod7cf598c5_64de_4b6d_803d_5a4a22cb1439.slice/cri-containerd-9878852f50dd1712f2e73453397e5c57d051d7be3034834f5059f8fe68a50a37.scope
0::/
guidorice commented 1 year ago

@CharlesFauman one possible workaround is to alias the import:

%%python
import random as pyrandom
drunkwcodes commented 1 year ago

Is the expected behavior re-definition?

CharlesFauman commented 1 year ago

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.

River707 commented 1 year ago

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!