First steps: Start Julia in a certain folder in VSCode

Hi (sorry, first day here),

I open a folder and get in the terminal just: PS D\andSoOn_theFolderPath >
To get to Julia (if one can say it like that) I have to run a lin of code of a program in my folder, what am I doing wrong?

If I set my Julia environment in the left corner of VSCode to anything else than my current folder I get the error (That occurs in many other questions as well): Julia package not part of your current environment. Is that bad? It seems to run that way, kind of.

What is the structure of your code tree?

Some past posts that might help:

1 Like

Hi, welcome!

Have you checked the manual to use Julia in VS Code?
https://www.julia-vscode.org/docs/stable/

If I got it right, you are stuck in the very first action: how to open a Julia REPL to run commands. You have the specific instructions for that in this page:
https://www.julia-vscode.org/docs/stable/userguide/runningcode/

Shortcut: Alt+J, Alt+O normally starts a Julia REPL, unless you have a particular configuration that changes such keybinding.

Hi,

my Path is for example this folder saved somewhere

From the question:

All I wanted is to run a Julia file, thats it. The example I tried is the link (PowerModels, but any would work for me, I just need an example), it does have a Project and Manifest file. I am in the right path, I guess, and I did activate and instantiated (what I think is the current folder with those files).

So to back up to the absolut beginning in VS Code: “File” → “Open Folder” and my Terminal Commant line shows PS D:\Daten\Julia\PowerModels.jl-master>
(… the cd(“D:\Daten\Julia\PowerModels.jl-master”) command then does nothing, right? But I should be in the working directory) I can change the environment in the left corner of VSCode and chose “C:\users\myName.julia\environments\v1.7”, but that doesnt help … and typing in “using Pkg” doesnt work in any case.
Runing one line of Code by Ctrl+enter in a file in that folder starts Julia and I can type in using Pkg and then activate and so on…

That cant be the way, right?
But that is also another problem due to inexperience, I hope.

I just want to run the Code, shown in the read me:

Basic Usage

Once PowerModels is installed, Ipopt is installed, and a network data file (e.g. “nesta_case3_lmbd.m”) has been acquired, an AC Optimal Power Flow can be executed with,

using PowerModels
using Ipopt
run_ac_opf(“nesta_case3_lmbd.m”, IpoptSolver())

Hello,

I have, but I will dig deeper, my specific Problem is posted as an answer to PetrKrysIUCSD here in this discussion, maybe thet helps to clarify. But I will dig deeper.
The way I try it seems just too hard for just running code.

Hi,

thank you, I will look into it.

Is there any reason to be using a six year old package that probably doesn’t work on recent Julia versions anways? I’d try GitHub - lanl-ansi/PowerModels.jl: A Julia/JuMP Package for Power Network Optimization instead if I were you.

That is the PowerShell terminal, nothing to do with Julia. What you are looking for is the REPL (which shows the julia> prompt), and you have to start it manually. That can be done in (at least) three ways:
a. Run the command “Julia: Start REPL” (from the command palette: Ctrl+Shift+P).
b. Default keybinding: Alt+J, Alt+O.
c. Run some code in the editor (the way you actually did it).

1 Like

Well, I open a normal terminal in vscode and start julia from that terminal… Faster and easier for me than using the Julia REPL from vscode…
And to run an example I just use the command

include "src/my_code.jl" 

or whatever…

Yes, that works, but other components of the VS Code Julia extension (inline execution, variable explorer, plot panel, automatic recognition of sysimage…) won’t be linked to what you do in that terminal. That’s ok if you like it like that, but may be confusing for new users who have read about those features.

1 Like

Hello,

there is not. Nice that you looked that up, but it was just an example and does therefore not tackle the main problem.
The new Version does look very interesting though, thx.

How about giving GitHub - PetrKryslUCSD/VSCode_Julia_portable: Portable Julia running in VSCode a shot?

Well, I do have a set file folder, structure
Folder(Project,Manifest,src(file1,file2),examples(example1,example2)).
I cannot just klick in examples, in example1 and go to “Run”.

include(“examples/example1.jl”) does not work, e.g. this and a 100 other Errors occur:
ERROR: LoadError: UndefVarError: VariablesContainer not defined

I am kind of confused.

Does it work if you just use the REPL without VSCode?
Can you share your file example1.jl?

Thank you a lot, I will try it out, hopefully it helps.

In WARNING: using x.x in module Main conflicts with an existing identifier my problem occurs. Basically, the problem seems to be, that I cnt really operate in Julia and I try to fix that. I just want any Code from Github with everythin included, e.g. Manifest, Projekt, a src-folder and maybe some other folders with example programs. I then want to run that without havin problems and I dont get there. Maybe its because the Code is wrong, but I dont think that. [PetrKryslUCSD] just send me an example and I will try that first.
Sorry for asking so basic things!

Well, first of all do NOT checkout PowerModuls.jl or any other package you want to use from git. (Unless you want to modify it). Do NOT use the Project.toml file from any package, create your own instead.
Example:

mkdir test
cd test
julia --project="."
using Pkg
pkg"add PowerModels"
pkg"add Ipopt"

Now you have a project folder with the package PowerModels and Ipopt installed (and with no other package, so no conflicts).

Now quit Julia (ctrl+d) and create a folder with your examples:

mkdir examples
cd examples
gedit first_example.jl

(Or use your favorite editor instead of gedit).
Paste the following lines into the editor:

using PowerModels
using Ipopt

solve_ac_opf("matpower/case3.m", Ipopt.Optimizer)

Save the file.
Go back to the project directory and have a look what is there:

cd ..
tree

The output should look like:

ufechner@tuxedi:~/repos/tmp$ tree
.
├── examples
│   └── first_example.jl
├── Manifest.toml
└── Project.toml

Now start Julia and run the example:

julia --project
include("examples/first_example.jl")

I suggest that beginners should learn to use the REPL first, when they know how to use it they can switch to vscode… vscode is more difficult to explain and has a lot of settings that you can set wrongly.

Another problem is that this example from the quickstart guide of the package PowerModels does not work. I created an issue in the package: Example does not work · Issue #826 · lanl-ansi/PowerModels.jl · GitHub

To fix the example, create a folder matpower, put the following file into it: https://raw.githubusercontent.com/power-grid-lib/pglib-opf/master/pglib_opf_case5_pjm.m and rename it to case3.m

The directory structure now looks like this:

ufechner@tuxedi:~/repos/tmp$ tree
.
├── examples
│   └── first_example.jl
├── Manifest.toml
├── matpower
│   └── case3.m
└── Project.toml

And Project.toml looks like this:

ufechner@tuxedi:~/repos/tmp$ cat Project.toml 
[deps]
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
PowerModels = "c36e90e8-916a-50a6-bd94-075b64ef4655"

Now I could run the example and get the output:

EXIT: Optimal Solution Found.
Dict{String, Any} with 8 entries:
  "solve_time"         => 3.35954
  "optimizer"          => "Ipopt"
  "termination_status" => LOCALLY_SOLVED
  "dual_status"        => FEASIBLE_POINT
  "primal_status"      => FEASIBLE_POINT
  "objective"          => 17551.9
  "solution"           => Dict{String, Any}("baseMVA"=>100.0, "branch"=>Dict{St…
  "objective_lb"       => -Inf

Another source of confusion might be that you were linking to an old version of PowerModels from a mirror. Do NOT use outdated versions, always use the original.