UndefVarError Main.xxx issue

I have just bought a new computer, which is a MacBook Air with an M3 chip. Ever since installing Julia and adding the extension via Visual Studio Code I receive errors such as "UndefVarError: ‘…’ not defined in ‘Main.xxx’.

I have never had this issue on my last computer, which was also a Mac. Does anyone have an idea how to solve this?

Thanks in advance.

You’re not giving enough information. See Please read: make it easier to help you

In particular:

  • provide example Julia code that causes the errors
    • paste the code and format it using Markdown
  • provide the complete error message and stack trace
    • paste the output and format it using Markdown
2 Likes

Welcome to Jluia discourse! :wave:

Additionally to the already mentioned two points:

  • did you copy over a startup file, but did not yet install the packages?
  • do you run code from another machine, but did not “bring” (or copy) the environment?

That would be the two main ideas I have that might be reasons for the error, but I would need more details to provide more detailed answers.

2 Likes

Hi,

My last computer broke down so I could not bring anything over. So I have installed Julia again from scratch on my new computer.

Then you are probably missing packages, that you installed on the previous machine, see theusing commands in your script. You have to reinstall those as well.

Hi,

I have added the packages with using Pkg followed by Pkg.add(“package_name”). I can make the code run, but I get the error “UndefVarError: xxx is not defined in main” more often now compared on my last computer. Is that normal or is it only happening to me because I have done an installation error of Julia?

As mentioned above by @nsajko and @kellertuer, in order to actually help you you will need to provide more details. Please read the link given by @nsajko.

2 Likes

Hi,

An example would be this

include("CrewPairingDataStructsExample2024.jl") using .CrewPairingDataStructsExample2024

I will receive and error ERROR: UndefVarError: CrewPairingDataStructsExample2024not defined inMain`.

Keep in mind CrewPairingDataStructsExample2024.jl is in the same folder as the file I am trying to call this. It is a module.

I don’t think that example is complete enough to enable anyone to help you. You should try to provide a reproducer that’s complete, e.g. an entire file. But also try to minimize it, removing irrelevant details.

You probably meant to format multiple lines there between triple backticks, like this but without the brackets (I added those to stop the formatting)
[```]

include(“CrewPairingDataStructsExample2024.jl”)
using .CrewPairingDataStructsExample2024

[```]
The error you mentioned only hints that the module of that name failed to be instantiated. We’ll need to see what the file’s code and the full error stacktraces to know what’s going wrong there, it’ll help if you reduce it as much as possible without eliminating the errors so it’s easy to share and debug. Reducing would be removing definitions, function calls, and especially imports of other packages. If you reduce something, and running the file eliminates the error, then keep it and comment that it’s needed for that particular error.

1 Like