Hi all, I have a strange problem. I have a Repo w...
# general
b
Hi all, I have a strange problem. I have a Repo with the following structure:
Copy code
- ./pants
- src
  - python
    - shared
      - BUILD
      - logging
        - BUILD
        - config.json
    - accounts
      - BUILD
      - api
        - server.py
    - ...
What I want to do is to include the
config.json
into the accounts package to be able to use a shared logging configuration. This is the BUILD file in
src/python/shared/logging
Copy code
resource(
    name="config",
    source="config.json",
)
This is my
pex_binary
in
src/python/accounts/BUILD
where I want to use the
config.json
Copy code
pex_binary(
    name="serve",
    restartable=True,
    dependencies=[
        "src/python/shared/logging:config",
        ":sources",
    ],
    entry_point="api/server.py",
    shebang="/usr/bin/env python3",
)
Copy code
```
If I got it right from documentation I should be able to set the path relative from the source root. In my case I have the `api/server.py` where I’m launching a uvicorn server with the following command:
```if __name__ == "__main__":
    uvicorn.run(
        get_app(),
        host="0.0.0.0",
        port=8000,
        log_config="config.json",
    )
What is the source root in that case? Currently I always get the error
FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
. Thanks in advance ☀️
1
b
You'd (assuming your source root is
src/python
) use
shared/logging/config.json
.
Or, wait maybe not, since that's all CWD specific and this is embedded. You'd want to make it relative to
__file__
Like
f"{__file__}/../../shared/logging/config.json
(or whatever the right path is)
b
Thanks for your quick repsonse. Unfortunately this doesn’t work. Now I get
NotADirectoryError: [Errno 20] Not a directory: '…/.pants.d/tmpi03th7ds/src/python/accounts/api/server.py/../../shared/logging/config.json'
May would it be better to relocate the config file in that case?
b
Ah
pathlib
I suppose is our friend
str(pathlib.PurePath(__file__, "../../shared/logging/config.json").resolve())
A couple notes though, are you planning on packaging and distributing the PEX, or are you just trying to run a script locally? If its the latter, Pants 2.13 will let you run using files in-repo (and not a sandbox)
b
So my plan is to package and run the pex_binary in a docker container.
b
Yeah you'll wanna load by path then 🙂
b
Mhhh, like this snippet?
str(pathlib.PurePath(__file__, "../../shared/logging/config.json").resolve())
b
Yeah thats one way 🙂
b
Nice, thanks 🙂
Using the run command it is working already! ❤️ Now I’m trying to run the server in a docker container. 🤞
b
❤️
b
Works 🙂 Thank you very much!
Saved me a lot of time :-)
b
Pass it on ❤️
(also really dope profile pic)
b
Hehe thanks 😁