The Wayback Machine - https://web.archive.org/web/20210104141821/https://github.com/bazelbuild/rules_python/issues/318
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a Python Package with 'console_scripts' #318

Open
amit-traiana opened this issue May 26, 2020 · 5 comments
Open

Using a Python Package with 'console_scripts' #318

amit-traiana opened this issue May 26, 2020 · 5 comments

Comments

@amit-traiana
Copy link

@amit-traiana amit-traiana commented May 26, 2020

Hi,

I have a Python package that generate console script as entry-point. The setup.py looks like so:

entry_points={"console_scripts": ["my_package" = "my_package.cli:main"]}

I would like to call this python command line script from within a gen_rule, but unable to do it just by calling my_package directly. What I currently do, is to install the package with the requirements file, and create a py_binary like so:

    native.py_binary(
          name = "my_package",
          main = "__main__.py",
          srcs = [
              requirement("my_package"),
          ],
       deps = [
              requirement("pyyaml"),
       ],
    )

This works and I can consume the it from within gen_rule, but it feels a bit hacky - especially the part where I need to manually add deps and not use the deps defined in setup.py.

Is there's a better way doing it perhaps?

@amitz
Copy link

@amitz amitz commented Aug 18, 2020

Seemed to be solved. I'm able to call console script directly from gen_rule (make sure you have the symbolic link in your path).

@thundergolfer
Copy link
Collaborator

@thundergolfer thundergolfer commented Aug 31, 2020

@amitz Are you able to provide an example, and more detail (particularly about "make sure you have the symbolic link...")?

@amitz
Copy link

@amitz amitz commented Aug 31, 2020

Yes, of-course. I'm using pip_import to install my package using requirements.txt file:

my_script==1.0

I then wrap it using a Bazel macro:

def my_macro(name, **kwargs):
    native.genrule(
        name = name,
        srcs = [":infile.xml"],
        outs = ["outfile.xml"],
        cmd = "my_script --infile=$(location :infile.yaml) --outfile=$@",
    )

The my_script "binary" (Bash script calling the Python code) will be installed somewhere. What I missed is that multiple Docker containers will have different places to store that "binary". On some container It was /usr/local/bin on other /usr/.local/bin etc. So instead - I'm running Bazel in the context of a Virtual env - so the "binary" is always installed on the same spot. Make sure this spot is on your PATH.

@pstradomski
Copy link
Collaborator

@pstradomski pstradomski commented Aug 31, 2020

@amitz
Copy link

@amitz amitz commented Aug 31, 2020

Your right - It's also installed it on the outer virtualenv! In that case, seems like there no way to do that properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.