Why are files in other folder being loaded instead of the files in the current folder?

My project directory was a little crowded and I decided to start a project from scratch in another directory nearby. I followed these steps.

  1. Created a new project folder C:\Dropbox\Code\Julia\Model by running

generate "C:/Dropbox/Code/Julia/Model"

in the pkg REPL.

My old project was in C:\Dropbox\Code\Julia\demo.

  1. Added a couple of packages using the pkg REPL. The packages show up when I type status in the pkg REPL.

  2. Copied some of the files from the old project folder into "C:/Dropbox/Code/Julia/Model/src". (I was not sure about the src folder but it seemed reasonable the files should be there).

  3. One of the files in the old and new projects does not have a module statement anywhere. Its name is MyProgram.jl. I run the project by selecting this file in VSCode and then hitting F5 or Ctrl+F5.

While doing this I got strange crashes. After trying many things I discovered the following:

a) At some point a function that is defined in another module, named ModelFunctions.jl (which is properly imported with import ModelFunctions into MyProgram.jl) is called with the command

ModelFunctions.optimize(some parameters).

b) The code in the old ModelFunctions.jl (meaning the file in the old project C:\Dropbox\Code\Julia\demo\src) is being executed, even though I am starting the program by hitting F5 while the file C:\Dropbox\Code\Julia\Model\MyProgram.jl is active on the screen. (There is a rectangular box around its name)

c) The code in the new ModelFunctions.jl in C:\Dropbox\Code\Julia\Model\src is not executed

d) I could not find anything in Project.toml or Manifest.toml that could be inducing this behavior.

e) Including

Pkg.activate("C:/Dropbox/Code/Julia/Model")

at the top of "C:/Dropbox/Code/Julia/Model/src/MyProgram.jl" did not help even though there was a message stating the project was activated.

f) Adding the line

cd("C:\\Dropbox\\Code\\Julia\\Model\\src")

to "C:/Dropbox/Code/Julia/Model/src/MyProgram.jl" did not help either.

Can anyone suggest a way to have the “right” file be executed? Thanks in advance.

What is the value of the variable LOAD_PATH ?
It will determine which source file is executed by import ModelFunctions

1 Like

Thanks for the suggestion. I added the variable LOAD_PATH with contents C:\Dropbox\Code\Julia\Model to both the User and System variables and rebooted the computer. Nothing changed: the file in the Julia\demo\src folder continues to load when I run the program. Here is the error message:

Stacktrace:
  [1] SubDataFrame
    @ C:\Users\fsald\.julia\packages\DataFrames\vuMM8\src\subdataframe\subdataframe.jl:78 [inlined]
  [2] view(adf::DataFrames.DataFrame, rowinds::UnitRange{Int64}, colinds::Colon)
    @ DataFrames C:\Users\fsald\.julia\packages\DataFrames\vuMM8\src\subdataframe\subdataframe.jl:136
  [3] calculate_statistic(model_dict::Dict{String, Any}, i_parameter::Tuple{Int64, Dict{String, Int64}}, indexes::UnitRange{Int64}, report_progress::Bool, verbose::Bool)
    @ ModelFunctions C:\Dropbox\Code\Julia\demo\src\ModelFunctions.jl:52
  [4] calc_stat
    @ C:\Dropbox\Code\Julia\demo\src\ModelFunctions.jl:114 [inlined]
  [5] iterate
    @ .\generator.jl:47 [inlined]
  [6] collect(itr::Base.Generator{Base.Iterators.Enumerate{Vector{Dict{String, Int64}}}, ModelFunctions.var"#calc_stat#13"{Bool, Bool, Dict{String, Any}, UnitRange{Int64}}})
    @ Base .\array.jl:678
  [7] optimize(model_dict::Dict{String, Any}; parallel::Bool, report_progress::Bool, verbose::Bool)
    @ ModelFunctions C:\Dropbox\Code\Julia\demo\src\ModelFunctions.jl:132
  [8] top-level scope
    @ c:\Dropbox\Code\Julia\Model\src\MyProgram.jl:93
  [9] include(fname::String)
    @ Base.MainInclude .\client.jl:444
 [10] startdebug(socket::Base.PipeEndpoint, error_handler::VSCodeDebugger.var"#3#4"{Tuple{String, String}})
    @ VSCodeDebugger.DebugAdapter c:\Users\fsald\.vscode\extensions\julialang.language-julia-1.4.3\scripts\packages\DebugAdapter\src\packagedef.jl:93
 [11] startdebugger()
    @ VSCodeDebugger c:\Users\fsald\.vscode\extensions\julialang.language-julia-1.4.3\scripts\packages\VSCodeDebugger\src\VSCodeDebugger.jl:38
 [12] top-level scope
    @ c:\Users\fsald\.vscode\extensions\julialang.language-julia-1.4.3\scripts\debugger\run_debugger.jl:9
in expression starting at c:\Dropbox\Code\Julia\Model\src\MyProgram.jl:93

Julia debuggee finished. Press ENTER to close this terminal.```

Is there a way to debug the ```import``` statement and find why it is importing the file from the ```Julia\demo\src``` folder and not from the ```Julia\Model\src``` folder?

I managed to find how to use the code in modules. For the benefit of those who may find themselves in a similar predicament, here are three steps:

a) Have an include statement for each one of your files with modules. If these files are in the src directory you need something like

include("./MyModule.jl")

There is no need to add src to the path.

b) For each file add something like

using .MyModule (notice the dot before the “M”, it is needed)

import may be used instead of using.

c) This will not work:

using .MyModule as mm

The as clause does not seem to be allowed.

I am glad you solved the issue.
You may want to read the julia documentation about code loading and modules.

Generally if you construct a package, you should not need the dot in „using .SomeModule“
I think this stems from the fact that you include the files defining the modules (in main Scope). The alternative is to use LOAD_PATH as discussed in the Julia manual.

Thanks for your comments. My understanding is the include/dots combination is necessary for modules that are not inside packages, but are not needed for packages. Since my code is changing rapidly I decided not to create packages yet. I am not sure this reasoning makes sense, though. Maybe it is as easy to change packages as it is to change modules.

your understanding is correct.
I you don’t know it yet, I highly recommend GitHub - timholy/Revise.jl: Automatically update function definitions in a running Julia session
It will make development of code much easier (changes in function definitions are automatically applied).
I am not sure if Revise works with ‘include’ (I don’t think so), but there seems to be a includet function instead User reference · Revise.jl

Generating a package seems a bit cumbersome if you have never done it, but it is quite simple.
consider Pkg.generate("foo") and have a look at GitHub - invenia/PkgTemplates.jl: Create new Julia packages, the easy way