M
Modular11mo ago
EzRyder

Jupyter - Import Custom Mojo Modules?

How can we import and use a custom Mojo module from a Jupyter notebook? I added details of what I've tried so far by replying to an existing post started by @hogepodge. Thanks! Here's a link to that post: https://discord.com/channels/1087530497313357884/1119100298456215572/1198684128091058198
2 Replies
EzRyder
EzRyderOP11mo ago
So I think I have some VS Code weirdness going on, and maybe an issue with Mojo version 0.6.1. With the Jupyter notebook open, VS Code highlighted the import statement saying it was unable to locate module 'my_name', so I prefixed it with the current folder shared.my_name which made the type checker happy but gave an error when I tried to run the Mojo code. I removed the extra shared. and it now runs as expected. Perhaps also important...I updated to Mojo version 0.6.3 Hope this helps anyone else trying to import custom modules in a Jupyter notebook. Cheers...EzRyder Edit: In VS Code, I found that I needed to restart the kernel in order for changes in a Mojo module to propagate into the open Jupyter file. Module: my_name.mojo
struct MyName(Stringable):
var name: String

fn __init__(inout self, name: String):
self.name = name

fn __str__(self) -> String:
return str(self.name)
struct MyName(Stringable):
var name: String

fn __init__(inout self, name: String):
self.name = name

fn __str__(self) -> String:
return str(self.name)
Jupyter
from my_name import MyName

let username = "Bob"
let name = MyName(username)
print("Hi " + str(name) + "!")
from my_name import MyName

let username = "Bob"
let name = MyName(username)
print("Hi " + str(name) + "!")
Output: Hi Bob!
ModularBot
ModularBot11mo ago
Congrats @EzRyder, you just advanced to level 2!
Want results from more Discord servers?
Add your server