When using the import
statement, how/where does Nim perform its search for modules?
I know that file paths can be used, but if I don't want to use a file path, where would I put the module I defined locally on my machine?
I haven't used Nimble yet and I assume that's one way, but I'm more interested in how it would be done if the module is only defined locally.
Nim searches for modules in the following places:
import dir.modname
or import dir/modname
if it's not in the same directory).import dir.modname
or import dir/modname
if it's not in the same directory).--path
or -p
.~/.nimble/pkgs
.The command nim dump
will show you all the module search paths being used (other than Nimble packages).
You can do the following to use your own modules:
--path/-p
command line option.libname.nimble
(replacing libname
with the actual name) file in the directory and use nimble install
. You can then import the *.nim
files in that library directly from any other project. Use nimble uninstall libname
to uninstall the library again.A basic libname.nimble
file has the following contents.
[Package]
name = "libname"
author = "Your Name"
version = "0.1"
description = "Example library."
license = "none"
You basically have two options:
You can pass a search path in the compilation command: nim c -p:~/myModulePath
.
You can add search paths to your config/nim.cfg
by path="$home/myModulePath"
. This file also tells you exactly how/where Nim looks for imports by default.
The nim dump
command will show you what's in your path. Take a look at the documentation of the advanced compiler options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With