Can I run `include("file")` for a file stored in a loaded package

let’s say I have a package, which in its main directory contain a file “some_file.jl”. if I run

using MyPackage

is it possible to somehow run

include("MyPackage.some_file.jl")

I.e. run some file contained in the package?

Something like

include(pkgdir(MyPackage, "src", "some_file.jl"))

should work. See the docs.

1 Like

That worked, thanks a lot :slight_smile:

Note, though, that there are contexts — particularly compiled contexts — where you won’t have access to that directory or file.

This kinda looks like eval-laundering to me — there’s lots of guidance about not using eval, but that’s effectively what it seems you’re wanting to do here. And it’s effectively what include does. You could alternatively parse that file into the module as a const value, and then just have folks explicitly eval(MyPackage.SOME_FILE), where that was just defined in the module as a const SOME_FILE = parse(read("some_file.jl", String)).

If I can guess at your root use-case, many packages implement similar functionality with a zero-argument macro.

2 Likes