M
Modularā€¢4mo ago
bunny

import_path

This post is not a Q, but rather an A for peeps to search out. I'm abusing this Questions forum as a knowledge base, so to say. In this vid, Shashank refs that the file ~/.modular/modular.cfg's setting mojo.import_path can take comma-sep values to identify multiple import paths. The default value is something like:
[mojo]
import_path = /Users/***/.modular/pkg/packages.modular.com_mojo/lib/mojo
[mojo]
import_path = /Users/***/.modular/pkg/packages.modular.com_mojo/lib/mojo
That path contains a bunch of .mojopkg files (like stdlib.mojopkg, algo, math, etc). As the video says, you can add another path, like this:
[mojo]
import_path = /Users/***/.modular/pkg/packages.modular.com_mojo/lib/mojo,/path/to/packages
[mojo]
import_path = /Users/***/.modular/pkg/packages.modular.com_mojo/lib/mojo,/path/to/packages
So, dir /path/to/packages/ is where I direct the "package" command's output. E.g., mojo package SomePkg -o /path/to/packages/SomePkg.mojopkg. As a result, I can do:
from SomePkg import SomeThing
from SomePkg import SomeThing
I can do that without copying the .mojopkg file into every project that uses it. Of note: there is both a [mojo] and a [mojo-nightly] section in the file ~/.modular/modular.cfg. If you want both stable and nightly to import from the same dir, then you need to add ,/path/to/packages to both sections' respective import_path setting. :mojo: :mojonightly:
Modular
YouTube
Modular Community Livestream - MojošŸ”„ on Mac
MojošŸ”„ on Mac (Apple Silicon) is going to be here soon! Join us on our upcoming community livestream as we discuss all things MojošŸ”„ on Mac! We'll walk you through getting started with MojošŸ”„ Mac, discuss Mac specific features, and show you how to build and run MojošŸ”„ applications using Visual Studio on Mac! We have lots of cool demos and a communit...
1 Reply
bunny
bunnyā€¢2mo ago
@ViynShade @MyriadColors Also of note: when you update (i.e. modular update nightly/mojo) then you need to go fix your modular.cfg file again. šŸ˜ Just wrinkles in an early language. Don't let it deter you from playing with Mojo. All languages are a bit rough around the edges when they first emerge onto the scene. I have been using a small update shell script to maintain my custom import_path variable.
#!/bin/sh

modular update mojo
modular update nightly/mojo

# edit path from , to |'
sed -i .bak '\|^import_path[^,]*$|s|$|,/path/to/importable_mojopkg_files|' ~/.modular/modular.cfg
#!/bin/sh

modular update mojo
modular update nightly/mojo

# edit path from , to |'
sed -i .bak '\|^import_path[^,]*$|s|$|,/path/to/importable_mojopkg_files|' ~/.modular/modular.cfg
Explanation: This is a shell (sh) script. It updates Mojo, then Nightly. After that, the final line of the script looks at my modular.cfg file (which was over-written during the updates) and edits any lines starting with import_path and NOT containing a comma inside the line. It then appends ,/Users/.../IMPORTABLE_MOJOPKG_FILES to the end of the line (if no comma found). Also, it retains the original modular.cfg file, renamed as modular.cfg.bak. "Installation": Save that script to a file. Mine is named update-mojo. Move that file in a directory that is in your path, like ~/bin (assuming that is in your $PATH) and be sure to chmod +x the file so that it is executable. Edit the final line so that the /path/to/importable_mojopkg_files fits whatever path you are using. Make sure to keep the final | at the end of the path; that is the seperator for the sed command. Now that everything is setup and the update-mojo file is in your $PATH, you can just do update-mojo every time to update Mojo and Nightly and retain your custom import_path.