Can I change the working directory in a file more than once?

Hey folks, I was having a little trouble getting some julia file system functions to work. The code seems correct, so I can’t tell if I made a code error, or if it is an issue with the vscode plugin, etc?

I have a julia .jl file, and I set the working directory to the directory of the current file. Then I changed the working directory to a different directory that contains some “include” files. For some reason the include statement is still referring to the original working directory instead of the changed working directory.

Here is some sample code. Note that the relative directory work works and is good–no problems with the relative path.


using Zygote
using Plots

cd(joinpath(@__DIR__, "../..", "opt_problems/"))


The error I get is ERROR: SystemError: opening file "/.../julia/julia_opt/algfo_polished/3_bracketing/unconstrained_problems.jl": No such file or directory. `

So the error is referencing the original working directory instead of the opt_problems directory.

Is there some restriction on changing directories in a file, or am I only allowed to change the directory once?

include is relative to the directory of the current file, not relative to the current working directory.

So you can do:


with no cd and no @__DIR__ at all.


Ahh, so that makes sense. Thanks @stevengj that is very helpful. I just made the wrong assumption.