Search code examples
pythondeploymentbuildbuild-processpants

Issue with pants build tool on a "Hello World" example


I was doing a "Hello World" exercise with Twitter's Pants build tool. After cloned the "pants" repo - source, I successfully configured pants on my local.

First, I created an nested dir in the repo as:

$ mkdir -p mark/python/project_test

Then, I created two files in that dir to specify my app and BUILD as:

$ touch mark/python/project_test/Hello_world.py
$ touch mark/python/project_test/BUILD

Hello_World.py:

print "Hello World!"

BUILD:

python_binary(name="myapp",
          source="Hello_world.py"
         )

It ran perfectly when i run it with ./pants like:

$ ./pants run mark/python/project_test:myapp
$ Hello World!

Then, I was trying to add dependencies by change the "Hello_world.py" as:

import utility
print "Hello World!", utility.user(), "!"

I also created the utility.py in the same dir as:

import os 

def user():
return os.environ['USER']

As I add dependencies to my original app, I also modified the BUILD as:

python_library(name="app-lib",
          source=globs("*py")
          )
python_binary(name="myapp",
          source="hello_world.py",
          dependencies=[pants(':app-lib')]
         )

However, when I called the ./pants with the same command, it returned error:

$ ./pants run mark/python/project_test:myapp
Exception caught: (<class 'pants.base.cmd_line_spec_parser.BadSpecError'>)

Exception message: name 'pants' is not defined
while executing BUILD file BuildFile(mark/python/project_test/BUILD,       
FileSystemProjectTree(/Users/mli/workspace/source))
Loading addresses from 'mark/python/project_test' failed.
when translating spec mark/python/project_test:myapp

There are currently three files on my dir:

$ ls mark/python/project_test
$ BUILD Hello_world.py utility.py

Why my app can't load the library from utility.py and what is the right way to arrange folder tree and BUILD files?

  • I am sort of new to build tool and would really appreciate if somebody could provide a bit context of using pants when answering the question. Thanks!!:)

Solution

  • I was able to make your project run with a few small adjustments. Your issues were:

    1. There used to be a pants() wrapper for pants shortcuts but it doesn't exist anymore. I think you have the syntax slightly wrong even if it did.
    2. You used source and sources interchangeably when they are in fact distinct.

    For number 2, it is perhaps a subtle distinction:

    • python_binary has one source - the entrypoint for the created binary.
    • python_library has sources - an arbitrary number of files to be imported into other projects.

    If you change your BUILD file to match the definition below, you should have success rerunning your invocation. Good luck!

      python_library(
        name='app-lib',
        sources=globs('*.py'),
      )
    
      python_binary(
        name="myapp",
        source="hello_world.py",
        dependencies=[':app-lib']
      )