addpath from Matlab?

546 views
Skip to first unread message

Ethan Anderes

unread,
Mar 11, 2014, 12:49:47 AM3/11/14
to julia...@googlegroups.com
Hi,

Is there an addpath equivalent from Matlab? Basically, I have a directory that contains a bunch of scripts and modules. I've been using include to run the scripts but don't want to add the full path to each script. For example, dir is a directory which contains Mod.jl and script.jl. The last line in the following code gives an error:


push!(LOAD_PATH, pwd()*"/dir")
using Mod                 # this works
include("script.jl")      # this doesn't work


Is there a way to tell the repl to search in dir when I include("script.jl")?

Sorry if I missed this in the documentation...

Kevin Squire

unread,
Mar 11, 2014, 1:36:29 AM3/11/14
to julia...@googlegroups.com
If you use require("script.jl") instead of include, it will search the LOAD_PATH.

Cheers,
   Kevin

Ethan Anderes

unread,
Mar 11, 2014, 1:58:06 AM3/11/14
to julia...@googlegroups.com
Thanks Kevin. I hadn't tried require since I wanted the ability to chance the scripts and re-include them. However, it appears require/reload will accomplish the same thing (although I guess I will need to change all my requires to reloads if I want it done without exiting the repl). 

Cheers,
Ethan

J Luis

unread,
Mar 11, 2014, 7:17:17 AM3/11/14
to julia...@googlegroups.com
I've been here before too (and will come back)

An "addpath" would be a far far simpler to use solution.

Tim Holy

unread,
Mar 11, 2014, 8:47:59 AM3/11/14
to julia...@googlegroups.com
Do you mean, all you want is "addpath(dirname) = push!(LOAD_PATH, dirname)"?
Or something different?

--Tim

J Luis

unread,
Mar 11, 2014, 10:43:47 AM3/11/14
to julia...@googlegroups.com


Do you mean, all you want is "addpath(dirname) = push!(LOAD_PATH, dirname)"?

By itself it would be already useful (imagine when one do not know this solution and have to search the docs for it)
 
Or something different?

Precisely. What I was referring was a way to declare paths that would be searched by the 'include' and not having errors like this (when the test0.jl file exists in LOAD_PATH). And not having to play around with the 'require()'

julia> include("test0.jl")
ERROR: could not open file C:\programs\julia64\test0.jl
 

Ethan Anderes

unread,
Mar 11, 2014, 12:38:31 PM3/11/14
to julia...@googlegroups.com
Yep. This is what I was hoping for too. However, just to be clear I would want include("test0.jl") to work when the directory which contains test0.jl is in LOAD_PATH.

Cheers,
Ethan

Kevin Squire

unread,
Mar 11, 2014, 1:00:26 PM3/11/14
to julia...@googlegroups.com
Maybe we should stop exporting "include", so that people use "require" and LOAD_PATH is always searched?

Ethan Anderes

unread,
Mar 11, 2014, 3:28:17 PM3/11/14
to julia...@googlegroups.com
Hi Kevin:

As a convert from Matlab, the first thing I asked for when shopping around for an alternative language for scientific computing was "what's the command I use to run a file, top to bottom".
From my view, this is a important command to have front and center. I would want to avoid forcing newbies to use "require"... for example just by looking at the help documentation I knew there would be some spurious behavior I didn't understand...

julia> ?require
Loading help data...
Base.require(file::String...)

Load source files once, in the context of the "Main" module, on
every active node, searching the system-wide "LOAD_PATH" for
files. "require" is considered a top-level operation, so it sets
the current "include" path but does not use it to search for
files (see help for "include"). This function is typically used
to load library code, and is implicitly called by "using" to load
packages.

Just my two cents.
Cheers,
Ethan

Mauro

unread,
Mar 11, 2014, 4:34:31 PM3/11/14
to julia...@googlegroups.com
This and related topics seem to crop up a lot, e.g. almost concurrently
this discussion happened:
https://groups.google.com/d/msg/julia-users/xbsdu8Ob4cw/2Vtyo9CFhYoJ

And Kevin opened this issue to improve the documentation:
https://github.com/JuliaLang/julia/issues/6082
It is a bit focused on packages but the doc-improvement should also be
including this 'just running' a file issue. I linked to this thread in
the issue now.

Jonathan Malmaud

unread,
Mar 11, 2014, 7:21:05 PM3/11/14
to julia...@googlegroups.com
It might be useful to add functionality equivalent to IPython's 'run' magic. From http://ipython.org/ipython-doc/dev/interactive/tutorial.html:

Running and Editing

The %run magic command allows you to run any python script and load all of its data directly into the interactive namespace. Since the file is re-read from disk each time, changes you make to it are reflected immediately (unlike imported modules, which have to be specifically reloaded). IPython also includes dreload, a recursive reload function.

%run has special flags for timing the execution of your scripts (-t), or for running them under the control of either Python’s pdb debugger (-d) or profiler (-p).

Ethan Anderes

unread,
Mar 13, 2014, 6:23:08 PM3/13/14
to julia...@googlegroups.com
Sorry to resurrect this issue, but I was playing around and came up with the following function to accomplish what we want.

function runfile(file::String)
flag = true # changes to false if the file is found
for dir in [pwd(), LOAD_PATH...]
dfile = joinpath(dir,file)
if isfile(dfile)
include_string(readall(dfile))
flag = false
break
end
end
if flag error("Error: could not open $file") end
end


This works right? Am I missing anything subtle about it's behavior? In particular, I want it to work like "include" but checks LOAD_PATH if it can't find the file in the working directy?

Cheers,
Ethan

Reply all
Reply to author
Forward
0 new messages