Using or import search current directory?

The lightest-weight solution is probably to modify LOAD_PATH manually. Although this has been discouraged previously in the thread, there’s currently no alternative if you want to load modules like packages without dealing with the overhead of creating a new package.

Although it’s not framed this way, the manual section on package directories is really an explanation of how Julia deals with entries in LOAD_PATH that don’t have the full structure of a package. It’s not written in a user-friendly way, so here’s the CliffsNotes version:

  • Put your code in a file
  • Wrap it in a top-level module such that the module name exactly matches the file name
  • Put the file in the package directory[1]

Now the module can be loaded like a package. Example:

  • My package directory is ./code
  • It contains a single file code/Foo.jl with the following contents:
    module Foo
    
    export foo
    
    foo() = println("Hello from Foo.jl")
    
    end
    
  • Now let’s load Foo like a package:
    julia> push!(LOAD_PATH, "code");  # add ./code to LOAD_PATH
    
    julia> using Foo
    [ Info: Precompiling Foo [top-level]
    
    julia> foo()
    Hello from Foo.jl
    

I think this is the closest you can currently get to a python-like import .file experience. The only extra overhead is making sure the module and file names match, and a single push to LOAD_PATH.


  1. Alternatively, put the file in a package-like folder structure inside the package directory, that is, instead of adding Foo.jl you can add Foo/src/Foo.jl ↩︎

4 Likes