Importing package into Main conflicts with an existing identifier

Hi all,

When trying the code below I get the error “importing HV04 into Main conflicts with an existing identifier”. Is there a way to resolve this? I’m quite new to Julia so could be making rookie errors.

When I restart julia and try using Pkg, using HV04, HV04.greet() is says “greet is not defined”.

Any help would be much appreciated.

module HV04

# Write your package code here.
export greet
greet() = print("Hello World!")

end
import HV04
HV04.greet()

Hi @Aje12, maybe you are loading an older (or different) version of the HV04 module. You can check which package version you are loading using import Pkg; Pkg.status(). Also you can check the path of the loaded module using import HV04; pathof(HV04). You can provide us this information for further help.

You can check at the documentation for how to develop packages.

1 Like

@josePereiro

Hi thanks for your reply. I’ve tried what you suggested and this is what I get

Status `C:\Users\aaron\.julia\environments\v1.5\Project.toml`
  [c52e3926] Atom v0.12.24
  [6e4b80f9] BenchmarkTools v0.5.0
  [336ed68f] CSV v0.7.7
  [aaaa29a8] Clustering v0.14.1
  [a93c6f00] DataFrames v0.21.7
  [38e38edf] GLM v1.3.10
  [28b8d3ca] GR v0.52.0
  [a2cc645c] GraphPlot v0.4.3
  [6049f220] HV04 v0.1.0 `C:\Users\aaron\~\code\HV04`
  [7073ff75] IJulia v1.21.4
  [b9914132] JSONTables v1.0.0
  [e5e0dc1b] Juno v0.8.4
  [093fc24a] LightGraphs v1.3.0
  [2f5eb75a] LightGraphsFlows v0.4.1
  [6f286f6a] MultivariateStats v0.7.0
  [14b8a8f1] PkgTemplates v0.7.13
  [91a5bcdd] Plots v1.6.9
  [d330b81b] PyPlot v2.9.0
  [ce6b1742] RDatasets v0.6.10
  [fc66bc1b] SNAPDatasets v0.1.0
  [bd369af6] Tables v1.1.0
  [b8865327] UnicodePlots v1.3.0
  [37e2e46d] LinearAlgebra
  [9a3f8284] Random
  [9e88b42a] Serialization
  [10745b16] Statistics

I tried pathof but nothing came up


julia> import HV04
ERROR: importing HV04 into Main conflicts with an existing identifier

julia> pathof(HV04)

I created the package HV04 in a different directory, would that be the problem? Its in “C:\Users\aaron~\code\HV04” and im working in “C:\Users\aaron\hv04”

Umm, Try cd to C:\Users\aaron\~\code\HV04 and run julia --startup-file=no --project -e "import HV04; println(pathof(HV04))".

What it does:
1- --startup-file=no won’t load ~/.julia/config/startup.jl (this is the default path, it may be different) file so anything you are defining there will not interfere.
2- --project will tell julia to use the package environment, not the global.
3- -e "import HV04; println(pathof(HV04))" will execute this lines.

Do I run that in the Julia terminal or a git bash one? I’ve tried both and it didn’t work, I’m definitely doing it wrong though.

Umm, you are using Windows! You probably should find a way to include julia in your path, but that is a different issue, see here.

Anyway, a version you can run from a julia repl:

cmd = `$(Base.julia_cmd()) --startup-file=no --project='C:\Users\aaron\~\code\HV04' -e "import HV04; println(pathof(HV04))"`
run(cmd)

It is calling a julia process from a julia process with the same parameters than before.

Thanks for all the help so far :slight_smile: I ran that command and this came up.

C:\Users\aaron~\code\HV04\src\HV04.jl Process('C:\Users\aaron\AppData\Local\Programs\Julia 1.5.2\bin\julia.exe' -Cnative '-JC:\Users\aaron\AppData\Local\Programs\Julia 1.5.2\lib\julia\sys.dll' -g1 --startup-file=no '--project=C:\Users\aaron\~\code\HV04' -e 'import HV04; println(pathof(HV04))', ProcessExited(0))

Ok, ProcessExited(0) means that all was ok and julia was able to load your package. Also as you can see the path C:\Users\aaron~\code\HV04\src\HV04.jl is consistent with the one given by Pkg.status() for this package C:\Users\aaron\~\code\HV04, so the environment is ok too.
That suggest that the problem could be that you (or something else) are defining other HV04 in the startup.jl file. For testing it just run the previous command without the --startup-file=no flag, or check your startup.jl file.

1 Like

I ran the command without the startup flag and got:

julia> run(cmd)
C:\Users\aaron\~\code\HV04\src\HV04.jl
Process(`'C:\Users\aaron\AppData\Local\Programs\Julia 1.5.2\bin\julia.exe' -Cnative '-JC:\Users\aaron\AppData\Local\Programs\Julia 1.5.2\lib\julia\sys.dll' --depwarn=yes -g1 --color=yes '--project=C:\Users\aaron\~\code\HV04' -e 'import HV04; println(pathof(HV04))'`, ProcessExited(0))

Well, another thing you can try is @which HV04 in a fresh julia session (without importing HV04). That will give you info about the already defined HV04

1 Like

Yeah, I got Maxima working in the REPL a week ago, only thing is it won’t load in Julia Markdown.

Got ‘Main’ when running this. Thank you again for all the help on this issue!

Umm, somehow something is defining HV04 in Main, it can only (I think (In a fresh session)) occur in your startup.jl file.

Another tests

Test1

cmd = `$(Base.julia_cmd()) --startup-file=no -e "println(@isdefined HV04)"`
run(cmd);

It should print false, which means that julia std library do not define any HV04.

Test2

cmd = `$(Base.julia_cmd()) --startup-file=yes -e "println(@isdefined HV04)"`
run(cmd);

Now, if the problem is that you somehow declare HV04 in the startup.jl file it must return true. But, if it returns false then something else is happening and I will need more info for trying to solve it

1 Like

Both tests returned false

julia> run(cmd)
false
Process(`'C:\Users\aaron\AppData\Local\Programs\Julia 1.5.2\bin\julia.exe' -Cnative '-JC:\Users\aaron\AppData\Local\Programs\Julia 1.5.2\lib\julia\sys.dll' --depwarn=yes -g1 --color=yes --startup-file=no -e 'println(@isdefined HV04)'`, ProcessExited(0))
julia> run(cmd)
false
Process(`'C:\Users\aaron\AppData\Local\Programs\Julia 1.5.2\bin\julia.exe' -Cnative '-JC:\Users\aaron\AppData\Local\Programs\Julia 1.5.2\lib\julia\sys.dll' --depwarn=yes -g1 --color=yes --startup-file=yes -e 'println(@isdefined HV04)'`, ProcessExited(0))

What extra info do you need?

Well I’m running out of ideas :wink: Its seams that if you start a fresh julia process (using run(`$(julia_cmd())...`)) you are able to import the package with no problem independent of the startup.jl. But somehow when you use julia the ‘other way’ in a fresh session you have already defined HV04 in Main so it conflict with an import call. So, could you describe, as precise as possible, how do you run julia in the cases that the import error occur?

Sure thing! When I open atom and start a new Julia session and do import HV04 followed by HV04.greet() I get the error “greet is not defined”.

If I run HV04.jl then type using HV04 i get the error “ERROR: importing HV04 into Main conflicts with an existing identifier”. Same thing if I import HV04.

However after running HV04.jl and typing HV04.greet() works.

What do you mean by that?

Its the main file for my project. Ill paste it below :slight_smile:

module HV04

export my_PageRank
export g_bfs, g_dijkstra, g_bellman, g_page_rank
export PageRank

include("GraphAlgorithms.jl")
include("PageRank.jl")
end

So if I hit run for this file in Atom and type ‘using HV04’ into the console I get the conflicts error.

Well in this case I think is understandable, I never used Atom but I believe when you run a file it just include it in the current session, so you are defining a new module in Main called HV04. using MyPkg means that you want to load a package from the environment and include it in the Main module. In your case the package exist in the environment but you can’t include it in Main due to the name conflict.

For instance, this is another scenario:

module A
    export funA
    funA() = println("Hi from A")
end
using A
# ERROR: ArgumentError: Package A not found in current path:
# - Run `import Pkg; Pkg.add("A")` to install the A package.
funA()

In this case the using statement fails because no module/package A was listed in the active environment. But I can do using .A (note the dot, which mean current Module) to tell the load system you want the Main version not the version listed in any environment. In this case all works and funA is exported to Main.

module A
    export funA
    funA() = println("Hi from A")
end
using .A
funA()
# Hi from A

Now that I see your code, I don’t see the definition of a function greet, so the call error could be also expected.

3 Likes

Ah that makes sense and works! Thank you very much for all your help :slight_smile: