Setting up tests: problems with `using myModule`

testing
module

#1

I’m attempting to create my own module (for the purpose of educating myself) and write tests for it. However, when attempting to call using myOpti in my test, I keep running into the error

ERROR: LoadError: ArgumentError: Package myOpti not found in current path: Run import Pkg; Pkg.add("myOpti") to install the myOpti package.

In the directory Users/natemcintosh/Documents/Julia/basicOptimization I have set up a git repository. Within the repository is a src directory and a test directory.

In src are two files: myOpti.jl and problem_set_1a.jl.

problem_set_1a.jl is where I keep the functions I want to be in the module.

myOpti.jl contains only the code
module myOpti include("problem_set_1a.jl") end # module ps1a

At the moment, problem_set_1a.jl does not export anything. Is this something I need to do?

I’ve also added the path to the LOAD_PATH so that Users/natemcintosh/Documents/Julia/basicOptimization now appears at the end of the LOAD_PATH.

Any suggestions to get this to work?


#2

Probably the easiest thing to do is:

  1. Reorganize your files a little bit so that the file defining your module lives in ...basicOptimization/myOpti/src/myOpti.jl
  2. Undo any modifications to your LOAD_PATH (it’s no longer needed since Julia 1.0)
  3. Start Julia, press ] to get the pkg> prompt, and then do:
pkg> dev /Users/natemcintosh/Documents/Julia/basicOptimization/myOpti

#3

Horay! It now finds the module and begins to compile! Thank you so much for the help!

Now I have a new error, but I suppose that’s the definition of progress; moving from one error to the next.

[ Info: Precompiling myOpti [ce8f6948-e2c0-503c-b155-f6049321a3fd]
┌ Warning: Package myOpti does not have CSVFiles in its dependencies:
│ - If you have myOpti checked out for development and have
│ added CSVFiles 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 myOpti
└ Loading CSVFiles into myOpti from project dependency, future warnings for myOpti are suppressed.
WARNING: using myOpti.myOpti in module Main conflicts with an existing identifier.
ERROR: LoadError: importing myOpti into Main conflicts with an existing identifier
in expression starting at /Users/natemcintosh/Documents/Julia/basicOptimization/myOpti/test/test_ps1.jl:3

I suppose now I have to add dependencies? Is this something I do with a REQUIRE file?


#4

The error is just because you probably already had a module defined with the same name in your existing Julia session. Restarting Julia should fix it.

Adding dependencies via a REQUIRE file will still work, but that’s the old way of doing things and will eventually be deprecated. The new approach is via https://docs.julialang.org/en/v1.0/stdlib/Pkg/#Creating-your-own-packages-1

For example, to declare that myOpti depends on CSVFiles, you could do:

<start a new julia session>
pkg> activate <whatever/basicOptimization/myOpti 
pkg> add CSVFiles

Note that you might find it easier to let Julia generate the basic package structure for you to avoid having to figure out some of these things in the future. That’s as easy as:

pkg> generate MyOptiPackage

which will create a Project.toml file and a module source file for you, with the right folder structure already.