How to run Julia project in VS Code?

Hey all,

Sorry for the simple question. I’m pretty new to Julia and I’m struggling with opening a project in VS Code. The project in question is:

I downloaded the project, installed Julia and its extension on VS Code, opened the master folder, tried to run master.jl via “Execute File in REPL” but I keep getting errors like:

UndefVarError: Operator not defined
 [1] top-level scope

Could someone help me please? Thank you

Hey! Sorry if I am misunderstanding your intentions.

QuantumOptics.jl is a so called package. It is meant to be installed via the package manager. In particular, a package defines its external dependencies and the package manager will resolve them for you.

I would highly recommend to read about Julia’s package manager here

Since the package is filed in Julia’s package registry, issuing
] add QuantumOptics from the REPL is enough to install it. Note that there is no need to manually clone or download the files from GitHub.

After installation has finished, using QuantumOptics will make the functionality available in the running Julia session. This will essentially load the package by executing the file src/QuantumOptics.jl – not master.jl like you tried. When trying to run master.jl first, a lot of code that should have been loaded first hasn’t, and hence the error you got.

If you are looking to use the functionality the package offers e.g as demonstrated in the docs Tutorial · QuantumOptics.jl, there is nothing more to do.

If you want to dig into the source and potentially make changes or extend it, I would recommend using the package manager to “develope” the package: ] dev QuantumOptics will clone from GitHub, resolve dependencies and make the source files available under ~/.julia/dev/QuantumOptics, although you are free to change that path.
So you could now open that folder in VSCode, start a Julia REPL, and load the package as above with using QuantumOptics while browsing around the source and changing things.

In all of this, it is usually wise to keep separate environments for different projects. It helps to reduce potential dependency clashes massively. Maybe you are familiar with virtual environments in Python. Julia has a similar mechanism. It is detailed in the Pkg manual linked above and a very recommended read.

1 Like

Thank you so much for the quick reply. I followed your steps and got through the installation. After installation I hit delete (on Mac) to go back to julia> and executed using QuantumOptics but when I type in src/QuantumOptics.jl afterwards, I get the following error:

ERROR: UndefVarError: jl not defined
 [1] getproperty(x::Module, f::Symbol)
   @ Base ./Base.jl:35
 [2] top-level scope
   @ REPL[11]:1

Thank you for the reply once again.

Why are you doing src/QuantumOptics.jl in the first place? After using ... the code is loaded, the file has been executed. You can use the package now as it is described in the manual.

You may want to open the file in e.g. VSCode to inspect what it is doing.

Can you explain what it is you are trying to achieve?

I’m trying to open some of their examples to test them out myself. I ended up installing a dozen packages (as they were suggested in the errors), so after all that, trying to run QuantumOptics.jl I get the following error:

WARNING: replacing module QuantumOptics.
WARNING: replacing module QuantumOptics.

this comes about because the module was already defined after using QuantumOptics.

But you want something else. The example are not defined anywhere in the package source. The package defines the functions used in the examples, e.g. Pumped cavity · QuantumOptics.jl

You should copy-paste from there, or look at GitHub - qojulia/QuantumOptics.jl-examples: Examples for QuantumOptics.jl Clone that repo and run those notebooks. For that you will need to install the Julia kernel for Jupyter called IJulia. Ignore the statements in the README about converting the notebooks to markdown. You can use them interactively.