Hey Everyone! I have developed a python app using ...
# general
f
Hey Everyone! I have developed a python app using streamlit and am trying to run it using pants. I keep hitting the error
The file or directory 'app.py' does not exist on disk in the workspace, so the address 'app.py' from the dependencies field from the target src/py/olx/apps/MolStandardizerWebApp:dev cannot be resolved.
When trying to run
pants package src/py/olx/apps/MolStandardizerWebApp:dev
This is what the src/py/olx/apps/MolStandardizerWebApp/BUILD file looks like:
Copy code
python_sources()

py_app_visibility(name="MolStandardizerWebApp",)

pex_binary(
    name="dev",
    script="streamlit",
    args=["run", "app.py"],
    execution_mode="venv",
    dependencies = ["app.py",],
)
And my code is structured in the following way:
Copy code
src/py/olx/apps/MolStandardizerWebApp/
├── BUILD
├── __init__.py
├── app.py
├── evaluate_smiles.py
├── run_streamlit.py
├── tests
│   ├── BUILD
│   ├── __init__.py
│   ├── constants.py
│   └── test_evaluate_smiles.py
└── utils.py
The weird thing is when I run
pants list ::
at the root of my monorepo I can see that
src/py/olx/apps/MolStandardizerWebApp/app.py
is listed, so why isn't it discoverable in my workspace?
w
My guess is that you'd probably need to specify the target rather than the file e.g.
Copy code
python_sources(
 name="foo"
)

pex_binary(
  ...
  dependencies=[:foo]
f
oh thanks Ill try that now
still getting Invalid file error
Copy code
19:49:27.86 [INFO] Completed: Building 3 requirements for src.py.olx.apps.MolStandardizerWebApp/dev.pex from the 3rdparty/py/default.lock resolve: datamol, rdkit==2023.09.5, streamlit
19:49:27.86 [WARN] /home/codespace/.cache/pants/named_caches/pex_root/installed_wheels/8d43c1ed16877f3d2ed39e5ef3e92aa8885d4427cd7d9ce62683c922a5c0cd07/pex-2.3.1-py2.py3-none-any.whl/pex/bin/pex.py:947: PEXWarning: Could not calculate a targeted shebang for:
cp310-cp310-manylinux_2_31_x86_64 interpreter at /workspaces/olx-code/.venv/bin/python3
cp312-cp312-manylinux_2_31_x86_64 interpreter at /opt/conda/bin/python3.12

Using shebang: #!/usr/bin/env python3.10
If this is not appropriate, you can specify a custom shebang using the --python-shebang option.
  pex_warnings.warn(

Usage: dev.pex run [OPTIONS] TARGET [ARGS]...
Try 'dev.pex run --help' for help.

Error: Invalid value: File does not exist: app.py
and just for reference my build file was changed like this:
Copy code
python_sources(name="app.py")

py_app_visibility(name="MolStandardizerWebApp",)

pex_binary(
    name="dev",
    script="streamlit",
    args=["run", "app.py"],
    execution_mode="venv",
    dependencies = [":app.py"],
)
This version of the BUILD returns the same error too
Copy code
python_sources(name="app")

py_app_visibility(name="MolStandardizerWebApp",)

pex_binary(
    name="dev",
    script="streamlit",
    args=["run", "app.py"],
    execution_mode="venv",
    dependencies = [":app"],
)
w
So, there are a few problems - why it can't calculate a shebang is odd. I've never used streamlit - so I'm not entirely understanding what's happening here, but I assume pex would also need to know about it - from this code, it looks more like you're running a system executable on app.py
It feels like you'll want to do something closer (but not exactly like) how I run fastapi here? https://github.com/sureshjoshi/pants-plugins/blob/main/examples/python/hellofastapi/BUILD.pants
I -think- you want something like this? Keeping in mind, NO clue about streamlit
Copy code
# requirements.txt
streamlit
Copy code
# pants.toml
[GLOBAL]
pants_version = "2.21.1"

backend_packages = [
    "pants.backend.python",
]

[python]
enable_resolves = true
interpreter_constraints = ["==3.12.4"]
Copy code
# BUILD

python_requirements(
    name="reqs",
)

python_sources(
    name="lib"
)

pex_binary(
    name="bin",
    dependencies=[":lib", "//:reqs#streamlit"],
    script="streamlit",
    args=["run", "app.py"],
    include_tools=True,
)
Copy code
pants run //:bin 

 👋 Welcome to Streamlit!
f
Thanks for your insight on this @wide-midnight-78598 for extra context I'm basically trying to emulate user Jake Z development framework https://pantsbuild.slack.com/archives/C046T6T9U/p1714415386771459 where I want to use a 3rd party library (streamlit) to run my first party code (app.py) and the reason pants comes into play is that pants is managing all the imports for this scripts and will be used to generate the production docker img of my app so I want the dev environment to be as close to that as possible
Can you explain more about the python_requirements component? should all the 3rd party requirements be inferable by pants since my app.py imports them from src?
Also what is the logic behind the inclusion of sources in your python_sources?
w
The docs will do a better job of explaining those: https://www.pantsbuild.org/2.21/docs/python/overview/third-party-dependencies python_sources will treat every python file in that folder as a target, so you can run it directly, lint, fmt, whatever - and also, I believe dep inference between files and 3rd party reqs won't kick off unless that source file is in a target. But Im not positive
👀 1