srplab / starcore_for_flutter

middleware for flutter calling other script languages
140 stars 29 forks source link

how to import library for python #5

Open ua4ever opened 5 years ago

ua4ever commented 5 years ago

I want to install pip module and pyrogram module for my project in flutter

Mellak commented 5 years ago

Hello, I have the same issue, have you find the solution ?

godilite commented 4 years ago

Any solution yet?

danyaYatsik commented 4 years ago

@srplab , could u help us?

MariaNabil commented 4 years ago

Same issue , is there any solution ?

sahilcool-nsut commented 3 years ago

Still no solution?

srplab commented 3 years ago
  1. Install the package in the host environment using pip, for example "pip install markdown"
  2. Compress the entire directory of the package into a zip file, and put it in starfiles folder
  3. set python import path, for example,

    String assetsPath = await Starflut.getAssetsPath(); int Platform = await Starflut.getPlatform(); if( Platform == Starflut.ANDROID){ await Starflut.copyFileFromAssets("markdown.zip", "flutter_assets/starfiles","flutter_assets/starfiles"); } python.call("import", ["sys"]);

    StarObjectClass pythonSys = await python.getValue("sys"); StarObjectClass pythonPath = await pythonSys.getValue("path"); pythonPath.call("insert",[0,assetsPath+"/flutter_assets/starfiles/markdown.zip"]);

    python.call("import", ["markdown"]); StarObjectClass markdown = await python.getValue("markdown"); String v = await markdown.call("markdown",["## hell-world"]);

sahilcool-nsut commented 3 years ago

Copying your same code, i've having some issues. 1) First issue was related to some importlib_metadata module. So i thought i'll import it the sameway as we are doing the markdown import. 2) After that, i got some error regarding " No module named '_csv' ". I couldn't think of a potential fix for this, as this seems to be an inbuilt file which should have been installed with python, and can't be installed manually.

srplab commented 3 years ago

add _csv.cpython-39.so to the project

sahilcool-nsut commented 3 years ago

Where am i supposed to find this .so file? Will it be available in my system if im on Windows? I cant find any place on the internet to download this

srplab commented 3 years ago

https://github.com/srplab/starcore_for_android/raw/master/android.python.3.9.0.zip

发送自 Windows 10 版邮件https://go.microsoft.com/fwlink/?LinkId=550986应用


发件人: sahilcool-nsut notifications@github.com 发送时间: Sunday, January 24, 2021 8:03:26 PM 收件人: srplab/starcore_for_flutter starcore_for_flutter@noreply.github.com 抄送: srplab srplab.cn@hotmail.com; Mention mention@noreply.github.com 主题: Re: [srplab/starcore_for_flutter] how to import library for python (#5)

Where am i supposed to find this .so file? Will it be available in my system if im on Windows?

― You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/srplab/starcore_for_flutter/issues/5#issuecomment-766336659, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACXTPWIW4U65DFCMAZB76QLS3QEA5ANCNFSM4GMV3ANQ.

sahilcool-nsut commented 3 years ago

I appreciate your help, but im still facing problems in the installation. Now, it says it cant find the module 'math'. I think the problem is that it cant find the inbuilt cpython files to compile. If possible, can you provide a WORKING example, with appropriate starflut function calls, so that we can succesfully import external libraries?

感谢您的帮助,但我仍然在安装过程中遇到问题。 现在,它说找不到模块“数学”。 我认为问题是找不到内置的cpython文件进行编译。 如果可能,您是否可以提供一个带有适当starflut函数调用的WORKING示例,以便我们可以成功导入外部库?

srplab commented 3 years ago

The missing python module can be found in https://github.com/srplab/starcore_for_android/raw/master/android.python.3.9.0.zip. Add them to the project

Hong822 commented 3 years ago

@srplab: Hi I'm trying to import 'requests' packages but keep failing. What I did is,

  1. Install python3.9.0

  2. download 'requests' by pip install requests.

  3. Compress the downloaded package into a zip. (Attached) requests.zip

  4. Paste the file in starfiles folder

  5. I added the following code as you instructed. `String assetsPath = await Starflut.getAssetsPath(); int Platform = await Starflut.getPlatform(); if( Platform == Starflut.ANDROID){ await Starflut.copyFileFromAssets("requests.zip", "flutter_assets/starfiles","flutter_assets/starfiles"); }

    python.call("import", ["sys"]);

    StarObjectClass pythonSys = await python.getValue("sys"); StarObjectClass pythonPath = await pythonSys.getValue("path"); pythonPath.call("insert",[0,assetsPath+"/flutter_assets/starfiles/requests.zip"]); python.call("import", ["requests"]);`

  6. However I still face the error message below. I think Starflut still don't recognize the package.

I/flutter (13614): -1 4 ModuleNotFoundError: No module named 'requests' 0

Could you guide me how to solve this?

Thank you.

srplab commented 3 years ago
  1. compress request to zip with folder name
  2. requests depends on other python packages, and package them together, See attached file
  3. Add the native libraries used below to the project await Starflut.copyFileFromAssetsEx("_struct.cpython-39.so", LibraryPath,null,false); await Starflut.copyFileFromAssetsEx("binascii.cpython-39.so", LibraryPath,null,false); await Starflut.copyFileFromAssetsEx("math.cpython-39.so", LibraryPath,null,false); await Starflut.copyFileFromAssetsEx("_random.cpython-39.so", LibraryPath,null,false); await Starflut.copyFileFromAssetsEx("_sha512.cpython-39.so", LibraryPath,null,false); await Starflut.copyFileFromAssetsEx("_socket.cpython-39.so", LibraryPath,null,false); await Starflut.copyFileFromAssetsEx("_ssl.cpython-39.so", LibraryPath,null,false); await Starflut.copyFileFromAssetsEx("select.cpython-39.so", LibraryPath,null,false); await Starflut.copyFileFromAssetsEx("array.cpython-39.so", LibraryPath,null,false); await Starflut.copyFileFromAssetsEx("_hashlib.cpython-39.so", LibraryPath,null,false); await Starflut.copyFileFromAssetsEx("_blake2.cpython-39.so", LibraryPath,null,false);
  4. Before initializing python, set environment variables String docPath = await Starflut.getDocumentPath(); await Starflut.setEnv("TMPDIR",docPath);

    Currently need to modify StarflutPlugin.java as follows(In the next version of starflut, these codes will be merged): case "starcore_getPlatform" :{ result.success((int)0); } break; case "starcore_setEnv" :{ ArrayList plist = (ArrayList)call.arguments; starcore._SRPLock(); boolean Result = starcore._SetEnv((String)plist.get(0),(String)plist.get(1)); starcore._SRPUnLock(); result.success(Result); } break; case "starcore_getEnv" :{ starcore._SRPLock(); String Result = starcore._GetEnv((String)call.arguments); starcore._SRPUnLock(); result.success(Result); } break; case "starcore_init" :{

    发送自 Windows 10 版邮件https://go.microsoft.com/fwlink/?LinkId=550986应用


    发件人: Hong822 notifications@github.com 发送时间: Sunday, January 31, 2021 11:22:10 PM 收件人: srplab/starcore_for_flutter starcore_for_flutter@noreply.github.com 抄送: srplab srplab.cn@hotmail.com; Mention mention@noreply.github.com 主题: Re: [srplab/starcore_for_flutter] how to import library for python (#5)

    @srplabhttps://github.com/srplab: Hi I'm trying to import 'requests' packages but keep failing. What I did is,

    1. Install python3.9.0

    2. download 'requests' by pip install requests.

    3. Compress the downloaded package into a zip. (Attached) requests.ziphttps://github.com/srplab/starcore_for_flutter/files/5899981/requests.zip

    4. Paste the file in starfiles folder

    5. I added the following code as you instructed. `String assetsPath = await Starflut.getAssetsPath(); int Platform = await Starflut.getPlatform(); if( Platform == Starflut.ANDROID){ await Starflut.copyFileFromAssets("requests.zip", "flutter_assets/starfiles","flutter_assets/starfiles"); }

    python.call("import", ["sys"]);

    StarObjectClass pythonSys = await python.getValue("sys"); StarObjectClass pythonPath = await pythonSys.getValue("path"); pythonPath.call("insert",[0,assetsPath+"/flutter_assets/starfiles/requests.zip"]); python.call("import", ["requests"]);`

    1. However I still face the error message below. I think Starflut still don't recognize the package.

    I/flutter (13614): -1 4 ModuleNotFoundError: No module named 'requests' 0

    Could you guide me how to solve this?

    Thank you.

    ― You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/srplab/starcore_for_flutter/issues/5#issuecomment-770398846, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACXTPWKN4BNMRPUZJ6ILYDTS4VYSFANCNFSM4GMV3ANQ.

    srplab commented 3 years ago

    [Uploading requests.zip…]()

    MCTRACO commented 3 years ago

    Hi, i´m doing exactly what you have described but it doesn´t work, also i cant find StarflutPlugin.java so i cant modify it.Here is my debug log:

    I/skeletonproc_module,32084(15256): create service group[0], community 32bit for android_x86, version[3.7.5.260] @www.srplab.com I/skeletonproc_module,32087(15256): "SetRegisterCode" function can be used to change it to professional version dynamically I/skeletonproc_module,32132(15256): lua engine[Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio] I/skeletonproc_module,1592(15256): create service success[test] I/flutter (15256): x = true I/flutter (15256): .............................before copying............................. I/flutter (15256): r =true I/flutter (15256): docPath = /data/data/com.example.word_definition_filler_android/files I/flutter (15256): resPath = /data/data/com.example.word_definition_filler_android/files I/flutter (15256): assetsPath = /data/data/com.example.word_definition_filler_android/files I/flutter (15256): begin init Raw) I/flutter (15256): -1 1 load library [/data/app/~~BrEIhidqxm2qwTJin3FmGA==/com.example.word_definition_filler_android-m-_8xDsE-IQPl1XkT21ECA==/lib/x86/libstar_python39.so] success.... 6 I/skeletonproc_module,48747(15256): load library [/data/app/~~BrEIhidqxm2qwTJin3FmGA==/com.example.word_definition_filler_android-m-_8xDsE-IQPl1XkT21ECA==/lib/x86/libstar_python39.so] success.... D/starcore(15256): Begin call _PyCodecRegistry_Init W/Thread-2(15256): type=1400 audit(0.0:2257): avc: granted { execute } for path="/data/data/com.example.word_definition_filler_android/files/zlib.cpython-39.so" dev="dm-5" ino=132771 scontext=u:r:untrusted_app:s0:c155,c256,c512,c768 tcontext=u:object_r:app_data_file:s0:c155,c256,c512,c768 tclass=file app=com.example.word_definition_filler_android I/flutter (15256): -1 1 basic script [python] for version [39] is registered, you can use [python] to interact with the script 6 I/skeletonproc_module,48174(15256): basic script [python] for version [39] is registered, you can use [python] to interact with the script I/flutter (15256): 0 128 python39 null I/flutter (15256): initRaw = true W/Thread-2(15256): type=1400 audit(0.0:2258): avc: denied { read } for name="/" dev="dm-4" ino=2 scontext=u:r:untrusted_app:s0:c155,c256,c512,c768 tcontext=u:object_r:rootfs:s0 tclass=dir permissive=0 app=com.example.word_definition_filler_android I/flutter (15256): 0 4 [warn(1):(cmd:0):tm(9:58:54)][:0]ModuleNotFoundError("No module named 'requests'") 1 W/cmd,0 (15256): [:0]ModuleNotFoundError("No module named 'requests'") I/flutter (15256): python = Name92ED657A[python39:global] I/flutter (15256): /data/data/com.example.word_definition_filler_android/files/flutter_assets/starfiles/testcallback.py I/flutter (15256): return from python null I/flutter (15256): 0 32 a346353c-6bc9-11eb-8000-f8638fd7e393 0 I/flutter (15256): 0 1 service[test] is unloaded....... 6 I/skeletonproc_module,1406(15256): service[test] is unloaded....... I/skeletonproc_module,1406(15256): service[VSSysService] is unloaded....... I/flutter (15256): finish

    And my code

    ` if (TestCase == 3) { print("test case 3"); String Path1 = await Starflut.getResourcePath(); String Path2 = await Starflut.getAssetsPath(); StarCoreFactory starcore = await Starflut.getFactory(); StarServiceClass Service = await starcore.initSimple("test", "123", 0, 0, []); await starcore.regMsgCallBackP( (int serviceGroupID, int uMsg, Object wParam, Object lParam) async { print("$serviceGroupID $uMsg $wParam $lParam"); return null; }); StarSrvGroupClass SrvGroup = await Service["_ServiceGroup"];

        int Platform = await Starflut.getPlatform();
        if (Platform == Starflut.MACOS) {
          await starcore
              .setShareLibraryPath(Path1); //set path for interface library
          bool LoadResult =
              await Starflut.loadLibrary(Path1 + "/libpython3.9.dylib");
          print("$LoadResult"); //--load
          await Starflut.setEnv("PYTHONPATH",
              "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9");
          String pypath = await Starflut.getEnv("PYTHONPATH");
          print("$pypath");
        } else if (Platform == Starflut.WINDOWS) {
          //await starcore.setShareLibraryPath(
          //    Path1.replaceAll("\\","/")); //set path for interface library
        }
        /*---script python--*/
        bool isAndroid = await Starflut.isAndroid();
        if (isAndroid == true) {
    
          String docPath = await Starflut.getDocumentPath();
          await Starflut.setEnv("TMPDIR", docPath);
    
          dynamic x = await Starflut.copyFileFromAssets(
              "python3.9.zip", null, null); //desRelatePath must be null
          print("x = $x"); //desRelatePath must be null
    
          var nativepath = await Starflut.getNativeLibraryDir();
          var LibraryPath = "";
          if (nativepath.contains("x86_64"))
            LibraryPath = "x86_64";
          else if (nativepath.contains("arm64"))
            LibraryPath = "arm64-v8a";
          else if (nativepath.contains("arm"))
            LibraryPath = "armeabi";
          else if (nativepath.contains("x86")) LibraryPath = "x86";
    
          await Starflut.copyFileFromAssets(
              "zlib.cpython-39.so", LibraryPath, null);
          await Starflut.copyFileFromAssets(
              "unicodedata.cpython-39.so", LibraryPath, null);
          await Starflut.loadLibrary("libpython3.9.so");
          await Starflut.copyFileFromAssetsEx(
              "_struct.cpython-39.so", LibraryPath, null, false);
          await Starflut.copyFileFromAssetsEx(
              "binascii.cpython-39.so", LibraryPath, null, false);
          await Starflut.copyFileFromAssetsEx(
              "math.cpython-39.so", LibraryPath, null, false);
          await Starflut.copyFileFromAssetsEx(
              "_random.cpython-39.so", LibraryPath, null, false);
          await Starflut.copyFileFromAssetsEx(
              "_sha512.cpython-39.so", LibraryPath, null, false);
          await Starflut.copyFileFromAssetsEx(
              "_socket.cpython-39.so", LibraryPath, null, false);
          await Starflut.copyFileFromAssetsEx(
              "_ssl.cpython-39.so", LibraryPath, null, false);
          await Starflut.copyFileFromAssetsEx(
              "select.cpython-39.so", LibraryPath, null, false);
          await Starflut.copyFileFromAssetsEx(
              "array.cpython-39.so", LibraryPath, null, false);
          await Starflut.copyFileFromAssetsEx(
              "_hashlib.cpython-39.so", LibraryPath, null, false);
          await Starflut.copyFileFromAssetsEx(
              "_blake2.cpython-39.so", LibraryPath, null, false);
        }
        print(
            ".............................before copying.............................");
        bool copy = await Starflut.copyFileFromAssets("requests.zip",
            "flutter_assets/starfiles", "flutter_assets/starfiles");
        print("r =$copy");
    
        String docPath = await Starflut.getDocumentPath();
        print("docPath = $docPath");
    
        String resPath = await Starflut.getResourcePath();
        print("resPath = $resPath");
    
        String assetsPath = await Starflut.getAssetsPath();
        print("assetsPath = $assetsPath");
    
        print("begin init Raw)");
        bool rr1 = await SrvGroup.initRaw("python39", Service);
        print("initRaw = $rr1");
    
        StarObjectClass python =
            await Service.importRawContext("python", "", false, "");
    
        python.call("import", ["sys"]);
    
        StarObjectClass pythonSys = await python.getValue("sys");
        StarObjectClass pythonPath = await pythonSys.getValue("path");
        pythonPath.call("insert",
            [0, assetsPath + "/flutter_assets/starfiles/requests.zip"]);
        python.call("import", ["requests"]);
    
        print("python = " + await python.getString());
        print(assetsPath + "/flutter_assets/starfiles/" + "testcallback.py");
    
        StarObjectClass CallBackObj = await Service.newObject([]);
        await python.setValue("CallBackObj", CallBackObj);
    
        /*
        StarObjectClass retobj = await python.call("tt", ["hello ", "world"]);
        print("retobj  = $retobj");
        */
    
        await starcore.moduleExit();
    
        print("finish");
      }

    `

    I think it might be because of this: avc: denied { read } for name="/" dev="dm-4" ino=2 scontext=u:r:untrusted_app:s0:c155,c256,c512,c768 tcontext=u:object_r:rootfs:s0 tclass=dir permissive=0 but i don´t really know

    Thanks in advance!

    Hesam-Taleghani commented 3 years ago

    How to import sth like numpy to the package to use in python code? something like adding the pip installed fikes to site-packages of your python.zip causing this Error for me:

    D/starcore(14083): import encodings failed.... D/starcore(14083): ModuleNotFoundError("No module named 'encodings'",) D/starcore(14083): /data/data/com.example.flutter_ocr_test/files:/data/data/com.example.flutter_ocr_test/files/python3.6.zip:/data/data/com.example.flutter_ocr_test/files/libs64:/data/data/com.googlecode.pythonforandroid/files:/sdcard/com.googlecode.pythonforandroid/extras/python:/data/data/com.googlecode.pythonforandroid/files/python/lib/python2.6/lib-dynload:/data/data/com.googlecode.pythonforandroid/files/python/lib/python2.6:/data/data/com.googlecode.pythonforandroid/files/python/lib/python26.zip D/starcore(14083): Py_Initialize: unable to load the file system codec

    @srplab

    longtruongg commented 3 years ago

    The missing python module can be found in https://github.com/srplab/starcore_for_android/raw/master/android.python.3.9.0.zip. Add them to the project

    hi, i used this library, but getting error :

    " /lib/arm64/libpython3.9.so" is 32-bit instead of 64-bit", 
    I/flutter (18949): -1  1   load library error, try[/data/data/com.example.star_exam/files/srplab/libs64/libstar_python39.so]   6
    I/skeletonproc_module,48672(18949): load library error, try[/data/data/com.example.star_exam/files/srplab/libs64/libstar_python39.so]
    I/flutter (18949): -1  1   load library error, try[/data/app/com.example.star_exam-QWy8RXQ3T6ID7Qw-D9PSMA==/lib/arm64/libstar_python39.so]   6
    I/skeletonproc_module,48694(18949): load library error, try[/data/app/com.example.star_exam-QWy8RXQ3T6ID7Qw-D9PSMA==/lib/arm64/libstar_python39.so]
    I/flutter (18949): -1  1   load library [/data/app/com.example.star_exam-QWy8RXQ3T6ID7Qw-D9PSMA==/lib/arm64/libstar_python39.so] error....   6
    I/skeletonproc_module,48743(18949): load library [/data/app/com.example.star_exam-QWy8RXQ3T6ID7Qw-D9PSMA==/lib/arm64/libstar_python39.so] error...
    

    so could you guide me to fix it? @srplab

    MohsenZ20 commented 2 years ago

    The missing python module can be found in https://github.com/srplab/starcore_for_android/raw/master/android.python.3.9.0.zip. Add them to the project

    hi, i used this library, but getting error :

    " /lib/arm64/libpython3.9.so" is 32-bit instead of 64-bit", 
    I/flutter (18949): -1  1   load library error, try[/data/data/com.example.star_exam/files/srplab/libs64/libstar_python39.so]   6
    I/skeletonproc_module,48672(18949): load library error, try[/data/data/com.example.star_exam/files/srplab/libs64/libstar_python39.so]
    I/flutter (18949): -1  1   load library error, try[/data/app/com.example.star_exam-QWy8RXQ3T6ID7Qw-D9PSMA==/lib/arm64/libstar_python39.so]   6
    I/skeletonproc_module,48694(18949): load library error, try[/data/app/com.example.star_exam-QWy8RXQ3T6ID7Qw-D9PSMA==/lib/arm64/libstar_python39.so]
    I/flutter (18949): -1  1   load library [/data/app/com.example.star_exam-QWy8RXQ3T6ID7Qw-D9PSMA==/lib/arm64/libstar_python39.so] error....   6
    I/skeletonproc_module,48743(18949): load library [/data/app/com.example.star_exam-QWy8RXQ3T6ID7Qw-D9PSMA==/lib/arm64/libstar_python39.so] error...

    so could you guide me to fix it? @srplab

    same problem here. I am running the example on windows. please help :(