TimWhiting / dartpy

An experiment in embedding Python in Dart via dart ffi and the Python c-api
68 stars 10 forks source link

code generation not works, the generated code is not generate. #9

Closed liudonghua123 closed 8 months ago

liudonghua123 commented 1 year ago

I tried the demo code on the readme, but when I execute dart run build_runner build, the gen_dartpy_example.g.dart file is not generated.

import 'package:dartpy/dartpy.dart';
import 'package:dartpy/dartpy_annotations.dart';
part 'gen_dartpy_example.g.dart';

@PyFunction(module: 'multiply')
int multiply(int a, int b) => pymultiply(a, b);

@PyFunction(module: 'multiply', name: 'multiply')
double mult_double(double a, double b) => pymultiplydouble(a, b);

@PyFunction(module: 'multiply', name: 'multiply')
num mult_num(num a, num b) => pymultiplynum(a, b);

void main() {
  try {
    print(multiply(6, 4));
    print(mult_double(6.13, 5.2));
    print(mult_num(6, 5.2));
    print(mult_num(6, 2));
  } on DartPyException catch (e) {
    if (pyErrOccurred()) {
      dartpyc.PyErr_Print();
    }
    print(e.message);
  }
}

image

image

logs ```shell D:\code\dart\dartpy_demo1>dart --version Dart SDK version: 2.17.5 (stable) (Tue Jun 21 11:05:10 2022 +0200) on "windows_x64" D:\code\dart\dartpy_demo1> D:\code\dart>dart create dartpy_demo1 Creating dartpy_demo1 using template console... .gitignore analysis_options.yaml CHANGELOG.md pubspec.yaml README.md bin\dartpy_demo1.dart lib\dartpy_demo1.dart test\dartpy_demo1_test.dart Running pub get... Resolving dependencies... Changed 46 dependencies! Created project dartpy_demo1 in dartpy_demo1! In order to get started, run the following commands: cd dartpy_demo1 dart run D:\code\dart>cd dartpy_demo1\ D:\code\dart\dartpy_demo1>dart pub add dartpy Resolving dependencies... + dartpy 0.0.1+4 + ffi 1.2.1 (2.0.1 available) + freezed_annotation 0.14.3 (2.0.3 available) + json_annotation 4.5.0 Downloading dartpy 0.0.1+4... Changed 4 dependencies! D:\code\dart\dartpy_demo1>dart pub add -d build_runner Resolving dependencies... + build 2.3.0 + build_config 1.0.0 + build_daemon 3.1.0 + build_resolvers 2.0.9 + build_runner 2.1.11 + build_runner_core 7.2.3 + built_collection 5.1.1 + built_value 8.3.3 + checked_yaml 2.0.1 + code_builder 4.1.0 + dart_style 2.2.3 ffi 1.2.1 (2.0.1 available) + fixnum 1.0.1 freezed_annotation 0.14.3 (2.0.3 available) + graphs 2.1.0 + pubspec_parse 1.2.0 + stream_transform 2.0.0 + timing 1.0.0 Downloading build_resolvers 2.0.9... Downloading built_value 8.3.3... Changed 16 dependencies! D:\code\dart\dartpy_demo1>cat pubspec.yaml name: dartpy_demo1 description: A sample command-line application. version: 1.0.0 # homepage: https://www.example.com environment: sdk: '>=2.17.5 <3.0.0' # dependencies: # path: ^1.8.0 dev_dependencies: build_runner: ^2.1.11 lints: ^2.0.0 test: ^1.16.0 dependencies: {dartpy: ^0.0.1+4} D:\code\dart\dartpy_demo1> # Make some modifications on the example code! D:\code\dart\dartpy_demo1>cat lib\dartpy_demo1.dart import 'package:dartpy/dartpy.dart'; import 'package:dartpy/dartpy_annotations.dart'; part 'dartpy_demo1.g.dart'; @PyFunction(module: 'multiply') int multiply(int a, int b) => pymultiply(a, b); @PyFunction(module: 'multiply', name: 'multiply') double mult_double(double a, double b) => pymultiplydouble(a, b); @PyFunction(module: 'multiply', name: 'multiply') num mult_num(num a, num b) => pymultiplynum(a, b); D:\code\dart\dartpy_demo1>cat lib\gen_dartpy_example.dart import 'package:dartpy/dartpy.dart'; import 'package:dartpy/dartpy_annotations.dart'; part 'gen_dartpy_example.g.dart'; @PyFunction(module: 'multiply') int multiply(int a, int b) => pymultiply(a, b); @PyFunction(module: 'multiply', name: 'multiply') double mult_double(double a, double b) => pymultiplydouble(a, b); @PyFunction(module: 'multiply', name: 'multiply') num mult_num(num a, num b) => pymultiplynum(a, b); void main() { try { print(multiply(6, 4)); print(mult_double(6.13, 5.2)); print(mult_num(6, 5.2)); print(mult_num(6, 2)); } on DartPyException catch (e) { if (pyErrOccurred()) { dartpyc.PyErr_Print(); } print(e.message); } } D:\code\dart\dartpy_demo1>cat bin\dartpy_demo1.dart import 'package:dartpy/dartpy.dart'; import 'package:dartpy_demo1/dartpy_demo1.dart'; void main() { try { print(multiply(6, 4)); print(mult_double(6.13, 5.2)); print(mult_num(6, 5.2)); print(mult_num(6, 2)); } on DartPyException catch (e) { if (pyErrOccurred()) { dartpyc.PyErr_Print(); } print(e.message); } } D:\code\dart\dartpy_demo1>dart run build_runner build [INFO] Generating build script completed, took 324ms [SEVERE] Nothing can be built, yet a build was requested. [INFO] Reading cached asset graph completed, took 45ms [INFO] Checking for updates since last build completed, took 498ms [INFO] Running build completed, took 4ms [INFO] Caching finalized dependency graph completed, took 28ms [INFO] Succeeded after 45ms with 0 outputs (0 actions) D:\code\dart\dartpy_demo1>dart run Building package executable... Failed to build dartpy_demo1:dartpy_demo1: lib/dartpy_demo1.dart:3:6: Error: Error when reading 'lib/dartpy_demo1.g.dart': The system cannot find the file specified. part 'dartpy_demo1.g.dart'; ^ lib/dartpy_demo1.dart:3:6: Error: Can't use 'lib/dartpy_demo1.g.dart' as a part, because it has no 'part of' declaration. part 'dartpy_demo1.g.dart'; ^ lib/dartpy_demo1.dart:6:31: Error: Method not found: 'pymultiply'. int multiply(int a, int b) => pymultiply(a, b); ^^^^^^^^^^ lib/dartpy_demo1.dart:9:43: Error: Method not found: 'pymultiplydouble'. double mult_double(double a, double b) => pymultiplydouble(a, b); ^^^^^^^^^^^^^^^^ lib/dartpy_demo1.dart:12:31: Error: Method not found: 'pymultiplynum'. num mult_num(num a, num b) => pymultiplynum(a, b); ^^^^^^^^^^^^^ Failed to build dartpy_demo1:dartpy_demo1: lib/dartpy_demo1.dart:3:6: Error: Error when reading 'lib/dartpy_demo1.g.dart': The system cannot find the file specified. part 'dartpy_demo1.g.dart'; ^ lib/dartpy_demo1.dart:3:6: Error: Can't use 'lib/dartpy_demo1.g.dart' as a part, because it has no 'part of' declaration. part 'dartpy_demo1.g.dart'; ^ lib/dartpy_demo1.dart:6:31: Error: Method not found: 'pymultiply'. int multiply(int a, int b) => pymultiply(a, b); ^^^^^^^^^^ lib/dartpy_demo1.dart:9:43: Error: Method not found: 'pymultiplydouble'. double mult_double(double a, double b) => pymultiplydouble(a, b); ^^^^^^^^^^^^^^^^ lib/dartpy_demo1.dart:12:31: Error: Method not found: 'pymultiplynum'. num mult_num(num a, num b) => pymultiplynum(a, b); ^^^^^^^^^^^^^ D:\code\dart\dartpy_demo1> ```
liudonghua123 commented 1 year ago

I walk through the example code on dartpy\gen_dartpy\example. It seems missing gen_dartpy as dev_dependencies . Maybe the readme should update to provide more information how to use code generation.

Since gen_dartpy is not published on pub.dev, I have to use dependency_overrides to map gen_dartpy to the local package. After I updated the pubspec.yaml with the following contents, dart run build_runner build now works as expected!

name: dartpy_demo1
description: A sample command-line application.
version: 1.0.0
# homepage: https://www.example.com

environment:
  sdk: '>=2.17.5 <3.0.0'

# dependencies:
#   path: ^1.8.0

dev_dependencies:
  build_runner: ^2.1.11
  lints: ^2.0.0
  test: ^1.16.0
  gen_dartpy: any
dependencies:
  dartpy: ^0.0.1+4

dependency_overrides:
  gen_dartpy:
    path: ../dartpy/gen_dartpy
D:\code\dart\dartpy\gen_dartpy\example>rm -rf lib\gen_dartpy_example.g.dart

D:\code\dart\dartpy\gen_dartpy\example>dart run build_runner build
Building package executable...
Built build_runner:build_runner.
[INFO] Generating build script completed, took 428ms
[INFO] Precompiling build script... completed, took 7.3s
[INFO] Building new asset graph completed, took 850ms
[INFO] Checking for unexpected pre-existing outputs. completed, took 1ms
[INFO] Generating SDK summary completed, took 5.7s
[INFO] Running build completed, took 7.6s
[INFO] Caching finalized dependency graph completed, took 25ms
[INFO] Succeeded after 7.7s with 2 outputs (3 actions)

D:\code\dart\dartpy\gen_dartpy\example>dart lib\gen_dartpy_example.dart
Unhandled exception:
UnimplementedError: Window python version not found, searched for Python 3.8 and 3.9, set pyLibLocation for custom install location
#0      _findWindows (package:dartpy/src/ffi/ffi.dart:56:3)
#1      _pyLib (package:dartpy/src/ffi/ffi.dart:19:43)
#2      _pyLib (package:dartpy/src/ffi/ffi.dart)
#3      dartpyc (package:dartpy/src/ffi/ffi.dart:63:56)
#4      pyInitialized (package:dartpy/src/helpers/bool_functions.dart:23:27)
#5      _ensureInitialized (package:dartpy/src/helpers/helpers.dart:30:8)
#6      pyImport (package:dartpy/src/helpers/helpers.dart:57:3)
#7      pymultiply (package:gendartpy_example/gen_dartpy_example.g.dart:11:20)
#8      multiply (package:gendartpy_example/gen_dartpy_example.dart:6:31)
#9      main (package:gendartpy_example/gen_dartpy_example.dart:16:11)
#10     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

D:\code\dart\dartpy\gen_dartpy\example>git diff lib\gen_dartpy_example.dart
diff --git a/gen_dartpy/example/lib/gen_dartpy_example.dart b/gen_dartpy/example/lib/gen_dartpy_example.dart
index 7fee9e2..ee2d2d1 100644
--- a/gen_dartpy/example/lib/gen_dartpy_example.dart
+++ b/gen_dartpy/example/lib/gen_dartpy_example.dart
@@ -12,6 +12,8 @@ double mult_double(double a, double b) => pymultiplydouble(a, b);
 num mult_num(num a, num b) => pymultiplynum(a, b);

 void main() {
+  pyLibLocation =
+      'C:/Users/Liu.D.H/AppData/Local/Programs/Python/Python310/Python310.dll';
   try {
     print(multiply(6, 4));
     print(mult_double(6.13, 5.2));

D:\code\dart\dartpy\gen_dartpy\example>
D:\code\dart\dartpy\gen_dartpy\example>dart lib\gen_dartpy_example.dart
ModuleNotFoundError: No module named 'multiply'
Importing python module multiply failed, make sure the multiply is on your PYTHONPATH
 eg. export PYTHONPATH=$PYTHONPATH:/path/to/multiply

D:\code\dart\dartpy\gen_dartpy\example>set PYTHONPATH=lib

D:\code\dart\dartpy\gen_dartpy\example>dart lib\gen_dartpy_example.dart
Will compute 6 times 4
24
Will compute 6.13 times 5.2
31.876
Will compute 6 times 5.2
TypeError: 'float' object cannot be interpreted as an integer
Error in converting to a dart num

D:\code\dart\dartpy\gen_dartpy\example>
liudonghua123 commented 1 year ago

I forgot the dependencies could use git-packages. So the pubspec.yaml could rewrite to the following.

name: dartpy_demo1
description: A sample command-line application.
version: 1.0.0
# homepage: https://www.example.com

environment:
  sdk: '>=2.17.5 <3.0.0'

# dependencies:
#   path: ^1.8.0

dev_dependencies:
  build_runner: ^2.1.11
  lints: ^2.0.0
  test: ^1.16.0
  gen_dartpy: any
dependencies:
  dartpy: ^0.0.1+4

dependency_overrides:
  gen_dartpy:
    git:
      url: git@github.com:TimWhiting/dartpy.git
      path: gen_dartpy
TimWhiting commented 8 months ago

By the way, I just published gen_dartpy. I know it probably doesn't support a whole lot now, but we can slowly add to it. Happy to accept contributions if you've found it useful.