# The macrocall @\_\_FILE\_\_ does not return the directory of the file, but rather the REPL output number

**URL:** <https://discourse.julialang.org/t/the-macrocall-file-does-not-return-the-directory-of-the-file-but-rather-the-repl-output-number/55585>\
**Category:** New to Julia\
**Created:** [February 18, 2021, 10:50pm UTC](https://discourse.julialang.org/t/the-macrocall-file-does-not-return-the-directory-of-the-file-but-rather-the-repl-output-number/55585 "2021-02-18T22:50:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bionicinnovations1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bionicinnovations1/32/22874_2.png) [@bionicinnovations1](https://discourse.julialang.org/u/bionicinnovations1)\
**Post date:** [February 18, 2021, 10:50pm UTC](https://discourse.julialang.org/t/the-macrocall-file-does-not-return-the-directory-of-the-file-but-rather-the-repl-output-number/55585/1 "2021-02-18T22:50:24Z")

</div>

I have been using to following to change my working directory to the location of my .jl scripts in Atom as follows without problems:

```julia
cd(dirname(@ __FILE__ ))

```

When I run a script with this in vs code, I get an error and it doesn’t change my working directory:

```julia
julia> cd(dirname(@ __FILE__ ))
ERROR: IOError: chdir : invalid argument (EINVAL)
Stacktrace:
 [1] uv_error at .\libuv.jl:97 [inlined]
 [2] cd(::String) at .\file.jl:84
 [3] top-level scope at REPL[2]:1

```

The reason that is I think is because there is nothing in the strong for cd() to work on:

```julia
julia> @ __FILE__"REPL[1]"

julia> dirname(@ __FILE__ )
""

```

When I use Alt+Enter to evaluate it inline from within a script, then it works fine. But when I run the actual script I get the error message above.

Any ideas on what is going on?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [February 20, 2021, 10:08pm UTC](https://discourse.julialang.org/t/the-macrocall-file-does-not-return-the-directory-of-the-file-but-rather-the-repl-output-number/55585/2 "2021-02-20T22:08:56Z")

</div>

How are you running the script? It works fine for me:

test.jl:

```julia
dir = dirname(@ __FILE__ )

println("Directory: $dir")

```

Produces:

```julia
$ julia test.jl 
Directory: /tmp

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 20, 2021, 10:16pm UTC](https://discourse.julialang.org/t/the-macrocall-file-does-not-return-the-directory-of-the-file-but-rather-the-repl-output-number/55585/3 "2021-02-20T22:16:14Z")

</div>

this is because you’re in `REPL` and REPL is not a file anywhere

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [February 20, 2021, 11:06pm UTC](https://discourse.julialang.org/t/the-macrocall-file-does-not-return-the-directory-of-the-file-but-rather-the-repl-output-number/55585/4 "2021-02-20T23:06:11Z")

</div>

You probably want `pwd()` instead of `dirname(@ __FILE__ )`. That will give you the directory you are `cd`ed into. As @jling explained, `@ __FILE__ ` will always give you the path of the script the macro was expanded in, so it doesn’t give anything useful in the REPL.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 21, 2021, 9:36am UTC](https://discourse.julialang.org/t/the-macrocall-file-does-not-return-the-directory-of-the-file-but-rather-the-repl-output-number/55585/5 "2021-02-21T09:36:46Z")

</div>

Use `@ __DIR__ `. When running from a file it’s the same as `dirname(@ __FILE__ )` and when running from the REPL it’s the same as `pwd()`.

---

<div class="post-metadata">

**Author:** ![bionicinnovations1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bionicinnovations1/32/22874_2.png) [@bionicinnovations1](https://discourse.julialang.org/u/bionicinnovations1)\
**Post date:** [February 25, 2021, 12:25am UTC](https://discourse.julialang.org/t/the-macrocall-file-does-not-return-the-directory-of-the-file-but-rather-the-repl-output-number/55585/6 "2021-02-25T00:25:34Z")

</div>

Yes, it appears that is the problem. However the behaviour of Atom and VS Code are different:

Ctrl + Enter when highlighting `cd(dirname(@ __FILE__ ))` in Atom does not through up the error, but does in VS Code.

I discovered that if I use Alt + Enter in VS Code when highlighting `cd(dirname(@ __FILE__ ))` it works fine.

---

<div class="post-metadata">

**Author:** ![Nelson\_Chow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nelson_chow/32/27288_2.png) [@Nelson\_Chow](https://discourse.julialang.org/u/Nelson_Chow)\
**Post date:** [July 19, 2021, 5:27am UTC](https://discourse.julialang.org/t/the-macrocall-file-does-not-return-the-directory-of-the-file-but-rather-the-repl-output-number/55585/7 "2021-07-19T05:27:00Z")

</div>

Hi Bionicinnovations1,

Did you find a reliable way to get to the sourcecode path yet?

I was looking at doing the same thing, but all the suggestions such as PWD, **files** , **dir** \_ all yield incorrect results in Windows via VSCode.

Cheers,  
Nelson

---

<div class="post-metadata">

**Author:** ![bionicinnovations1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bionicinnovations1/32/22874_2.png) [@bionicinnovations1](https://discourse.julialang.org/u/bionicinnovations1)\
**Post date:** [October 10, 2021, 11:21pm UTC](https://discourse.julialang.org/t/the-macrocall-file-does-not-return-the-directory-of-the-file-but-rather-the-repl-output-number/55585/8 "2021-10-10T23:21:22Z")

</div>

I didn’t find a better solution. So far the only way I can run scripts (without directly inputting the directory) is to Alt+Enter the command “cd(dirname(@ **FILE** ))”. Shift+Enter won’t work because it’s like pasting it into the RELP.

If you have a better solution, please post it!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 10, 2021, 11:43pm UTC](https://discourse.julialang.org/t/the-macrocall-file-does-not-return-the-directory-of-the-file-but-rather-the-repl-output-number/55585/9 "2021-10-10T23:43:06Z")

</div>

this should be a function provided by VSCode along the line of “cd to the file I’m looking at right now in my terminal panel”
