Typically when opening and running a file in an IDE, the current path would be the residing path of the file, i.e. the location that file resides. For some reason, experience tells this is not the case in JUNO for Julia language (Edit: in Windows).
The consequence is that one receives a several errors due to unfound files unless they set the path manually.
How can one fix this?
Why is that?
Julia and Juno both work the same way in terms of the working directory. It's set when you start Julia and stays the same until you call cd
– it's not determined by the running file at all.
How does it get chosen, then? The julia
command inherits it from the terminal, so running:
julia script.jl
will work in script.jl
's folder whereas
julia dir/script.jl
will work in the folder above that one. It's just the folder that you launched Julia from, and again the location of the script itself makes no difference.
When Juno boots Julia it has to choose what folder to launch it in, and it uses your "project folder" – that is, the folder that you currently have open. Simple enough, but might cause issues if you're relying on pwd()
and your script is in a subdirectory of your project folder.
The simple solution to this is to avoid relying on pwd()
at all and to use @__FILE__
instead, which will always refer to the currently executing file. For example,
readcsv(joinpath(dirname(@__FILE__), "data.csv"))
This is the most idiomatic way to load local files in Julia, but if you're really set on using pwd()
then you can also easily change the working directory using the set of commands starting with Julia Client: Work in ...
.