很难判断你那边究竟做了哪些操作,我这里从零开始举一个例子吧。
我这里先设定一下环境变量 JULIA_DEPOT_PATH=~/Downloads/tmp
来告诉 Julia 使用一个新的部署环境。
可以看到这是一个非常干净的环境。
julia> LOAD_PATH
3-element Vector{String}:
"@" # 当前项目所在目录
"@v#.#" # 当前版本的根项目: $(JULIA_DEPOT_PATH)/environments/v1.7/Project.toml
"@stdlib" # 标准库
然后随便找一个目录创建两个新的包:
(@v1.7) pkg> generate MyPkgA
Generating project MyPkgA:
MyPkgA/Project.toml
MyPkgA/src/MyPkgA.jl
(@v1.7) pkg> generate MyPkgB
Generating project MyPkgB:
MyPkgB/Project.toml
MyPkgB/src/MyPkgB.jl
shell> pwd
/Users/jc/Downloads/pkgtest
假设说我们想要让 MyPkgA
项目里能够正常使用 MyPkgB
的话:
(@v1.7) pkg> activate MyPkgA
Activating project at `~/Downloads/pkgtest/MyPkgA`
(MyPkgA) pkg> st
Project MyPkgA v0.1.0
Status `~/Downloads/pkgtest/MyPkgA/Project.toml` (empty project)
julia> using MyPkgA
[ Info: Precompiling MyPkgA [d9bfe67c-2d5f-4848-978e-d22535e455c7]
julia> using MyPkgB
ERROR: ArgumentError: Package MyPkgB not found in current path:
- Run `import Pkg; Pkg.add("MyPkgB")` to install the MyPkgB package.
Stacktrace:
[1] require(into::Module, mod::Symbol)
@ Base ./loading.jl:967
当 using MyPkgB
的时候,Julia 会按顺序分别在 LOAD_PATH
里寻找符合条件的 MyPkgB
:
@
: 项目目录,即 MyPkgA/Project.toml
。因为现在是空的所以没有。
@v#.#
: 版本根目录,即 ~/Downloads/tmp/environments/v1.7/Project.toml
。因为我重新设定了 JULIA_DEPOT_PATH
所以这也是空的。
@stdlib
:标准库就更不可能有了
所以 using 失败了。
先说一个 “错误” 操作,就是在 @v#.#
里记录 MyPkgB
的信息:
(@v1.7) pkg> dev /Users/jc/Downloads/pkgtest/MyPkgB
Resolving package versions...
Updating `~/Downloads/tmp/environments/v1.7/Project.toml`
[ab7b05f2] + MyPkgB v0.1.0 `~/Downloads/pkgtest/MyPkgB`
Updating `~/Downloads/tmp/environments/v1.7/Manifest.toml`
[ab7b05f2] + MyPkgB v0.1.0 `~/Downloads/pkgtest/MyPkgB`
这样一来无论是在哪个项目(MyPkgA 项目又或者是版本根项目)里,都可以正常使用 MyPkgB
:
(@v1.7) pkg> activate MyPkgA
Activating project at `~/Downloads/pkgtest/MyPkgA`
julia> using MyPkgB # 正常 using
但是需要注意的是 MyPkgA/Project.toml
里其实并没有记录 MyPkgB
,你能够正常导入仅仅是因为 Julia 在 @v#.#
里面找到了。因此如果重启 Julia 并且试图让 MyPkgA
去使用 MyPkgB
的话,就会有警告发生:
# MyPkgA/src/MyPkgA.jl
module MyPkgA
using MyPkgB
greet() = MyPkgB.greet()
end # module
julia> using MyPkgA
[ Info: Precompiling MyPkgA [d9bfe67c-2d5f-4848-978e-d22535e455c7]
┌ Warning: Package MyPkgA does not have MyPkgB in its dependencies:
│ - If you have MyPkgA checked out for development and have
│ added MyPkgB as a dependency but haven't updated your primary
│ environment's manifest file, try `Pkg.resolve()`.
│ - Otherwise you may need to report an issue with MyPkgA
└ Loading MyPkgB into MyPkgA from project dependency, future warnings for MyPkgA are suppressed.
这里警告的意思就是说 MyPkgB
并没有记录在 MyPkgA/Project.toml
里面 (primary environment’s manifest file)。但是因为 Julia 的 LOAD_PATH 里其他的路径 (@v#.#
) 记录了所以倒也能够正常使用。
假如说我们把 MyPkgB
从版本根环境移除的话:
(@v1.7) pkg> st
Status `~/Downloads/tmp/environments/v1.7/Project.toml` (empty project)
然后重开一个 Julia,就会直接报错而不是警告了,因为确实没找到:
(@v1.7) pkg> activate MyPkgA
Activating project at `~/Downloads/pkgtest/MyPkgA`
julia> using MyPkgA
[ Info: Precompiling MyPkgA [d9bfe67c-2d5f-4848-978e-d22535e455c7]
ERROR: LoadError: ArgumentError: Package MyPkgA does not have MyPkgB in its dependencies:
- If you have MyPkgA checked out for development and have
added MyPkgB as a dependency but haven't updated your primary
environment's manifest file, try `Pkg.resolve()`.
- Otherwise you may need to report an issue with MyPkgA
Stacktrace:
结论?你需要在哪一个项目里使用你开发的 MyPkgB
,那就需要在对应的项目 Project.toml (实际上是 Manifest.toml 了) 里面记录对应的 MyPkgB
信息。之所以出现警告大概是因为你之前在版本根项目 (@v#.#
) 里面已经记录了 MyPkgB
的原因。