Skip to content

Conversation

@pdecat
Copy link
Contributor

@pdecat pdecat commented Jul 26, 2021

Description

This PR adds support for installing dependencies with poetry for projects with pyproject.toml and optionally poetry.lock files.

Marked as draft as poetry does not currently provide a proper way to isolate installed dependencies from virtualenv (pip's --target option) so this implementation is kind of a hack that requires manually excluding undesired files from the lambda package.

Submitted the PR early anyway to get feedback.

Motivation and Context

poetry's is more and more used and has a robust dependency resolver.

Breaking Changes

New poetry install step is not enabled by default and requires explicitly setting poetry_install = True in the source block.

How Has This Been Tested?

  • I have tested and validated these changes using one or more of the provided examples/* projects
rm -rf builds/lambda_layer/ && terraform apply -target module.lambda_layer -auto-approve rm -rf builds/lambda_layer_poetry/ && terraform apply -target module.lambda_layer_poetry -auto-approve rm -rf builds/package_dir/ && terraform apply -target module.package_dir -auto-approve rm -rf builds/package_dir_poetry/ && terraform apply -target module.package_dir_poetry -auto-approve 
build_in_docker = true
runtime = "python3.8"
source_path = "${path.module}/../fixtures/python3.8-app1"
artifacts_dir = "${path.root}/builds/package_dir/"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifying different artifacts_dir for each module makes it easier to check lambda packages content.

Comment on lines +48 to +71
"!_virtualenv.pth",
"!_virtualenv.py",
"!distutils-precedence.pth",
"!easy_install.py",
"!(pip|pip-.*)(\\.virtualenv|/.*)",
"!(pkg_resources|pkg_resources-.*)(\\.virtualenv|/.*)",
"!(setuptools|setuptools-.*)(\\.virtualenv|/.*)",
"!(wheel|wheel-.*)(\\.virtualenv|/.*)",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required as poetry does not support a --target parameter like pip so everything is put in the same directory of the created virtualenv.

os.path.join(path, 'requirements.txt'))
step('zip', path, None)
if runtime.startswith("python"):
pip_requirements_step(os.path.join(path, "requirements.txt"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pip_requirements_step() already appends the filename if path is a directory:

Suggested change
pip_requirements_step(os.path.join(path, "requirements.txt"))
pip_requirements_step(path)
Comment on lines +1029 to +1050
shlex_join([ poetry_exec, "config", "--no-interaction", "virtualenvs.create", "true" ]),
shlex_join([ poetry_exec, "config", "--no-interaction", "virtualenvs.in-project", "true" ]),
shlex_join([ poetry_exec, "install", "--no-interaction" ]),
Copy link
Contributor Author

@pdecat pdecat Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Until poetry install supports a --target parameter, an alternative may be to let poetry export the dependencies in requirements.txt format then install dependencies with pip:

Suggested change
shlex_join([ poetry_exec, "config", "--no-interaction", "virtualenvs.create", "true" ]),
shlex_join([ poetry_exec, "config", "--no-interaction", "virtualenvs.in-project", "true" ]),
shlex_join([ poetry_exec, "install", "--no-interaction" ]),
shlex_join([ poetry_exec, "export", "-f", "requirements.txt", "--output", "requirements.txt" ]),
shlex_join([ python_exec, '-m', 'pip', 'install', '--no-compile', '--prefix=', '--target=.','--requirement=requirements.txt'),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pdecat please add --no-dev to the install command. I believe it's more correct behaviour

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tonkonozhenko good point, done!

@panessa
Copy link
Contributor

panessa commented Nov 23, 2021

I've seen this Pull Request not proceeding since 26th of July @Tonkonozhenko
I would take over if you agree @pdecat

@pdecat
Copy link
Contributor Author

pdecat commented Nov 23, 2021

Well, I've not abandoned it yet, it's just blocked on current poetry limitations. Feel free to experiment if you have better ideas to do it properly.

Marked as draft as poetry does not currently provide a proper way to isolate installed dependencies from virtualenv (pip's --target option) so this implementation is kind of a hack that requires manually excluding undesired files from the lambda package.

@panessa
Copy link
Contributor

panessa commented Nov 24, 2021

@pdecat One good approach might be generate requirements.txt from poetry and use it. Then requirements.txt can be used by pip_requirements

File poetry.toml should also be uploaded inside building container.

poetry config --no-interaction virtualenvs.create true poetry config --no-interaction virtualenvs.in-project true poetry export --with-credentials --format requirements.txt --output requirements.txt
@pdecat
Copy link
Contributor Author

pdecat commented Nov 24, 2021

Tried that, but in some situations, the generated requirements.txt was either incorrect or pip did not manage to process it.

I remember facing issues like those mentioned in https://github.com/python-poetry/poetry/issues/3160

Maybe time to re-evaluate that option.

@pdecat
Copy link
Contributor Author

pdecat commented Nov 25, 2021

Rebased on current master branch.

@panessa
Copy link
Contributor

panessa commented Nov 25, 2021

That's really interesting @pdecat. In which case requirements.txt generates issue? I've personally never faced a problem like that.

@pdecat
Copy link
Contributor Author

pdecat commented Nov 28, 2021

Tried to reproduce the issue with a few projects of mine but I did not manage to reproduce it with poetry 1.1.11.

@github-actions
Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Jan 11, 2022
@pdecat pdecat changed the title feat: support installing dependencies with poetry feat: Support installing dependencies with poetry Jan 11, 2022
@github-actions
Copy link

This PR was automatically closed because of stale in 10 days

@github-actions github-actions bot closed this Jan 23, 2022
@pdecat
Copy link
Contributor Author

pdecat commented Jan 23, 2022

I'll re-open a new PR once all blocking points are resolved.

@pdecat
Copy link
Contributor Author

pdecat commented Mar 22, 2022

Another issue I faced may have now been resolved: python-poetry/poetry#3472 (comment)

@pdecat
Copy link
Contributor Author

pdecat commented May 17, 2022

Re-opened as #311 using pip to install dependencies.

@github-actions
Copy link

github-actions bot commented Nov 8, 2022

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

3 participants