Why some variables in a package are not properly included when calling that package

I do not understand why some functions and variables are not included properly in my project, and as a result, I get an error: UndefVarError: something not defined. I am working with SymbolicWedderburn.jl (@abulak ). However, I think my questions are more general.

I am working in Windows. I have one directory where all of my packages are stored, namely: C:\Users\raven.julia\packages

When I am in the directory C:\Users\raven.julia\packages\SymbolicWedderburn\lbiby\examples If I run

Pkg.activate(@__DIR__)
Pkg.instantiate()
include(joinpath(@__DIR__, "ex_C2_linear.jl"))
include(joinpath(@__DIR__, "ex_S4.jl"))
include(joinpath(@__DIR__, "ex_motzkin.jl"))
include(joinpath(@__DIR__, "ex_robinson_form.jl"))

I get

Test Summary:      | Pass  Total  Time
Decompose in basis |   16     16  0.2s
Test Summary:                 | Pass  Total  Time
induced Matrix Representation |    5      5  2.2s
ERROR: LoadError: UndefVarError: `AP` not defined
Stacktrace:
 [1] top-level scope
   @ c:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\action_polynomials.jl:20
in expression starting at c:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\action_polynomials.jl:20
in expression starting at c:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\ex_S4.jl:6

I downloaded the SymbolicWedderburn package and stored it somewhere else, namely: F:\darsi\jl
Now, when I am in the directory F:\darsi\jl\SymbolicWedderburn.jl-master\examples if I run the same script, I get

Decompose in basis: Error During Test at f:\darsi\jl\SymbolicWedderburn.jl-master\examples\ex_C2_linear.jl:40
  Got exception outside of a @test
  UndefVarError: `StarAlgebras` not defined
  Stacktrace:
    [1] macro expansion
      @ f:\darsi\jl\SymbolicWedderburn.jl-master\examples\ex_C2_linear.jl:43 [inlined]
    [2] macro expansion
      @ C:\Users\raven\AppData\Local\Programs\julia-1.10.4\share\julia\stdlib\v1.10\Test\src\Test.jl:1577 [inlined]
    [3] top-level scope
      @ f:\darsi\jl\SymbolicWedderburn.jl-master\examples\ex_C2_linear.jl:41
    [4] include(fname::String)
      @ Base.MainInclude .\client.jl:489
    [5] top-level scope
      @ f:\darsi\jl\SymbolicWedderburn.jl-master\examples\run_examples.jl:4
    [6] eval
      @ .\boot.jl:385 [inlined]
    [7] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
      @ Base .\loading.jl:2076
    [8] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::@Kwargs{})
      @ Base .\essentials.jl:892
    [9] invokelatest(::Any, ::Any, ::Vararg{Any})
      @ Base .\essentials.jl:889
   [10] inlineeval(m::Module, code::String, code_line::Int64, code_column::Int64, file::String; softscope::Bool)
      @ VSCodeServer c:\Users\raven\.vscode\extensions\julialang.language-julia-1.83.2\scripts\packages\VSCodeServer\src\eval.jl:271
   [11] (::VSCodeServer.var"#69#74"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
      @ VSCodeServer c:\Users\raven\.vscode\extensions\julialang.language-julia-1.83.2\scripts\packages\VSCodeServer\src\eval.jl:181
   [12] withpath(f::VSCodeServer.var"#69#74"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams}, path::String)
      @ VSCodeServer c:\Users\raven\.vscode\extensions\julialang.language-julia-1.83.2\scripts\packages\VSCodeServer\src\repl.jl:276
   [13] (::VSCodeServer.var"#68#73"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
      @ VSCodeServer c:\Users\raven\.vscode\extensions\julialang.language-julia-1.83.2\scripts\packages\VSCodeServer\src\eval.jl:179
   [14] hideprompt(f::VSCodeServer.var"#68#73"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})
      @ VSCodeServer c:\Users\raven\.vscode\extensions\julialang.language-julia-1.83.2\scripts\packages\VSCodeServer\src\repl.jl:38
   [15] (::VSCodeServer.var"#67#72"{Bool, Bool, Bool, Module, String, Int64, Int64, String, VSCodeServer.ReplRunCodeRequestParams})()
      @ VSCodeServer c:\Users\raven\.vscode\extensions\julialang.language-julia-1.83.2\scripts\packages\VSCodeServer\src\eval.jl:150
   [16] with_logstate(f::Function, logstate::Any)
      @ Base.CoreLogging .\logging.jl:515
   [17] with_logger
      @ .\logging.jl:627 [inlined]
   [18] (::VSCodeServer.var"#66#71"{VSCodeServer.ReplRunCodeRequestParams})()
      @ VSCodeServer c:\Users\raven\.vscode\extensions\julialang.language-julia-1.83.2\scripts\packages\VSCodeServer\src\eval.jl:263
   [19] #invokelatest#2
      @ .\essentials.jl:892 [inlined]
   [20] invokelatest(::Any)
      @ Base .\essentials.jl:889
   [21] (::VSCodeServer.var"#64#65")()
      @ VSCodeServer c:\Users\raven\.vscode\extensions\julialang.language-julia-1.83.2\scripts\packages\VSCodeServer\src\eval.jl:34
Test Summary:      | Error  Total  Time
Decompose in basis |     1      1  0.1s
ERROR: LoadError: Some tests did not pass: 0 passed, 0 failed, 1 errored, 0 broken.
in expression starting at f:\darsi\jl\SymbolicWedderburn.jl-master\examples\ex_C2_linear.jl:40

First, I do not understand why I get different errors. Second, whenever I kill the REPL and try to start from scratch then some variables are not defined.
Would you also tell me whether there is any difference where I define the directory of my new environment? Lastly, would you tell me why when I use a package sometimes I need to include some files from that package in my script and sometimes there is no need to include anything and everything seems connected inside that package? I do not know when a file from a package is automatically included in my script when I write using Name of that Package and when is not.

If you are manually downloading a package, you are probably not loading it correctly.

There are only two ways I would recommend loading a registered package.

using Pkg
Pkg.add("SymbolicWedderburn")

or

using Pkg
Pkg.dev("SymbolicWedderburn")

For the most part the use of include to load your own code seems a bit questionable as well.

Could you provide the output of

using Pkg
Pkg.status()

I did not manually download SymbolicWedderburn.jl. I indeed used `add SymbolicWedderburn’ in each of my environments. I needed to include some files from this package, so I additionally downloaded it manually.

If I run using Pkg Pkg.status() when I am in different environments, I get:

Status `F:\darsi\jl\SymbolicWedderburn.jl-master\examples\Project.toml`
  [36d08e8a] AbstractPermutations v0.3.1
  [7c1d4256] DynamicPolynomials v0.6.0
  [d5909c97] GroupsCore v0.5.0
  [4076af6c] JuMP v1.22.2
  [8bc5a954] PermutationGroups v0.6.3
  [c946c3f1] SCS v2.0.0
  [858aa9a9] SymbolicWedderburn v0.4.0
  [2f01184e] SparseArrays v1.10.0

and

Status `C:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\Project.toml`
⌃ [7c1d4256] DynamicPolynomials v0.5.3
  [d5909c97] GroupsCore v0.5.0
⌃ [4076af6c] JuMP v1.19.0
⌃ [8bc5a954] PermutationGroups v0.6.2
  [c946c3f1] SCS v2.0.0
  [858aa9a9] SymbolicWedderburn v0.4.0 `..`
  [2f01184e] SparseArrays v1.10.0
Info Packages marked with ⌃ have new versions available and may be upgradable.

Here’s how I would approach this.

julia> begin
    using Pkg
    Pkg.activate(; temp=true)
    Pkg.add("SymbolicWedderburn")
end
julia> using SymbolicWedderburn

julia> const examples_dir = joinpath(
           dirname(pathof(SymbolicWedderburn)),
           "..",
           "examples"
       )

# AP was not exported by SymbolicWedderburn.
# I suggest reporting this as a bug
julia> using SymbolicWedderburn: AP 

julia> Pkg.activate(examples_dir)

julia> include(joinpath(examples_dir, "run_examples.jl"))

This makes sure that I am using the examples for the same released version of SymbolicWedderburn.jl that I added.

If you do need the grab the latest commits, then I would do the following.

using Pkg
Pkg.activate(; temp=true)
Pkg.develop("StarAlgebras")
Pkg.develop("SymbolicWedderburn")
using SymbolicWedderburn
using StarsAlgebras
const examples_dir = joinpath(
           dirname(pathof(SymbolicWedderburn)),
           "..",
           "examples"
       )
Pkg.activate(examples_dir)
Pkg.develop("StarAlgebras") # update examples Manifest.toml
using SymbolicWedderburn: AP
include(joinpath(examples_dir, "run_examples.jl"))

You get different errors because the package that you added is different from the package that you downloaded. There are also just some issues that the authors need to address.

2 Likes

I also met a similar problem. I pull a repo from GitHub and try to run its example. There are “project.toml” and “manifest.toml” in the same directory and after I activate all the packages and check the status, the repl still complained something like: Random not defined. Why?

Since it is not sure this is a related error, could you maybe open a new topic, and provide more details? How and which version of that package did you install? Which example did you run? What was the concrete error message?

Without those details, all I could do is guesswork and that is probably more like a waste of time.

The general answer is that it’s either a bug in the package, in its documentation, or in your use of the package. Randomly including files from inside a package is never a correct solution.

3 Likes

Thanks! I’m new to the community and thank you for noticing me.

Thanks. There are still problems. If I do this for ‘AP’, then there will be another variable that causes an error. About your script, something strange also happens. It depends on when I run your script. If I kill the REPL, and then run your script, everything works. However, after killing the REPL, If I first run

using Pkg
Pkg.activate(@__DIR__)
Pkg.instantiate()

and then I run your script, I get an error as follows:

ERROR: LoadError: AssertionError: no_symmetry.solve_t / wedderburn_dec.solve_t > 1
Stacktrace:
 [1] top-level scope
   @ c:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\ex_robinson_form.jl:160
in expression starting at c:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\ex_robinson_form.jl:160
in expression starting at c:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\run_examples.jl:7

At this moment if I run your script for the second time, I do not get any error.

There is also another possibility. If I kill the REPL, then activate one of my environments in which the SymbolicWedderburn is added, and then run your script, I get the following error:

ERROR: LoadError: UndefVarError: `Monomial` not defined
Stacktrace:
 [1] top-level scope
   @ C:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\ex_C2_linear.jl:16
in expression starting at C:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\ex_C2_linear.jl:16
in expression starting at C:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\run_examples.jl:4

But this time, no matter how many times I run your script, I get the same error.

I would like to understand how Julia works; why sometimes, after getting an error, running the script works well, but some other times, running the script multiple times gives the same error.

In what context are you running that? @__DIR__ refers to the directory of the file. If you are running in the REPL, I think it means the current directory.

From what I can tell you are probably changing environments and loading packages in different environments. This is not expected to work.

The concept is pretty simple. The examples are only going to work with the package version they were released with.

The package that you downloaded is from the master branch and is under development. If you want to download something that corresponds to the package that you add, try this link to version 0.4.0.

See that the example from version v0.4.0 uses Monomial while the master branch version uses AbstractMonomial.

I’m the author of SymbolicWedderburn.jl :wink: and sorry for taking time of everybody here ;D, but the problem is very simple:

@Khashayar-Neshat You’re trying to run examples from master branch while using the released version of the package. This is bound to fail, given the number of changes in the current master.

as I’ve already stated, I only haphazardly update examples folder while developing the next release.

If you can’t make them work, it’d be better to open an issue in the repository directly.

I think I do not use the master branch anymore. I try to run your examples from the default directory. If I run ‘run_examples.jl’, I get

  Activating project at `c:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples`
Test Summary:      | Pass  Total  Time
Decompose in basis |   16     16  3.4s
Test Summary:                 | Pass  Total   Time
induced Matrix Representation |    5      5  12.7s
ERROR: LoadError: UndefVarError: `AP` not defined
Stacktrace:
 [1] top-level scope
   @ c:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\action_polynomials.jl:20
in expression starting at c:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\action_polynomials.jl:20
in expression starting at c:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\ex_S4.jl:6

This is the latest released version:

julia> Pkg.status("SymbolicWedderburn")
Status `C:\Users\raven\.julia\packages\SymbolicWedderburn\lbiby\examples\Project.toml`
  [858aa9a9] SymbolicWedderburn v0.4.0 `..`

Am I still doing something wrong? Does .. have a meaning? If I am doing wrong, So what should I do? I added symbolicwedderburn using REPL. I removed all the packages that I downloaded manually.

You need to add this line to the script. It is missing.

using SymbolicWedderburn: AP 
2 Likes

Aren’t we supposed to make different environments for different purposes so that for each purpose, we only load specific packages that do not conflict with each other?

Why should this make any difference? I activate one of my environments, then I write ]add the name of that package. If I do this for different environments, shouldn’t that package work the same in different environments?

Okay, I understand. If I activate one of my environments, and I write ]add SymbolicWedderburn, the released version is stored in the default directory C:\Users\raven.julia\packages. Now, when I go to the default directory and run one of the examples, I still get an error. Doesn’t this approach mean I am running examples that correspond to the package that I ‘add’? If is that so, why do I still get an error?

there was probably a missing definition, since the examples are only run locally:

but the required line is located a bit earlier in the file:

I’m trying now to make examples more self-contained, but honestly, I’d be better if You would do it ;D This way I’d save my time and you’d learn the innards of the package :smiley:

1 Like

Okay. Nowadays, I just work 25 hours a day with your package :D. I will make a new topic and report and ask all the problems there.

1 Like