Why do hitting Ctrl+Enter or cutting and pasting code in the REPL yield different results?

I have some code that I am working on in VSCode, where at some point I try to include a file with this line:


Then I get an error message:

ERROR: SystemError: opening file "c:\\Users\\fsald\\Dropbox\\Code\\Julia\\bootstrapwalkforward\\BWModule\\src\\src\\ModelData.jl": No such file or directory

The problem is the src\\src in the path. The true path has only one src. When I hit pwd() on the REPL I get a path that does not include src at the end. If I write pwd() in the editor, select that line, and hit Ctrl+Enter I get the same exact path, without src at the end.

Selecting that line and hitting Ctrl+Enter generates the same error.

However, if I cut and paste that exact same line on the REPL the code runs fine.

This is a problem as when I am working on the code sometimes after selecting a code bit I hit Ctrl+Enter and other times I copy that code and paste it on the REPL.

I would like to understand the reason for this difference in behavior, so that perhaps I can make the behavior uniform when I hit Ctrl+Enter or when I cut and paste code on the REPL.

Any ideas?

1 Like

I didn’t know the notation . in a file path, and it gives me an error in my computer. Are you trying to use "$(homedir())".

Notice that, if you work with commands that handle files, the output could change depending on whether you write the prompt in the REPL or with ctrl+enter. For instance, @__FILE__ provides the path to the file. But if you’re in the REPL, you aren’t working in a specific file and it’ll return "REPL[7]". Instead, pressing ctrl+enter will return the name of the file from which you’re executing it.

Don’t know if all this helps.

The . in the ./src notation is a “relative” path — the question is relative to what? At the REPL it’s relative to your current working directory. When Julia is executing a file, it’s relative to the file it’s currently executing.

When you execute a line of code with control-enter, VS Code is choosing to make it relative to the file you’re in so it matches the behavior of Julia itself.


Explains perfectly what was happening. I guess I will use cd(“./src”) to change my working directory to be the same as the directory the file I am executing is in. After that it should make no difference whether I paste code on the REPL or select the code on the editor and hit Ctrl+Enter.