The docs <Passing arguments> section for the `run`...
# general
f
The docs Passing arguments section for the
run
goal mentions running an executable like this:
Copy code
$ ./pants run project/app.py
I am confused — if there is a file
app.py
in the
project
directory, one wouldn’t be able to run it with the command above.
Copy code
Exception message: 1 Exception encountered:

  NoApplicableTargetsException: No applicable files or targets matched. The `run` goal works with these target types:

  * pex_binary

However, you only specified files with these target types:

  * python_library
If I have a
pex_binary
with the name
app.py
, then the
pex_binary
target name would be
project:app.py
. Is the documentation slightly misleading or am I missing something obvious?
c
It does work, if you have a
pex_binary
that uses `app.py`:
Copy code
$ ./pants run src/app.py
Loading app.py...
Hello from app.py
with this simple repro:
Copy code
$ git diff --staged
diff --git a/pants.toml b/pants.toml
new file mode 100644
index 0000000..d17ff58
--- /dev/null
+++ b/pants.toml
@@ -0,0 +1,6 @@
+[GLOBAL]
+pants_version = "2.5.1"
+
+backend_packages = [
+  'pants.backend.python'
+]
diff --git a/src/BUILD b/src/BUILD
new file mode 100644
index 0000000..2bdbb8a
--- /dev/null
+++ b/src/BUILD
@@ -0,0 +1,6 @@
+python_library()
+
+pex_binary(
+    name="cli",
+    entry_point="app.py:hello"
+)
diff --git a/src/app.py b/src/app.py
new file mode 100644
index 0000000..73b5b7f
--- /dev/null
+++ b/src/app.py
@@ -0,0 +1,9 @@
+
+print("Loading app.py...")
+
+def hello():
+    print("Hello from app.py")
+
+
+if __name__ == "__main__":
+    hello()
👍 1
✔️ 1
I guess the docs could be clearer, with an example BUILD file showing how to tie it all together..
👍 1
2
h
Yeah, it's why we recommend using the entry point as a file name, rather than module name But I see how that's confusing because it does depend on how you set things up
f
@hundreds-father-404 wow I had no idea you could do a file name
entry_point="app.py:hello"
— I haven’t seen a single simple example like this online! So your recommendation is to use the file name in the entry point, right? Is it specifically to allow
./pants run project/app.py
commands? FWIW, having
Copy code
pex_binary(
  name="custom-pex-name",
  entry_point='project/app.py:main',
)
lets one do both:
Copy code
$ ./pants run project:custom-pex-name
$ ./pants run project/app.py
h
Hm, have you been seeing examples that don't use file name? I tried to audit everywhere to update to use it because it's preferred, but may have missed some places See https://www.pantsbuild.org/docs/python-package-goal#the-entry_point-field for more
Is it specifically to allow ./pants run project/app.py commands?
Yep, exactly! And less boilerplate than
a_long.path.to.my.module:func
🙏 1
f
have you been seeing examples that don’t use file name?
I mean online on Python related resources, not Pants docs 🙂 E.g. https://click.palletsprojects.com/en/8.0.x/setuptools/
Copy code
entry_points={
        'console_scripts': [
            'yourscript = yourpackage.scripts.yourscript:cli',
        ],
    },
)
And less boilerplate than 
a_long.path.to.my.module:func
Wouldn’t you need to provide the full path to the file (in case it’s some nested directory)? So the difference would be to use a
/
instead of
.
? I guess I am missing something important here, sorry 😞
reading the docs page you’ve shared now, I’ve totally missed this, hugely useful
oh I understand now why I haven’t seen the file path examples online — because standard
setup.py
Python expects a module, but Pants does the conversion. And the path is relative to the
BUILD
, it all makes sense now
h
Yep, exactly on Pants doing the conversion :)