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.

3 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.