Nim 从项目根目录导入

huangapple go评论44阅读模式
英文:

Nim import from project root directory

问题

如何在Nim中从项目根目录导入模块?

而不是这样做:

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

我想要使用相对于项目根目录的绝对路径导入我的模块,就像这样:

import src/http
import src/utils/seq

我如何实现这一点?

英文:

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?

答案1

得分: 3

这可以通过将项目根目录作为新的搜索路径传递给Nim来实现:

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

并且您可以在项目根目录下放置一个名为config.nims的文件,其内容为--path:".",以实现自动化。

因此,如果目录树如下所示:

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

然后,editing_me.nim 模块可以导入 httpseq,分别为 src/httpsrc/utils/seq。您仍然可以使用相对路径导入它们。

英文:

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.

huangapple
  • 本文由 发表于 2023年4月16日 23:07:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定