wpkzz
wpkzz

Reputation: 899

Julia only imports names from first homemade Module

I have made two modules in Julia. One is for plotting graphs of trees structures, and one is for estimating entropy from simbolic secuences. The first one has this first lines:

  module ArbolGrafosTikz

using LightGraphs, TikzGraphs

abstract type LabelledDiGraph
end

export LabelledTree, Nodo, Subarbol, Arbol,
       walk_tree, walk_tree!, tikz_representation

struct LabelledTree <: LabelledDiGraph
    g::DiGraph
    labels::Vector{Any}
end

etcetera. The second one has this header:

module EntropySymb

using Base.Iterators

export simboliza, problock, ncartprod, issubvec, allprobsn


function simboliza(x, delta=epsi)
    # funcion que pasa floats a tres simbolos.
    result=0
    if x>delta

etcetera.

In my main program I have the first lines as thus:

push!(LOAD_PATH,".")
include("EntropySymb.jl")
include("ArbolGrafosTikz.jl")
using ArbolGrafosTikz
using EntropySymb

but I can only have direct access to the names of objects in ArbolGrafosTikz directly, for EntropySymb I have to use the name prefixed with the module, or Julia REPL doesn't seem to know about them. Why is this?

Upvotes: 0

Views: 62

Answers (2)

hckr
hckr

Reputation: 5583

The problem in your code is that you first include the files, which already loads the modules into Main so that Main.EntropySymb exists. This makes the subsequent import statement using EntropySymb return an error

ERROR: importing EntropySymb into Main conflicts with an existing identifier

If you already add the current directory (or the directory that contains the modules) to LOAD_PATH, you do not need the calls to include in order to import the module with using or import. You can simply write

push!(LOAD_PATH, ".")
using ArbolGrafosTikz
using EntropySymb

If you want to use include instead you should use using with Main.ModuleName or .ModuleName to bring the exported names into scope.

include("EntropySymb.jl")
include("ArbolGrafosTikz.jl")
using .ArbolGrafosTikz
using .EntropySymb

Note that you should also face the issue for the first module but I guess the reason why you do not is because the code is run in somewhat different order than the one given in the question or you did not actually run the first include statement. Maybe try the same code in a new session to reproduce the issue for the first module, as well.

Upvotes: 1

Oscar Smith
Oscar Smith

Reputation: 6378

You are running into the difference between using and import. using MyModule brings MyModule into scope, but import MyModel brings all exported names.

Upvotes: 0

Related Questions