Reputation: 2991
I am trying to get my head around programming with multiple modules (in different files). I don't want to load explicitly the files with ìnclude
in the right order.
I am using the Atom IDE as my development platform, so I don't run julia explicitly.
when I am just using importall Datastructures
(where ModuleName is the name of the module) julia complains:
LoadError: ArgumentError: Module Datastructures not found in current path.
Run `Pkg.add("Datastructures")` to install the Datastructures package.
while loading F:\dev\ai\Interpreter.jl, in expression starting on line 8
Upvotes: 0
Views: 337
Reputation: 18530
There are two ways to build a package or module in julia:
1) Use the tools in PkgDev
. You can get them with Pkg.add("PkgDev") ; using PkgDev
. Now you can use PkgDev.generate("MyPackageName", "MIT")
(or whatever license you prefer) to build your package folder. By default, julia will build this folder in the same directory as all your other external packages. On Linux, this would ~/.julia/v0.6/
(or whatever version you are running). Also by default, this folder will be on the julia path, so you can just type using MyPackageName
at the REPL to load it.
Note that julia essentially loads the package by looking for the file ~/.julia/v0.6/MyPackageName/src/MyPackageName.jl
and then running it. If your module consists of multiple files, you should have all of them in the ~/.julia/v0.6/MyPackageName/src/
directory, and then have a line of code in the MyPackageName.jl
file that says include("MyOtherFileOfCode.jl")
.
2) If you don't want to keep your package in ~/.julia/v0.6/
for some reason, or you don't want to build your package using PkgDev.generate()
, you can of course just set the files up yourself.
Let's assume you want MyPackageName
to be stored in the ~/MyCode
directory. First, create the directory ~/MyCode/MyPackageName/
. Within this directory, I strongly recommend using the same structure that julia and github use, i.e. store all your code in a directory called ~/MyCode/MyPackageName/src/
.
At a minimum, you will need a file in this directory called ~/MyCode/MyPackageName/src/MyPackageName.jl
(just like in the method above). This file should begin with module MyPackageName
and finish with end
. Then, put whatever you want in-between (including include
calls to other files in the src
directory if you wish).
The final step is to make sure that julia can find MyPackageName
. To do this, you will need ~/MyCode
to be on the julia path. To do this, use: push!(LOAD_PATH, "~/MyCode")
or push!(LOAD_PATH, "~/MyCode/MyPackageName")
.
Maybe you don't want to have to run this command every time you want to access MyPackageName
. No problem, you just need to add this line to your .juliarc.jl
file, which is automatically run every time you start julia. On Linux, your .juliarc.jl
file should be in your home directory, i.e. ~/.juliarc.jl
. If it isn't there, you can just create it and put whatever code you want in there. If you're on a different OS, you'll have to google where to put your .juliarc.jl
.
This answer turned out longer than I planned...
Upvotes: 3