proud-art-41399
10/03/2023, 9:11 AMruntime.virtualenv
in the project file. Especially does it switch to the virtual env prior to running the Pulumi program? As it seems it does not. Here's the context.
I'm using Ubuntu 22.04 which uses Python 3.10 by default. I also installed Python 3.11 (from PPA) because that's the target runtime I use on AWS. I'm provisioning couple of Lambda functions and in the Pulumi program, I use local.Command
from Pulumi Command provider to prepare the distribution package for the Lambdas. The command installs the dependencies using pip
into a directory containing the handler and then uses the directory in pulumi.FileArchive
supplied to the Lambda resource. The problem is as follows. The default system Python is 3.10 and I can't simply change it because it breaks some things (e.g. APT package manager). I thus manually create the venv
directory for Pulumi with python3.11 -m venv venv
. I expect that when running pulumi up
, the Pulumi program runs in context of the virtual env, i.e. using pip
from the virtual env and also pointing the python
binary to the one from virtual env. But it's not the case, it uses Python 3.10 system default instead. That's a problem, because one of the dependencies is Pydantic (via AWS Lambda Powertools) and it needs the installed version to exactly match the target runtime platform. It fails running the Lambda function, because the installed pydantic-core
package is for Python 3.10 while the runtime is Python 3.11. It fails with
Unable to import module 'lambda_function': No module named 'pydantic_core._pydantic_core'
If I temporarily switch the system default to Python 3.11 (using alternatives), it works and correctly installs the package for Python 3.11.
I know I can manually switch to the virtual env prior to running pulumi up
, it's just about the expectations.
Thanksdry-keyboard-94795
10/03/2023, 9:30 AMruntime.options.virtualenv
specified in Pulumi.yaml
?proud-art-41399
10/03/2023, 9:31 AMoptions
in the path.dry-keyboard-94795
10/03/2023, 9:34 AM./venv/bin/python --version
gives you Python 3.11.x?proud-art-41399
10/03/2023, 9:34 AMdry-keyboard-94795
10/03/2023, 9:37 AMpip
, it's installing the wrong versions, or that pulumi isn't using the venv?proud-art-41399
10/03/2023, 9:40 AMpip
is a Python script which contains a shebang with path to the Python interpreter. When Pulumi is not using the venv
, pip
uses the wrong Python interpreter (on a system path)dry-keyboard-94795
10/03/2023, 9:48 AMsource .venv/bin/activate
(ie, use the venv in current shell), it all works.
But without, pulumi defaults to using the system python env for running and installing.
Can you post the contents of Pulumi.yaml
, can redact anything sensitive?pip
side, I wasn't aware pulumi did dependency management. Always managed my own venvs 🙂proud-art-41399
10/03/2023, 9:51 AMso if you doYes, exactly.(ie, use the venv in current shell), it all works.source .venv/bin/activate
But without, pulumi defaults to using the system python env for running and installing.That's not exactly what I stated (and hence the question). Pulumi clearly uses the
venv
for installing the dependencies and also interpreting the Pulumi program. It just doesn't switch to the virtualenv. (And thus, when using local.Command
with command like pip install ...
, it uses the pip
on system path instead the one in the virtualenv.)name: xxx-prod
description: xxx
runtime:
name: python
options:
virtualenv: venv
options:
refresh: always
dry-keyboard-94795
10/03/2023, 10:01 AMproud-art-41399
10/03/2023, 10:07 AMlocal.Command
would most probably run the command in a subprocess... Thanks!dry-keyboard-94795
10/03/2023, 1:15 PMlocal.Command
should use the venv as well.
It's worth raising a feature request.
I've similar problems with passing Google Auth tokens around, and it'd be good to see functionality to remove this kind of boilerplategreat-sunset-355
11/04/2023, 11:20 PMruntime.virtualenv
config at all because I've had multiple projects using the same VENV and just used poetry shell
before I started working.
Nevertheless, I also made a mistake of using pedantic for pulumi.ComponentResource
which resulted in a lot of unnecessary type headaches with outputs.
Apart from that I used ASDF to manage my python installations and sometimes Pulumi just did not register the PATH to python binary correctly 🤷 (perhaps it has to do something with multiple PyCharm windows I had opened)
In the end, I decided to move to TypeScript because it is much easier to express and write for pulumi. Mainly due to required boilerplate of for python classes (this ma be a gamechanger though https://github.com/pulumi/pulumi/issues/11732)
As for lambdas, I find your solution interesting. I settled on dissociating the artifact from pulumi and just give pulumi an S3 path of pre-built package, so that lambda can use any runtime and I can write a more generic/flexible component.