2

In Lua is there a way to define package.path to apply only to a local scope?

I understand I could store the package.path in a local variable and restore it before my script ends as in the example below but is there a cleaner way?

local startingPackagePath = package.path
package.path = "../Lib/?.lua;" .. package.path
local someLib = require "someLib"
package.path = startingPackagePath
--Do some stuff
Puddler
  • 2,619
  • 2
  • 17
  • 26

1 Answers1

2

I don't think there is a shorter way to make package.path module-specific, but you may consider using package.preload that allows to provide a custom loader, which may look at module-specific path.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56