Open alikian opened 5 years ago
I recommend creating & activating a virtualenv, installing your dependencies, then using sls offline
When each function had separate dependencies I created a local plugin to make it work offline, the plugin modifies sys.path
and makes sure even boto3
and friends are installed. For this to work npx sls requirements install
needs to be run first.
#!/usr/bin/env node
'use strict'
const path = require('path')
const fs = require('fs')
class PythonOfflineFix {
constructor(serverless, options) {
this.hooks = {
'before:offline:start:init': () => this.fixPaths(serverless)
}
if (options.stage === 'local') {
serverless.service.custom.pythonRequirements.noDeploy = []
}
}
async fixPaths(serverless) {
for (const func of Object.values(serverless.service.functions)) {
if (func.runtime.match(/python/)) {
if (func.module) {
func.handler = `${func.module}/${func.handler}`
delete func.module
}
if (!func.environment) {
func.environment = {}
}
func.environment.PYTHONPATH = await fs.promises.realpath(
path.join('.serverless', path.dirname(func.handler), 'requirements')
)
}
}
}
}
module.exports = PythonOfflineFix
@JamesKyburz I was almost forking the serverless-offline to modify that module path. Thank you!
Is there any support for serverless-offline? it seems serverless-offline doesn't call this plug-in