enough-leather-70274
06/22/2021, 11:18 PMwork_dir
param to be set to) a higher level directory than the project dir, e.g. a directory further up the hierarchy from the one containing the yaml and main.py files? That way common code can be shared between multiple pulumi projects as sibling or parent modules/ packages and imported in _main.py and python would resolve them (as they'd naturally be included in sys.path).
Or is there a different more obvious solution or workaround that I'm missing?red-match-15116
06/22/2021, 11:25 PMenough-leather-70274
06/22/2021, 11:29 PMred-match-15116
06/22/2021, 11:35 PMenough-leather-70274
06/22/2021, 11:35 PMauto.select_stack
passing in work_dir as infra/usfrom infra.components import Vpc
red-match-15116
06/22/2021, 11:36 PMenough-leather-70274
06/22/2021, 11:37 PMModuleNotFoundError: No module named 'infra'
red-match-15116
06/22/2021, 11:37 PMpython main.py
from deploy?enough-leather-70274
06/22/2021, 11:37 PMred-match-15116
06/22/2021, 11:38 PMenough-leather-70274
06/22/2021, 11:38 PMred-match-15116
06/22/2021, 11:40 PMenough-leather-70274
06/22/2021, 11:40 PMred-match-15116
06/22/2021, 11:40 PMpresumably because setting the work_dir to infra/us in effect does the same as using the CLI in that directoryYeah that’s right…
enough-leather-70274
06/22/2021, 11:41 PMred-match-15116
06/22/2021, 11:43 PMfrom ..components import Vpc
(I hear you, I’m just not sure we have the capacity to take this on right now so I’m trying to figure out if there’s a workaround)enough-leather-70274
06/22/2021, 11:43 PMImportError: attempted relative import with no known parent package
red-match-15116
06/22/2021, 11:46 PMOther python tools I’ve worked with usually decouple configuration from the execution contextI think there’s a way to do this. Gimme a second.
sys.path
as @full-artist-27215 mentioned?enough-leather-70274
06/22/2021, 11:55 PMred-match-15116
06/23/2021, 12:00 AMYeah, I’ve not had luck with that so far eitherHmm, how do you mean? I would think
sys.path.append(os.path.abspath("../components.py"))
would then let you do from components import Vpc
enough-leather-70274
06/23/2021, 12:04 AMred-match-15116
06/23/2021, 12:10 AMenough-leather-70274
06/23/2021, 12:10 AMred-match-15116
06/23/2021, 12:12 AM__main__.py
file is by passing in a main
key to the Pulumi.yaml
file but I’m not sure that actually solves your problem since you have multiple pulumi projects in one repo.enough-leather-70274
06/23/2021, 12:14 AMred-match-15116
06/23/2021, 12:29 AMvenv
with pip install -e
. I’m not sure if that would work with your directory structure since infra
contains both the components and the pulumi programs so dunno if that would cause some weird recursive isses, but if there was a sibling directory to infra
called resources
or something which contained components.py
and __init__.py
you could conceivably pip install -e /path/to/resources
and then be able to do from resources.components import Vpc
.
Again, not the ideal solution but a possible workaround.enough-leather-70274
06/23/2021, 12:49 AMfull-artist-27215
06/23/2021, 12:54 AM__main__.py
file, along with Pulumi.yaml
and my stack config files. All my shared code imports are absolute, and I add
import sys
sys.path.insert(0, "..")
to the top of each __main__.py
file. So far, this works, with a minimal amount of fuss (though being able to control things more concretely with some CLI arguments would ultimately be fantastic). I just cd pulumi/<PROJECT>
and run pulumi up
and things work.
(I'm maybe doing some unconventional virtualenv management, though, since all the projects share a virtualenv that I manage outside of Pulumi's built-in support via project config options. That may affect the overall suitability of this approach somewhat)enough-leather-70274
06/23/2021, 1:04 AMfull-artist-27215
06/23/2021, 1:08 AMrunpy.run_module
(https://docs.python.org/3/library/runpy.html#runpy.run_module), as opposed to just runpy.run_path
would be useful for dealing with this kind of usecase? I think that might alleviate the need to manually patch sys.path
enough-leather-70274
06/23/2021, 1:10 AMgreat-sunset-355
06/23/2021, 7:53 AM-C, --cwd string Run pulumi as if it had been started in another directory
enough-leather-70274
06/23/2021, 8:05 AMpulumi --cwd=us preview
I get ModuleNotFoundError: No module named 'infra'
.pulumi --cwd=./.. preview
I get error: no Pulumi.yaml project file found
great-sunset-355
06/23/2021, 8:10 AMbrave-knife-93369
06/24/2021, 12:35 PMred-match-15116
06/24/2021, 2:17 PMbrave-knife-93369
06/24/2021, 2:27 PMenough-leather-70274
06/24/2021, 10:23 PMred-match-15116
06/24/2021, 10:24 PMenough-leather-70274
06/24/2021, 10:26 PM