Reputation: 2578
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.
Upvotes: 10
Views: 5590
Reputation: 18375
The nim dump
command will show you what's in your path. Take a look at the documentation of the advanced compiler options.
Upvotes: 1
Reputation: 8740
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"
Upvotes: 15
Reputation: 26709
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.
Upvotes: 3