Search code examples
elixirelixir-mix

Elixir library application path to scripts


I have a Python script that I run as part of an Elixir library that I am writing - that is, upon starting the application, I run System.cmd("python", [path/to/script], []) in one of my start_link functions.

I install the script in the user's deps/mylibrary/lib folder.

The path/to/script is the part that I don't know how to do - I can deduce where the script is located by evaluating System.cwd() and looking at the result, but this seems wrong.

I figure there must be a way to do this using Mix.


Solution

  • Files that are needed by a library/package at runtime should be stored in the priv/ folder. Everything in this directory will be present in Erlang Releases created by e.g. Distillery. At runtime, you can use the path returned by :code.priv_dir/1 to know the location of the priv directory of the package.

    So, if your Python script is in priv/foo/bar.py, you can get the absolute path of that file using:

    Path.join(:code.priv_dir(:my_package), "foo/bar.py")
    

    where :my_package is the name of your package.