Poetry2nix | 323 Members | |
| https://github.com/nix-community/poetry2nix | 61 Servers |
| Sender | Message | Time |
|---|---|---|
| 16 Feb 2022 | ||
| And this is a Python thing, not really a Nix thing | 20:24:24 | |
In reply to @k900:0upti.meIt is in site-packages, if you look at the second command I put | 20:26:00 | |
| Oh, I see | 20:26:44 | |
| Your path is wrong for two reasons | 20:26:52 | |
| 1) you're looking one folder above the current directory, when the file is in the same directory as your Python code | 20:27:12 | |
And 2) open works relative to current working directory, not source file location | 20:27:35 | |
| The actually correct way to do this would be https://docs.python.org/3/library/importlib.html#module-importlib.resources | 20:29:08 | |
| This is what I thought! But
| 20:29:24 | |
| Of course I admit my thinking could be wrong here but I couldn't argue with the results | 20:29:37 | |
As poetry run lbob and the Nix derivation both run successfully | 20:29:49 | |
| So you fixed issue #1, kind of | 20:31:13 | |
| But you still have issue #2 | 20:31:19 | |
The easy way out would be to do something like pathlib.Path(__file__) / "../data.json" | 20:31:50 | |
| The correct way out would be to use importlib.resources | 20:32:05 | |
| Interesting. Can you give me an example of how I would use importlib.resources in this case? | 20:32:40 | |
Basically just from importlib import resources; resources.files("your_package_name").join("data.json").read_text() | 20:34:19 | |
From inside my data.py file right? It would replace the open() | 20:35:20 | |
| It would replace the whole thing | 20:35:52 | |
| This is my first real Python project, sorry if my questions are very amateur | 20:36:03 | |
You probably want to then pass the result to json.loads | 20:36:04 | |
| Also, you might want to move that out of the function | 20:36:43 | |
| Because importlib will be slower than just reading the file off the disk | 20:37:00 | |
| Probably not by enough for it to mattrr | 20:37:10 | |
| * Probably not by enough for it to matter | 20:37:13 | |
| But it's generally good practice to load resources once and then keep them in memory | 20:37:26 | |
| Because your project could be installed as a wheel, or as an egg, or as whatever other weird format whatever other weird Python implementation uses | 20:38:02 | |
| Wow, this is really helpful, thank you for taking the time with me! I will try and implement your suggestions and see if I can marshal it into some form of life | 20:39:54 | |
| * Wow, this is really helpful, thank you for taking the time with me! I will try and implement your suggestions and see if I can marshal my application into some form of life | 20:43:18 | |
| 22:08:54 | ||
| 17 Feb 2022 | ||
| I've changed the code of
which again works with
| 07:39:15 | |