Pkg.add'ing my own modules Question

I am at the point where I feel comfortable using the language and now have a project that will be broken up over several modules. I have had trouble adding my own module though. I followed what was written here: Introducing Julia/Modules and packages - Wikibooks, open books for an open world

I have a projects directory and under it are (simplified):

/projects
  /module1.jl
    /src
      module1.jl
  /module2.jl
    /src
      module2.jl

for each module directory I did git init.

I did push!(LOAD_PATH, “/projects”)

Pkg.add(“module1.jl”) however produces ERROR: unknown package VGTRNG

Have I missed a step or done a step incorrectly? Neither module has a /tests directory yet. Is that an issue? Am I misunderstanding where Pkg.add() will look for packages and I need to do something else to add local packages? Do I need to modify the .git/description?

Hi Peter,
I think you might be confusing modules and packages. Very broadly speaking modules are a way of organizing your code and separating namespaces, whereas packages provide a way to share your code and have users install it easily. See the documentation on modules for more information on modules.

As far as I know, the push!(LOAD_PATH, "/projects") command doesn’t have anything to do with packages. It’s used to add to the list of directories that Julia will search for code. If you want to use statements like

using module1

or

import module1

in your code to brings functions from module1 into scope, you’ll need to tell Julia where to find module1, which in this case is done by appending the “/projects” directory to the LOAD_PATH array.

The Pkg.Add() command is used to install a registered Julia package. If you want to create your own package from scratch, your first step is to run the Pkg.generate("package-name",license) function in Julia. That command will create a folder called “package-name” in your .julia directory. The “package-name” folder will contain a git repository with some boilerplate code and directory structure for creating a package. See here in the documentation for more information. Note that your .julia directory is searched for code by default so if you were to generate a module1 package and move your code into the .julia directory there’s no need to add to the LOAD_PATH in order for julia to be able to find the code.

Finally, packages really start to be useful when you want to share your code with others but as far as I can tell, there’s no harm in using them even if your code is just for you.

1 Like

That is great. Thanks for helping me understand the distinction. The code I am writing is important for my team at work, so I want to share it in an easy manner. I suppose making it available in a local git repo and then making sure my team adds the directory holding these modules to the LOAD_PATH is the right thing for now.

The Pkg.generate function looks like it will be useful in the long run. Thank you so much for the help.

–Pete

Just to be clear, Pkg.add is only good for registered packages so if you don’t want to publish your code publicly you’ll have to use Pkg.clone and Pkg.checkout to initially copy the code and update it.

Slightly topic related, since using SomePkg doesn’t allow for its modules (under the pkg’s directory tree) to mutually refer to (i.e. use) between each other (…the so-called circular dependency problem), I wrote a little module that allows one do so (i.e. bypassing using Pkg). See:

https://github.com/stustd/julia-intra-package-module-usage