# How to \`dynamic-require\` from the enclosing module?

**URL:** <https://racket.discourse.group/t/how-to-dynamic-require-from-the-enclosing-module/3345>\
**Category:** Questions & Answers\
**Tags:** question\
**Created:** [November 21, 2024, 1:09pm UTC](https://racket.discourse.group/t/how-to-dynamic-require-from-the-enclosing-module/3345 "2024-11-21T13:09:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [November 21, 2024, 1:09pm UTC](https://racket.discourse.group/t/how-to-dynamic-require-from-the-enclosing-module/3345/1 "2024-11-21T13:09:26Z")

</div>

I'm trying to obtain a function from a symbol within a submodule, something like:

```scheme
# lang racket

(define (foo x) (list x))

(module+ my-mod
  (dynamic-require <something> 'foo))

```

This can be done with some syntax magic (with syntax/location) and `<something> = (list 'file <file-path-string>)`, but there must be a proper, simpler and likely more robust way.

I also tried `namespace-variable-value` but while it works within DrRacket it doesn't work on the command line — or I did it wrong.

---

<div class="post-metadata">

**Author:** ![mflatt](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/mflatt/32/6_2.png) [@mflatt](https://racket.discourse.group/u/mflatt)\
**Post date:** [November 21, 2024, 2:04pm UTC](https://racket.discourse.group/t/how-to-dynamic-require-from-the-enclosing-module/3345/2 "2024-11-21T14:04:47Z")

</div>

I recommend `(quote-module-path "..")` using `syntax/location`.

The primitive form would be `(module-path-index-join '(submod "..") (variable-reference->module-path-index (#%variable-reference)))`, and that's roughly what `quote-module-path` expands to.

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [November 21, 2024, 2:35pm UTC](https://racket.discourse.group/t/how-to-dynamic-require-from-the-enclosing-module/3345/3 "2024-11-21T14:35:56Z")

</div>

I see. So, IIUC, once encapsulated in a library, this still requires passing a syntax object from the actual submodule to the library so as to know the source location, is that correct?

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [November 21, 2024, 3:52pm UTC](https://racket.discourse.group/t/how-to-dynamic-require-from-the-enclosing-module/3345/4 "2024-11-21T15:52:58Z")

</div>

I would use `define-runtime-module-path-index` from `racket/runtime-path`, which also registers a dynamic dependency for tools like `raco exe`:

```scheme
#lang racket

(define (foo x)
  (list x))

(provide foo)

(module+ main
  (require racket/runtime-path)
  (define-runtime-module-path-index foo-mpi
    '(submod ".."))
  (dynamic-require foo-mpi 'foo))

```

> [@mflatt](#):
>
> I recommend `(quote-module-path "..")` using `syntax/location`.

My concern about this is that the [docs](https://docs.racket-lang.org/syntax/Source_Locations.html#%28form._%28%28lib._syntax%2Flocation..rkt%29._quote-module-path%29%29) say the result will only be a valid module path “when possible”. Since the docs say it “builds the result using [`quote`](https://docs.racket-lang.org/reference/quote.html#%28form._%28%28quote._~23~25kernel%29._quote%29%29), a path, [`submod`](https://docs.racket-lang.org/reference/require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._submod%29%29), or `'top-level`”, I thought it had limitations with some kinds of module paths, at least unsaved modules in DrRacket.
