Skip to content
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

Several pip fixes for Python hydration #186

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
- name: Install
run: |
npm install
pip3 install -r requirements.txt
python3 -m pip install -r requirements.txt

- name: Test
run: npm test
Expand Down
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
- Docs: https://arc.codes/docs/en/guides/developer-experience/dependency-management#python
- This supports global options passed in a root `requirements.txt` file (example: `--extra-index-url https://test.pypi.org/simple/`), but does not yet support dependencies versioned at root, or shared or views dependencies
- All project dependencies must be installed on the system prior to deployment
- Python Lambda treeshaking also requires the `pipdeptree` package to be available from shell; ensure you've run `pip3 install pipdeptree` prior to use
- Python Lambda treeshaking also requires the `pipdeptree` package to be available from shell; ensure you've run `python3 -m pip install pipdeptree` prior to use

---

Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Installs function dependencies, then invokes [`hydrate.shared()`][shared].
To ensure local development behavior is as close to `staging` and `production` as possible, `hydrate.install()` (and other hydrate functions) uses:

- **Node.js**: `npm ci` if `package-lock.json` is present and `npm i` if not; or `yarn`
- **Python**: `pip3 install`
- **Python**: `python3 -m pip install`
- **Ruby**: `bundle install`

Note: by default `update` also installs dependencies in shared folders like `src/shared` and `src/views`.
Expand All @@ -75,7 +75,7 @@ Updates function dependencies, then invokes [`hydrate.shared()`][shared].
`update` is functionally almost identical to [`install`][install], except it will update dependencies to newer versions _if they exist_. This is done via:

- **Node.js**: `npm update` or `yarn upgrade`
- **Python**: `pip3 install -U --upgrade-strategy eager`
- **Python**: `python3 -m pip install -U --upgrade-strategy eager`
- **Ruby**: `bundle update`

Note: by default `update` also updates dependencies in shared folders like `src/shared` and `src/views`.
Expand Down
6 changes: 3 additions & 3 deletions src/actions/autoinstall/python/check-py-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ let { spawnSync } = require('child_process')

module.exports = function checkPyTools () {

let cmd = 'pip3'
let args = [ 'list' ]
let cmd = 'python3'
let args = [ '-m', 'pip', '--version' ]
let raw = spawnSync(cmd, args, {
cwd: __dirname,
shell: true,
Expand All @@ -19,7 +19,7 @@ module.exports = function checkPyTools () {
if (l.split(' ')[0] === 'pipdeptree') pipdeptree = true
})
if (!pipdeptree) {
throw Error(`pipdeptree required for treeshaking Python Lambdas, please run 'pip3 install pipdeptree'`)
throw Error(`pipdeptree required for treeshaking Python Lambdas, please run 'python3 -m pip install pipdeptree'`)
}
return true
}
4 changes: 2 additions & 2 deletions src/actions/autoinstall/python/get-dep-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function getDepTree (topLevelDeps) {
let shell = true

if (!pipdeptree) {
let pipdeptreeCheck = spawnSync('pip3', [ 'list' ], { shell })
let pipdeptreeCheck = spawnSync('python3', [ '-m', 'pip', 'list' ], { shell })
if (pipdeptreeCheck.status) {
console.error(pipdeptreeCheck.output.toString())
throw Error(`pip3 error`)
Expand All @@ -16,7 +16,7 @@ module.exports = function getDepTree (topLevelDeps) {
if (l.split(' ')[0] === 'pipdeptree') pipdeptree = true
})
if (!pipdeptree) {
throw Error(`pipdeptree required for treeshaking Python Lambdas, please run 'pip3 install pipdeptree'`)
throw Error(`pipdeptree required for treeshaking Python Lambdas, please run 'python3 -m pip install pipdeptree'`)
}
}

Expand Down
35 changes: 23 additions & 12 deletions src/actions/install-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,28 @@ module.exports = function hydrator (params, callback) {
else if (isPy) {
let flags = ''
if (lambda) {
// Technique per AWS, found that `--python-version` was essential, but `--implementation cp` may not be
// https://repost.aws/knowledge-center/lambda-python-package-compatible
// This may still not work because of glibc version differences, see:
// https://docs.aws.amazon.com/linux/al2023/ug/compare-with-al2.html#glibc-gcc-and-binutils
let arch = lambda.config.architecture === 'arm64' ? 'manylinux2014_aarch64' : 'manylinux2014_x86_64'
let ver = lambda.config.runtime.split('python')[1]
flags = '--only-binary=:all: ' +
`--platform=${arch} ` +
`--python-version ${ver} `
// Reset flags if installing from Sandbox
if (local) flags = ''
if (!local) {
let arch = lambda.config.architecture === 'arm64' ? 'aarch64' : 'x86_64'
let ver = lambda.config.runtime.split('python')[1]

let pythonPlatformCheck = child.spawnSync('python3', [ '-c', "'import platform; import sys; print(platform.system(), platform.machine(), *platform.python_version_tuple())'", ], { shell: true })
if (pythonPlatformCheck.status) {
console.error(pythonPlatformCheck.output.toString())
throw Error(`python3 error`)
}
let [ platformSystem, platformMachine, platformPythonVersionMajor, platformPythonVersionMinor, ] = pythonPlatformCheck.stdout.toString().trim().split(' ')
let isNative = platformSystem === 'Linux' && platformMachine === arch && `${platformPythonVersionMajor}.${platformPythonVersionMinor}` === ver

if (!isNative) {
// Technique per AWS, found that `--python-version` was essential, but `--implementation cp` may not be
// https://repost.aws/knowledge-center/lambda-python-package-compatible
// This may still not work because of glibc version differences, see:
// https://docs.aws.amazon.com/linux/al2023/ug/compare-with-al2.html#glibc-gcc-and-binutils
flags = '--only-binary=:all: ' +
`--platform=manylinux2014_${arch} ` +
`--python-version ${ver} `
}
}

// Update Python deps
if (!installing) {
Expand All @@ -129,7 +140,7 @@ module.exports = function hydrator (params, callback) {
flags += '-U --upgrade-strategy eager'
}
}
let cmd = `pip3 install -r requirements.txt -t ./vendor ${flags}`.trim()
let cmd = `python3 -m pip install -r requirements.txt -t ./vendor ${flags}`.trim()
exec(cmd, options, callback)
}

Expand Down