Is this a good workflow with Julia environment?

Will the following be a good workflow with Julia environment? Is there any bug in it? Or is there anything better to do?

The workflow:

  1. Create a folder named myProject for a certain project.
  2. Open folder myProject with VS Code.
  3. Start Julia REPL in VS Code.
  4. Under package management mode, type activate . to set up a Julia environment myProject.
  5. Add some necessary packages in the environment myProject.
  6. Create some .jl files in folder myProject to realize the function of the project.
1 Like

Looks fine to me! Ofc this is not a package. For faster development, you will benefit greatly to use, i.e. load, Revise.jl in the REPL. If you want something more complicated and streamlined have a look at DrWatson.jl.

5 Likes

Thank you for your affirmation! :handshake: :smiley:

For people stumbling on this in the future: we’re working on teaching this in a better way

https://gdalle.github.io/ModernJuliaWorkflows/

5 Likes

Very nice! Thanks! :handshake: :handshake:

Note that creating a package with a full structure is not much harder: Create new package · JuliaNotes.jl

3 Likes

Thanks! I’ll read that.

I would add that your functions should go in a different file than the scripts that call them. Why? Because it makes calling the functions more convenient, flexible, and efficient. If you put the function definitions in the same file as the script that calls them, then each time you include the script, the functions will have to be recompiled. But If, instead, you execute

using Revise

followed by

includet("my_functions.jl")

(Notice the “t” at the end of the includet function), where “my_functions.jl” contains all your function definitions, then any edits you make to the function definitions are instantly and automatically sensed by Julia so that you are ready to run (or re-run) any scripts that depend on them. Note that the includet function only need be run once per Julia session in that project.

2 Likes

But I found that in this way I could no longer click to choose the environment in the VS Code’s status bar successfully. It tells “You opened a Julia package that is not part of your current environment. Do you want to activate a different environment?”

Yes, I’m aware of this. Thanks!

But must I execute using Revise strictly after activating the project? I have added using Revise to ~/.julia/config/startup.jl to make sure Revise to be loaded on the Julia REPL startup. Maybe at that timepoint, the activated environment is still @v1.x.

Perfect! Using Revise any time prior to the includet is all that is necessary.

1 Like

Here has mentioned this issue. But I can’t switch environments by the “Julia env” button successfully. Don’t know why.

Maybe showing us your file structure will help.

From what I can tell, your proposed workflow should not require an environment change. When you open myProject folder in VSCode, it should automatically switch to the environment myProject (defined by the project.toml file there). Then all your .jl files inside the myProject folder can also work with that environment. Check that you don’t have an extra project.toml somewhere in the structure and that pwd() in the REPL returns the myProject folder.

If you instead want to call your myProject functions from .jl files that live outside of that folder, then you will want to make myProject a package. (I believe all that would require is making a file myProject/src/myProject.jl. I don’t think you need to deal with git and Github.) Then you would make a new environment where the external .jl files live, and use dev "path/to/myProject" in that external environment.

myProject.jl
module myProject
using PackageA, PackageB
export functionX, functionY
include("FileWithFunctionDefinitions.jl")
end
2 Likes

You can but you need to restart the VSCode REPL

1 Like

Yes. Thanks!

Thanks!

You might find my comment here, and the two comments below useful. It assumes that you have already created a project folder containing some main .jl file, which you have opened in VScode.
All of my projects start with these lines of code. Note that it does not matter if you start a project for the first time, or if you resume to it.

1 Like

It’s worth learning. Thanks!