# Working directory

**URL:** https://discourse.julialang.org/t/working-directory/127797
**Category:** New to Julia
**Created:** [April 7, 2025, 1:41pm UTC](https://discourse.julialang.org/t/working-directory/127797 "2025-04-07T13:41:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Magnus\_Lilledahl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magnus_lilledahl/32/51079_2.png) [@Magnus\_Lilledahl](https://discourse.julialang.org/u/Magnus_Lilledahl)
#### Post date: [April 7, 2025, 1:41pm UTC](https://discourse.julialang.org/t/working-directory/127797/1 "2025-04-07T13:41:26Z")

</div>

I have a package that provides some functions that needs data stored in files on the users computer

My plan was to require that these files are stored in the same folder as the file that imports (`using package`) the package so I could define a global variable in the module

`DATADIR = pwd()`

and then use this variable anywhere in the package I need to access the data.

To test this I have a script located in `mypath/test/test/run.jl` (`mypath` is just the rest of the path). This script imports my package (`using package`) and tries to call some functions, but I get the error that there is no such file as: `mypath/test/datafile.txt`

The function in question tries to open the file using

```julia
filepath = joinpath(DATADIR, datafile)
data = open(readdlm, filepath)

```

The error is correct sin the data file is located in `mypath/test/test/datafile.txt`

I get the following in the REPL after the error occurs

```julia
>DATADIR
"mypath\test"
>pwd()
"mypath\test\test

```

It seems like one folder level is truncated from pwd() when storing in DATADIR. Hmm…?

I guess I have two questions:

1. Why is this not behaving as expected (where did the last part of the path go (\test)?)
2. Is there a better way to reference the file location where the package was imported (or a better way of defining where data files should be located and accessed for that matter)?

(The actual package is here [Magnus Borstad Lilledahl / AggregatorX · GitLab](https://gitlab.sintef.no/magnus.lilledahl/aggregatorx)

---

<div class="post-metadata">

### Author: ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)
#### Post date: [April 7, 2025, 3:07pm UTC](https://discourse.julialang.org/t/working-directory/127797/2 "2025-04-07T15:07:11Z")

</div>

If I understand your setup correctly\*, then the issue will be that the current working directory `pwd` is simply a different concept from the directory of the current file. To get the latter, you can use `@ __DIR__ `.

E.g. if I put a script

```julia
println("pwd: ", pwd())
println("@ __DIR__ : ", @ __DIR__ )

```

in `E:\temp\script.jl`, and run it from `E:\`

```julia-repl
E:\>julia ./temp/script.jl
pwd: E:\
@ __DIR__ : E:\temp

```

you can see that `pwd()` gives the terminal directory I launched Julia from, while `@ __DIR__ ` gives the directory of `script.jl`.

\*Edit: I presumably do not with regards to your first question 🙂 , considering a declaration `DATADIR = pwd()`, should really mean that `DATADIR == pwd()`, regardless of whether this is the directory you really intend. You’re not `cd`’ing at some point by any chance?

---

<div class="post-metadata">

### Author: ![Magnus\_Lilledahl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magnus_lilledahl/32/51079_2.png) [@Magnus\_Lilledahl](https://discourse.julialang.org/u/Magnus_Lilledahl)
#### Post date: [April 7, 2025, 6:42pm UTC](https://discourse.julialang.org/t/working-directory/127797/3 "2025-04-07T18:42:57Z")

</div>

Thanks for some added clarity @eldee.

I later tried to uninstall the package from the Pkg environment (in `mypath/test/test`). I then tried to reinstall it, but this time making sure that the current working directory while Pkg.add was `mypath/test/test`. After doing this, no errors 😄

My theory is that the code in the module is evaluated while being added or precompiled and thus the value of DATADIR variable is determined at this point and _not_ when `using` the package at a later point.

My guess is that last time I added the package the current working directory was `mypath\test` and I probably did `Pkg> activate .\test` (instead of `Pkg>activate .` which I did now).

Does this seem reasonable?

So the conclusion is that using `pwd()` is completely unsafe because there is no control of what the working diretory was when the user added the package.

But `@ __DIR__ ` seems to give the same problem, it will only refer to the folder where the package is installed which also can be anywhere and is typically the Julia package depot.

Is it possible to determine, from within the package, the location of the script that imports (`import/using`) the package?

If not it seems like the only way is to prompt/require the user to provide the file location of the datafiles in user’s script, e.g. export a function `set_datadir()`:

```julia
function set_datadir(path:String)
  DATADIR = path
end

```

Or are there better ways?

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [April 7, 2025, 7:22pm UTC](https://discourse.julialang.org/t/working-directory/127797/4 "2025-04-07T19:22:40Z")

</div>

> [@Magnus\_Lilledahl](#):
>
> My theory is that the code in the module is evaluated while being added or precompiled and thus the value of DATADIR variable is determined at this point and _not_ when `using` the package at a later point.

That’s how precompile caching of top-level code works, yes. If you want runtime values, then you need to define global variables in ` __init__ `, and it only runs automatically on the first _load_, not further `using`/`import` statements. [Another example with top level `rand(1:100)`](https://www.oxinabox.net/2023/06/16/top-level-code-in-julia.html).

> [@Magnus\_Lilledahl](#):
>
> Is it possible to determine, from within the package, the location of the script that imports (`import/using`) the package?

I don’t think an import can provide any information backwards to the module, even if it’s just the first time via ` __init__ `. If it were possible and _multiple_ imports occur from different modules and files, then you can’t be sure what the state of your package ends up being. You can sort of get the same effect with your `set_datadir` idea. Say I run 2 scripts that `set_datadir(@ __DIR__ )` before doing some work, that should be fine. Then I decide to rerun a specific function call from the 1st script to double check or demonstrate something, but I forget `set_datadir` directed the package to the 2nd script. If I’m unlucky, the mistake is unnoticed and causes problems later. Global state is called evil for good reason, independent inputs are far preferable.

---

<div class="post-metadata">

### Author: ![Magnus\_Lilledahl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magnus_lilledahl/32/51079_2.png) [@Magnus\_Lilledahl](https://discourse.julialang.org/u/Magnus_Lilledahl)
#### Post date: [April 7, 2025, 7:53pm UTC](https://discourse.julialang.org/t/working-directory/127797/5 "2025-04-07T19:53:03Z")

</div>

Thank’s for the insightful input @Benny. I think this whole problem was a good exercise to make me realise that I must excorcise the evil global state variables 👹 from my code 😄
