Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load file with a relative path

I am trying to load a file in Lisp from a file in the same directory using a relative path.

My file structure looks like this:

repo/
    subdir/
        main.lisp
        test.lisp

In main.lisp I have a number of function definitions, and in test.lisp I want to test the functions.

I have tried using (load "main.lisp") and (load "main") in test.lisp, as well as a number of variations on the pathname (i.e., including ./ before the filename) but both times I get the following error (where <filename> is the filename passed to the load function):

File-error in function LISP::INTERNAL-LOAD: "<filename>" does not exist.

Is it possible to load main.lisp using a relative path?

It may be worth noting that I am running CMUCL and executing the code using SublimeREPL inside of Sublime Text 3.

like image 986
maxdeviant Avatar asked Sep 12 '14 03:09

maxdeviant


People also ask

How do I add a relative file path?

Relative path Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory.

How do I give a relative a file path in Python?

A relative path starts with / , ./ or ../ . To get a relative path in Python you first have to find the location of the working directory where the script or module is stored. Then from that location, you get the relative path to the file want.

What is an example of a relative path?

A relative path needs to be combined with another path in order to access a file. For example, joe/foo is a relative path.


1 Answers

When a file is being loaded, the variable *LOAD-PATHNAME* is bound to the pathname of the file being loaded, and *LOAD-TRUENAME* to its truename.

So, to load a file in the same directory with the file currently being loaded, you can say

(load (merge-pathnames "main.lisp" *load-truename*))
like image 63
jlahd Avatar answered Oct 26 '22 03:10

jlahd