fresh-cat-90827
07/28/2021, 9:45 AMrun
goal mentions running an executable like this:
$ ./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.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?curved-television-6568
07/28/2021, 9:57 AMpex_binary
that uses `app.py`:
$ ./pants run src/app.py
Loading app.py...
Hello from app.py
with this simple repro:
$ 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()
hundreds-father-404
07/28/2021, 1:43 PMfresh-cat-90827
07/28/2021, 8:24 PMentry_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
pex_binary(
name="custom-pex-name",
entry_point='project/app.py:main',
)
lets one do both:
$ ./pants run project:custom-pex-name
$ ./pants run project/app.py
hundreds-father-404
07/28/2021, 8:26 PMIs it specifically to allow ./pants run project/app.py commands?Yep, exactly! And less boilerplate than
a_long.path.to.my.module:func
fresh-cat-90827
07/28/2021, 8:30 PMhave 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/
entry_points={
'console_scripts': [
'yourscript = yourpackage.scripts.yourscript:cli',
],
},
)
And less boilerplate thanWouldn’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 aa_long.path.to.my.module:func
/
instead of .
? I guess I am missing something important here, sorry 😞setup.py
Python expects a module, but Pants does the conversion. And the path is relative to the BUILD
, it all makes sense nowhundreds-father-404
07/28/2021, 9:03 PM