# How to #load with a path?

**URL:** <https://discuss.ocaml.org/t/how-to-load-with-a-path/11200>\
**Category:** Learning\
**Created:** [January 17, 2023, 8:44am UTC](https://discuss.ocaml.org/t/how-to-load-with-a-path/11200 "2023-01-17T08:44:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ysalmon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ysalmon/32/4168_2.png) [@ysalmon](https://discuss.ocaml.org/u/ysalmon)\
**Post date:** [January 17, 2023, 8:44am UTC](https://discuss.ocaml.org/t/how-to-load-with-a-path/11200/1 "2023-01-17T08:44:01Z")

</div>

I am using the OCaml REPL and I need to load a precompiled .cmo file. This works fine if the file is in the working directory.

However if the file lies elsewhere, specifying an absolute or relative path does not show an error on loading, but the corresponding module is not available :

```ocaml
# #load "corrige/build/lin/5.0.0/support.cmo";;                                                                                          
# #show Support;;
Unknown element.

```

I know I can use `#cd` beforehand, but I find this cumbersome :

```ocaml
# #cd "corrige/build/lin/5.0.0";;
# #load "support.cmo";;
# #show Support;;
module Support :
  sig
    module Patch : sig ... end
    module Types : sig ... end
    module Regexp : sig ... end
    module Determinisation : sig ... end
    module Glushkov : sig ... end
    module Affichage : sig ... end
    module Aide : sig ... end
  end

```

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [January 17, 2023, 9:07am UTC](https://discuss.ocaml.org/t/how-to-load-with-a-path/11200/2 "2023-01-17T09:07:26Z")

</div>

The toplevel needs to see the `cmi` file of a module to be able to interact with it. So you should add the directory to the load path. I think this sequence should work for your:

```auto
# #directory "corrige/build/lin/5.0.0";;
# #load "support.cmo";;
# #show Support;;

```

(the reason why it works with the `#cd` is that `.` is part of the load path)

---

<div class="post-metadata">

**Author:** ![ysalmon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ysalmon/32/4168_2.png) [@ysalmon](https://discuss.ocaml.org/u/ysalmon)\
**Post date:** [January 17, 2023, 9:18am UTC](https://discuss.ocaml.org/t/how-to-load-with-a-path/11200/3 "2023-01-17T09:18:01Z")

</div>

Thanks. Can we use variables here ?

I tried to do

```ocaml
#directory ("corrige/build/lin/" ^ Sys.ocaml_version);;

```

and other variants, but to no avail.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [January 17, 2023, 9:34am UTC](https://discuss.ocaml.org/t/how-to-load-with-a-path/11200/4 "2023-01-17T09:34:19Z")

</div>

No, toplevel directives (stuff starting with `#`) work at a “meta” level.

---

<div class="post-metadata">

**Author:** ![vlaviron](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@vlaviron](https://discuss.ocaml.org/u/vlaviron)\
**Post date:** [January 17, 2023, 10:48am UTC](https://discuss.ocaml.org/t/how-to-load-with-a-path/11200/5 "2023-01-17T10:48:14Z")

</div>

If you use the functions from the `Topdirs` module directly, you should be able to pass arbitrary values:

```ocaml
# Topdirs.dir_directory ("corrige/build/lin/" ^ Sys.ocaml_version);;
# #load "support.cmo";;
# #show Support;;

```
