How to correctly set up VS working directory on Windows for project with multiple subfolders?

I am on Windows, trying to move from running Julia in Jupyter notebooks to directly running my code from VisualStudio.

I am working on a project that uses multiple modules (XXX.jl files) that live in one subfolder, but which draw on data files (YYY.csv) that live in another subfolder.

That is, my folder structure is: ..\project\modules and ..\project\data.

So far I have managed to set my general Julia working directory in VisualStudio to .…\project.

In my code (XXX.jl), I can load modules by including them via “…/modules/XXX.jl”.

However, when a module tries to access one of the data files in ..\project\data, I get an error: “ERROR: LoadError: ArgumentError: “…/data/YYY.csv” is not a valid file or doesn’t exist”

When I run the same code in Jupyter notebook with the same project working directory set everything runs smoothly, which makes me think this is an issue with my VS configuration.

Many thanks for helping, and apologies if this is not phrased correctly - it’s my first post.

Maybe you could print the output of pwd() to see the current working directory.
Your Julia session of VS code probably has the root directory of your project as working directory:

  • When you include modules it is with respect to the file location.
  • When you load files it will be with respect to working directory.

In Jupyter notebooks, it will spawn a new Julia session for you which then has the file location as the working directory.


EDIT: I would do what @pfitzseb wrote :smiley:

1 Like

Generally I’d recommend specifying paths relative to the current dir with e.g. joinpath(@__DIR__, "..", "project", "modules) instead of modifying cwd.

5 Likes

The last answer is a good one. There are a number of similarly helpful macros:
https://docs.julialang.org/en/v1/base/base/#Base.@__DIR__

Thank you all! The solution was simply to make the sub-directory in which XXX.jl lives the working directory.

This still is done via cwd - so room for me to experiment with @pfitzseb’s solution.

Thanks for the earlier answers. Unfortunately, the problem still comes back from time to time - why or under which conditions I don’t know.

I have to admit what @pfitzseb wrote above is beyond my current level of comprehension, similar to @jd-foster’s pointer to macros.

What @SteffenPL diagnoses above seems to hint at a problem in how Julia sessions within VisualStudio are set up - would anyone have a tutorial in very basic language on how to set these up properly in VisualStudio?

For reference:

(i) when I run ‘pwd()’ in a Julia session in VS, I correctly get …\project\ as the working directory.

(ii) the way I call the file in question is already by specifying paths relative to that working directory: ‘countries = CSV.read(“…/data/pattern-scaling.csv”, DataFrame).Country’

Many thanks

One additional piece of information I just discovered related to point (ii) above:

If I remove the “…/” bit before the relative path (“…/data/pattern-scaling.csv”), Julia in VS finds the file and everything runs - would anyone know what might cause this?

The current code with “…/” runs correctly on my collaborators machine (which I think is either Linux or MacOS).

For posteriority I record the solution, which turned out to be simple:

Note that my path structure was:
-Main folder
–Subfolder 1
–Subfolder 2

The Julia code lives in Subfolder 1, while the data the Julia code calls lives in Subfolder 2.

Through “…/”, the relative path goes up to the main folder first. By making Subfolder 2 the current working directory, all relative paths work as intendend.