logo

How to Determine the Location of Code Files Executed in Julia 📂Julia

How to Determine the Location of Code Files Executed in Julia

Guide

If you are someone who uses Julia, it’s likely that you’re comfortable with using multiple operating systems or computers, including servers. If there is file input/output involved, having to adjust the path each time the development environment changes can be quite bothersome. This is where the @__DIR__ macro comes in handy. Suppose you have a Julia code file like the following.

20201221_150546.png

Typically, when executed from the terminal, pwd() and @__DIR__ may appear to not be differentiated as seen below.

20201221_161047.png

The difference between these two arises when using an IDE (Integrated Development Environment) such as Atom. Unlike pwd() which simply returns the current working directory, @__DIR__ informs you of the actual location of the code file. This can be extremely useful for complex and repetitive tasks where the working directory may be changed frequently, but the location of the executing code file is not as likely to change.

20201221_160331.png