Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different behavior for loading in REPL and in script #27390

Closed
PetrKryslUCSD opened this issue Jun 2, 2018 · 2 comments
Closed

Different behavior for loading in REPL and in script #27390

PetrKryslUCSD opened this issue Jun 2, 2018 · 2 comments

Comments

@PetrKryslUCSD
Copy link

The following works in the REPL, but not when the same functions are called from within a script:

julia> d = pwd()
"c:\\Users\\PetrKrysl\\devel\\CoNoGMOR\\investigations\\square_plate_hva"
julia> cd("../../../FinEtools/src")
julia> println("pwd() = $(pwd())")
pwd() = c:\Users\PetrKrysl\devel\FinEtools\src
julia> include("./FinEtools.jl")
Main.FinEtools
julia> cd(d)

From within a script the loading of include("./FinEtools.jl") fails:

julia> include("runsimadapt.jl")
pwd() = c:\Users\PetrKrysl\devel\FinEtools\src
ERROR: LoadError: could not open file c:\Users\PetrKrysl\devel\CoNoGMOR\investigations\square_plate_hva\FinEtools.jl

Apparently the file is being sought in the wrong folder (even though pwd says we are in the correct folder containing the file to be loaded).

@StefanKarpinski
Copy link
Member

StefanKarpinski commented Jun 4, 2018

include works relative to the directory in which the current source file resides or the current working director if you're in the REPL. This allows sets of files that are "glued together" by relative include calls to be position-independent in your file system. So the include("FinETools.jl") call looks for c:\Users\PetrKrysl\devel\FinEtools\src\FinETools.jl in the REPL but looks for c:\Users\PetrKrysl\devel\CoNoGMOR\investigations\square_plate_hva\FinEtools.jl when it occurs inside of c:\Users\PetrKrysl\devel\CoNoGMOR\investigations\square_plate_hva\runsimadapt.jl. The bottom line is that using cd to change working directories in order to load relative code in scripts won't work (and is probably a bad idea anyway). If you really want to do it anyway, you'll have to explicitly call pwd or abspath when constructing a path to pass to include like so: include(abspath("FinETools.jl")).

@PetrKryslUCSD
Copy link
Author

PetrKryslUCSD commented Jun 4, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants