Search code examples
nim-lang

Nim import from project root directory


How to import a module from project root directory in Nim?

Instead of doing so:

import ../../http
import ../seq

I'd like to import my module using absolute path relative to the project root directory, like this:

import src/http
import src/utils/seq

How can I achieve that?


Solution

  • This can be done by giving nim the project root directory as a new search path:

    nim c --path:'.' project.nim
    

    And you can drop in a config.nims at the project root with the content --path:"." to automate this.

    So, with a directory tree like

    .
    ├── config.nims
    ├── project.nim
    └── src
        ├── http.nim
        └── utils
            ├── dir
            │   └── editing_me.nim
            └── seq.nim
    

    The editing_me.nim module can then import http and seq as src/http and src/utils/seq. You will still be able to import them with relative paths, too.