TimWhiting / dartpy

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

Path issue in Macos #7

Open rohitsangwan01 opened 2 years ago

rohitsangwan01 commented 2 years ago

Hey great package , but am not able to run this project , getting Path issues

[ERROR:flutter[/lib/ui/ui_dart_state.cc]()(209)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library '[/Users/rohitsangwan/Drive/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib]()': dlopen([/Users/rohitsangwan/Drive/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib](), 0x0001): tried: '[/Users/rohitsangwan/Drive/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib]()' (file system sandbox blocked open()), '[/usr/local/lib/libpython3.9.dylib]()' (no such file), '[/usr/lib/libpython3.9.dylib]()' (no such file)

this error

herer is my Code

void main() {
  pyLibLocation =
      '/Users/rohitsangwan/Drive/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib';

  dartpyc.Py_Initialize();
  const python = '''
  print("Hello, world!")

    ''';
  final pystring = python.toNativeUtf8();
  dartpyc.PyRun_SimpleString(pystring.cast<Int8>());
  malloc.free(pystring);
  print(dartpyc.Py_FinalizeEx());

  runApp(
    GetMaterialApp(
      title: "Application",
      initialRoute: AppPages.INITIAL,
      getPages: AppPages.routes,
    ),
  );
}
TimWhiting commented 2 years ago

I don't know if I can help with this, seeing as it is a problem that that location does not exist on your machine. Why is python in your Drive folder? That seems weird.

rohitsangwan01 commented 2 years ago

@TimWhiting that location because i copied it there , initially it was in pyenv directory , but i was getting same error there as well , and path is correct Btw can we do something like Copy the whole python environment files in Flutter projects itself , and hardcode paths of that in project

TimWhiting commented 2 years ago

Does the default lookup not work? (without setting pyLibLocation?). I wouldn't typically recommend using a non-standard install location.

You could try making the lookup system more robust to different typical python installation paths. pyLibLocation is only provided if you want to manually set it to a non-standard install location. For most use cases I would assume that you would expect the environment to be set up a certain way and catch exceptions if it is not set up correctly with some sort of notification or message saying how to set it up appropriately.

I've only ever run this in pure dart for a command line app. I've never tried embedding this into a Flutter app with the python code bundled in it. It is probably feasible, but I don't have the time to figure out how to do it and document it. If you figure it out, I'd happy to accept some documentation and publish a new version. You might be running into permissions errors if you are trying to do it in a flutter app, I don't know that MacOS is happy about letting any app dynamically link to a random dynamic library. You'll have to turn off sandboxing probably (just google it).

In general this library is a lightly maintained experiment & light wrapper around the CPython ffi bindings & while I'm open to accepting contributions and making this better, I don't have the time to really make it what I wanted it to be. I'll answer any questions you have in these issues if I think I can help though.

rohitsangwan01 commented 2 years ago

ohhh i see ,i thaught its ready to use for flutter App , will try and let you know if got any success in it , just wanted to use some python packages on Flutter platforms

rohitsangwan01 commented 2 years ago

@TimWhiting path wokring fine on Windows , btw how can use your annotation and code generation library , is it on Pub.dev ?

TimWhiting commented 2 years ago

No, it is not on pub.dev, because I didn't feel like it was high enough quality. It works for simple things like numbers (int, double, bool, void), but not strings / tuples / objects. It would probably be somewhat straightforward to add those things, but again, I don't have the time right now, but am definitely open to reviewing any pull-requests.

You can depend on it with a git dependency:

dependencies:
  gen_dartpy:
    git:
      url: git@github.com:TimWhiting/dartpy
      path: gen_dartpy
rohitsangwan01 commented 2 years ago

@TimWhiting Great , thanks

bcichowlas commented 8 months ago

FWIW, a year later, this seemed to work on the current MacOS: pyLibLocation = '/usr/bin/python3';

I did have to change the capitalization on py_Initialize, I think, and stopped when I had trouble with this one: Invalid argument(s): Failed to lookup symbol 'Py_SetProgramName': dlsym(0x7ff90dd3ddf0, Py_SetProgramName): symbol not found

I'd still very much like to get it to work, though, and may try again a little later.

TimWhiting commented 8 months ago

It looks like you potentially are using an incompatible version of python. It should be relatively straightforward to update.