I’m not super clear on how the filesystem interact...
# general
s
I’m not super clear on how the filesystem interacts with
pex_binary
for Python targets. When I run a
python_test
, the only files that seem to be visible in the filesystem at runtime are those that I include with
files
or similar, but I can see everything when I’m running a Python binary. Is this the expected behavior? I’m not able to get
files
or
relocate_files
to work here, are there docs about this somewhere I missed? Specific use case is pointing Flask at a template/static folder, which I’d like to have visible from the root if possible. Other workarounds appear to be passing in a source file path to the executable itself but that complicates the open source release of this code.
Flask in general actually behaves weirdly, for example hot reloading doesn't work for me, it seems that there's some module issue which must happen because the hot reloader doesn't use the same installation as the main executable.
Hey, just wanted to resurface this. I still can’t figure out how to get Flask working without having to explicitly specify a root directory. I’d like to relocate the files if possible during runtime.
b
We include our templates and similar as
resources
rather than
files
, and that seems to work okay
Copy code
resources(name="templates", sources=["templates/*"])
python_sources(dependencies=[":templates"])
https://www.pantsbuild.org/docs/assets was what I read to get this going, but I don't remember the details
s
oh that looks reasonable, I’ll look into this, thank you for the tip!
👍 1
h
Hey, to be sure I understand the issue, are you asking about running the flask development server with
./pants run
?
There is a similar issue with the Django development server, which I am more familiar with: When you
./pants run
, the cwd is the repo root, so your code can see the real source files. But those aren't what it's running from. Instead it's running from sandboxed sources, so the native hot reloading doesn't work.
As the sandboxed sources aren't modified when you edit in the repo.
However, Pants itself has file watching and invalidation logic. So if you set
restartable = True
on the
pex_binary
, then
./pants run
will restart automatically when files change.
And this is in fact better than at least the Django hot reloader, because if you set up your dependencies correctly then it can reload on things like css changes, template changes, requirements changes, codegen changes, anything that would invalidate the build, even things that the framework's hot reloader doesn't know about.
If you get a module not found error when running the development server with hot reloading on then that is because that reloader mechanism doesn't play nicely with pex. But letting Pants handle the reloading solves all that.
Regarding loading templates from your static folder, if Flask loads them using pkgutil-style loading, then you probably want to use
resources
targets
As @broad-processor-92400 suggested
Unfortunately I don't know much about Flask, but to the extent that it's similar in spirit to Django, then we do have solutions 🙂
In answer to the first bit of your question: when you run a test then the cwd is the sandbox. When you
./pants run
, the code is loaded from the sandbox, but the cwd is the repo root. So what you're seeing is expected. But note that the files in the repo root are not the ones being "used" by run.
The repo root being the cwd makes it easier to run binaries that do stuff in the repo, such as write files into it.
s
Ooooh interesting on the reloader, that would make sense and probably work great
The issue with the file locations is that the pex binary doesn’t sandbox the files (it appears)
The reason I’m kinda stuck there is that the flask server is located deeper in the monorepo, but the flask repo will be released to github as an open source project. This means references to the greater monorepo are not ideal. One option would be to cut releases with logic that removes references to the broader repo.
h
Hmm, you don't see those files in the sandbox directory if you run with
--no-process-cleanup
?
s
they’re available at
src/path/to/package/templates
but I was hoping to use
relocated_files
in order to move them to the root directory since the open source release shouldn’t/cannot reference
src/path/to/
if that makes sense
h
I think that would work? Or you can use
resources
targets, which strip the source root (assuming
src/path/to
is a source root?)
s
Does relocated_files work with pex binaries?
h
Oh, now that I think of it,
files
in general don't get embedded into .pex files, you have to use
resources
, it's a bit of a mess. @bitter-ability-32190 can comment in detail, because I'm not sure how that affects
./pants run
on a
pex_binary
target.
👀 1
b
If I look at the source, we use
include_files=True
so files should be present in the sandbox
h
But not if we
package
into a pex file, right?
b
Yup