Skip to content

exclude does not work on dependencies #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hinnerk opened this issue Feb 19, 2018 · 6 comments
Open

exclude does not work on dependencies #138

hinnerk opened this issue Feb 19, 2018 · 6 comments

Comments

@hinnerk
Copy link

hinnerk commented Feb 19, 2018

Files that are installed via requirements.txt can't be excluded. For example this doesn't work:

package:
  exclude:
    - __pycache__/**
    - pandas/tests/**
@dschep
Copy link
Contributor

dschep commented Feb 19, 2018

That's on purpose so that you don't have to enumerate the packages if you go with an exclude all, then include some approach that I often use:

package:
  exclude:
    - '**/*'
  include:
    - handler.py

I think the best approach to what you're asking for would be to implement a plugin specific exclude option that would make your example look like:

custom:
  pythonRequirements:
    exclude:
      - __pycache__/**
      - pandas/tests/**

or just a toggle to turn off the plugin's code that adds to the includes:

custom:
  pythonRequirements:
    modifyPackageInclude: false

@dschep
Copy link
Contributor

dschep commented Feb 19, 2018

I don't really have the time to implement either feature, but would be happy to accept a PR for either approach.

@hinnerk
Copy link
Author

hinnerk commented Feb 20, 2018

We've more or less solved that for us with the serverless-scriptable-plugin:

plugins:
  - serverless-scriptable-plugin

custom:
  scriptHooks:
    before:package:createDeploymentArtifacts:
      - "rm -rf .serverless/requirements/pandas/tests || echo ERR: UNABLE TO REMOVE pandas/tests"
      - "rm -rf .serverless/requirements/numpy/core/tests || echo ERR: UNABLE TO REMOVE numpy/core/tests"

@dschep
Copy link
Contributor

dschep commented Feb 20, 2018

Nice workaround using a great plugin!

@ghost
Copy link

ghost commented Jun 18, 2019

I'm trying to remove some language models from spacy to reduce the overall size, but when the above is used in combination with serverless-python-requirements this doesn't seem to work.

What I wrote

scriptHooks:
    before:package:createDeploymentArtifacts:
      - "mv .serverless/requirements/spacy/lang .serverless/requirements/spacy/lang_old"
      - "mkdir .serverless/requirements/spacy/lang"
      - "mv .serverless/requirements/spacy/lang_old/en .serverless/requirements/spacy/lang/"
      - "mv .serverless/requirements/spacy/lang_old/xx .serverless/requirements/spacy/lang/"
      - "cp .serverless/requirements/spacy/lang_old/*.py .serverless/requirements/spacy/lang/"
      - "rm -rf .serverless/requirements/spacy/lang_old"

Output:

Serverless: Adding Python requirements helper...
Serverless: Generated requirements from /home/user/Projects/test-spacy/requirements.txt in /home/user/Projects/test-spacy/.serverless/requirements.txt...
Serverless: Installing requirements from /home/user/Projects/test-spacy/.serverless/requirements/requirements.txt ...
Serverless: Running ...
Serverless: Zipping required Python packages...
Running command: mv .serverless/requirements/spacy/lang .serverless/requirements/spacy/lang_old
Running command: mkdir .serverless/requirements/spacy/lang
Running command: mv .serverless/requirements/spacy/lang_old/en .serverless/requirements/spacy/lang/
Running command: mv .serverless/requirements/spacy/lang_old/xx .serverless/requirements/spacy/lang/
Running command: cp .serverless/requirements/spacy/lang_old/*.py .serverless/requirements/spacy/lang/
Running command: rm -rf .serverless/requirements/spacy/lang_old
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Removing Python requirements helper...
Serverless: Injecting required Python packages to package...

Interesting that the .serverless/requirements folder which is used for creating the .requirements.zip has been modified appropriately. Any guidelines would be most helpful!

@Sam152
Copy link

Sam152 commented Jul 1, 2021

Hey @kalinkirev, I found that I had to remove files from the built zip file to get this working. I wrote a blog post with an example here: https://dev.to/sam152/crude-python-tree-shaking-for-squeezing-into-aws-lambda-package-size-limits-357a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants