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

can't run my program executable (console_script) via mkPoetryApplication #159

Closed
teto opened this issue Aug 26, 2020 · 11 comments
Closed

can't run my program executable (console_script) via mkPoetryApplication #159

teto opened this issue Aug 26, 2020 · 11 comments

Comments

@teto
Copy link
Contributor

teto commented Aug 26, 2020

I am trying to convert my project to poetry, it's a cli interactive application. I was annoyed by python-poetry/poetry#1279 but could fix it by committing the generated poetry setup.py file.

Now once I am in a nix-shell (poetry2nix.mkPoetryApplication {), I can't run my software (executable is called mptcpanalyzer) because of:

Traceback (most recent call last):
  File "/run/user/1000/tmp.fnf8TG4JP9/bin/mptcpanalyzer", line 33, in <module>
    sys.exit(load_entry_point('mptcpanalyzer', 'console_scripts', 'mptcpanalyzer')())
  File "/run/user/1000/tmp.fnf8TG4JP9/bin/mptcpanalyzer", line 22, in importlib_load_entry_point
    for entry_point in distribution(dist_name).entry_points
  File "/nix/store/fjgnz0xfl04hsblsi4ym5y5akfh6mlmy-python3-3.8.5/lib/python3.8/importlib/metadata.py", line 504, in distribution
    return Distribution.from_name(distribution_name)
  File "/nix/store/fjgnz0xfl04hsblsi4ym5y5akfh6mlmy-python3-3.8.5/lib/python3.8/importlib/metadata.py", line 177, in from_name
    raise PackageNotFoundError(name)
importlib.metadata.PackageNotFoundError: mptcpanalyzer

Not sure what's the error here, wether it's nix or poetry's fault.

If you wanna try/check, just run nix-shell on this code https://github.com/teto/mptcpanalyzer/pull/35
I am using nixpkgs c59ea8b8a0e7f927e7291c14ea6cd1bd3a16ff38

@takeda
Copy link
Contributor

takeda commented Aug 30, 2020

You are using nix-shell to execute it, but mkPoetryApplication is meant for building a final executable. E.g. you can place it in default.nix and build using nix-build.

There's mkPoetryEnv which from my understanding is meant for nix-shell. It is the equivalent of installing package in dev mode(pip -e .). Until recently it didn't support creating binaries from tool.poetry.scripts but that was just added. You might still run into this issue if you make use of pkg_resources (although since it is your application you probably can replace it with importlib.metadata.

If you want to use nix to package application for regular use and not development, you should invoke mkPoetryApplication via nix build, and as far as I can tell that works correctly without any issues.

@teto
Copy link
Contributor Author

teto commented Aug 30, 2020

thanks for the help, but mkPoetryEnv doesnt even generate the mptcpanalyzer script so not sure it's the correct way (even when setting editablePackageSources).

@takeda
Copy link
Contributor

takeda commented Aug 31, 2020

How old is your poetry2nix? Because, this functionality was added very recently: #150

@teto
Copy link
Contributor Author

teto commented Aug 31, 2020

I am using the one from nixpkgs so maybe that's why: I doubt it has this change. With nix flakes, is there a way to override nixpkgs' poetry with this repo's ?

@takeda
Copy link
Contributor

takeda commented Aug 31, 2020

You can use overlays and poetry2nix makes it esier since it creates overlay.nix that you can just include and will overwrite version of poetry2nix and poetry itself.

I personally use niv to pin packages to specific version, I guess I won't need it once flakes are officially released.

But basically to set it up I do:

$ niv init
$ niv modify nixpkgs -b nixpkgs-unstable
$ niv add nix-community/poetry2nix

Then place a nix/default.nix file:

{ ... }@args:
let
  sources = import ./sources.nix;

  # remove overlays and config attributes from args
  clean_args = builtins.removeAttrs args [ "overlays" "config" ];
in import sources.nixpkgs {
  overlays = [
    # latest version of poetry and poetry2nix
    (import "${sources.poetry2nix}/overlay.nix")
    (self: super: {
      inherit sources;
      inherit (import sources.niv { pkgs = super; }) niv;
    })
  ];
  config = {};
} // clean_args

Now instead of import <nixpkgs> {} you do import ./nix {}. This will provide nixpkgs as usual, except it will be pinned. You can later use niv update to get latest versions.

Of course if you don't want to use niv, you can just provide the overlay, you can use builtins.fetchGit or fetchFromGitHub and then reference overlay.nix similar as it is done in default.nix.

@teto
Copy link
Contributor Author

teto commented Sep 2, 2020

arf I tried your suggestion and added poetry2nix as an overlay to nixpkgs to get the latest version, Yet I get the same issue as in my first post #159 (comment) :(

@teto
Copy link
Contributor Author

teto commented Nov 30, 2020

Maybe I misunderstood you. Do you mean I should add importlib-metadata as a dependency of my program for it to work ?
within my poetry nix-shell

>>> from importlib.metadata import *
>>> entry_points()

I can't find my package "mptcpanalyzer", which kinda similar to the first error, aka the current package being developed is not added to the list of importlib-metadata.

@teto
Copy link
Contributor Author

teto commented Dec 1, 2020

I added importlib-metadata and that fixed it \o/ I don't if it's the final fix, but it should at least be documented. Thanks for the help.

@teto teto closed this as completed Dec 1, 2020
@teto teto reopened this Dec 1, 2020
@teto
Copy link
Contributor Author

teto commented Dec 1, 2020

my bad I was still running my old version that relies on nixpkgs+setup.py

@teto
Copy link
Contributor Author

teto commented Dec 1, 2020

source "$( poetry env info --path )/bin/activate" seems to do the trick once in the nix-shell

@teto
Copy link
Contributor Author

teto commented Dec 2, 2020

I followed the advice in NixOS/nixpkgs#105593 and just tweaking PYTHONPATH works fine.

@teto teto closed this as completed Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants