purple-plastic-57801
07/18/2023, 12:37 AMpurple-plastic-57801
07/18/2023, 12:40 AM__init__.py
file lives in the directory? Or at least it isn't working for me.broad-processor-92400
07/18/2023, 12:57 AMimportlib
, the directory itself doesn't have __init__.py
in it, although it is nested within a top-level one that does.
I think PEXes ~always run as actual files on disk these days, so using open
+ __file__
can likely work too.
I don't know of a way to identify the location of the original PEX file, although there might be some goodies hiding in https://pex.readthedocs.io/en/v2.1.138/api/vars.htmlbroad-processor-92400
07/18/2023, 12:58 AMPEX
environment variablepurple-plastic-57801
07/18/2023, 1:02 AMpurple-plastic-57801
07/18/2023, 1:06 AMbroad-processor-92400
07/18/2023, 1:09 AM<http://importlib.resources.as|importlib.resources.as>_file
? It seems to have documentation that suggests you are right...
that said, by the time user code within a PEX is running, it's always normal files on disk; the unzipping happens as part of PEX start-uppurple-plastic-57801
07/19/2023, 12:14 AMdef __get_resource_dir_path(package: str, directory: str, file: str = None) -> str:
"""Get a posix path to a resource directory or file within the directory."""
directories = directory.split('/')
resource = importlib.resources.files(package)
path = resource.joinpath(*directories)
if file:
path = path.joinpath(file)
if not path.exists():
raise FileNotFoundError(f"The file '{file}' does not exist in the directory path '{directory}'")
else:
if not path.is_dir():
print(resource.joinpath())
raise NotADirectoryError(f"The directory path '{directory}' does not exist")
return path.as_posix()
and this BUILD file
python_sources(
dependencies=[
"//frontend:python-sources",
]
)
pex_binary(
name="binary",
entry_point="main.py",
)
pants run path/to/main.py
-> works
pants run path/to:binary
-> throws a NotADirectoryError
🤔
How does run path/to/module.py
differ from run path/to:binary
?purple-plastic-57801
07/19/2023, 12:39 AM__init__.py
is being passed but the actual build/dist dirs are just showing up without an init filepurple-plastic-57801
07/19/2023, 12:40 AMteletom/frontend/BUILD
I have
...
shell_command(
name="build",
command="npm run build",
tools=["node", "sh", "npm"],
execution_dependencies=[":node-modules", ":frontend-sources"],
extra_env_vars=["CI=true"],
output_directories=["dist", "build"],
timeout=300,
)
experimental_wrap_as_resources(
name="resources",
inputs=[":build"],
)
python_sources(name="python-sources", dependencies=[":resources"])
purple-plastic-57801
07/19/2023, 12:41 AMpython_sources(
dependencies=[
"//teletom/frontend:python-sources",
]
)
pex_binary(
name="binary",
entry_point="main.py",
)
purple-plastic-57801
07/19/2023, 1:11 AMpackage.json
did the trick
"postbuild": "touch __init__.py",
"pantsbuild": "npm run build && npm run postbuild",
Then I can load the package teletom.frontend