ivlivs-iii / dart_python_ffi Goto Github PK
View Code? Open in Web Editor NEWA Python-FFI for Dart. Easily import any pure Python module into your Dart or Flutter project.
License: BSD 3-Clause "New" or "Revised" License
A Python-FFI for Dart. Easily import any pure Python module into your Dart or Flutter project.
License: BSD 3-Clause "New" or "Revised" License
Hello @IVLIVS-III, I've successfully launched example flutter project on macOS, but on Windows I am getting the following errors.
In python_ffi_cpython.dart
file on line 47 - await tmpZipFile.delete();
Exception has occurred. PathAccessException (PathAccessException: Cannot delete file, path = 'C:\Users\Anton\AppData\Roaming\com.example\example/python_ffi/lib/python3.11.zip' (OS Error: The process cannot access the file because it is being used by another process. , errno = 32))
As you can see from the error log the path is incorrect and has /
and \
in different places. Are you planning on fixing this anytime soon?
P.S. After fixing the path in this particular place, I've found out that the issue starts to show up in other places.
@IVLIVS-III The example project still does not work after the fix. Here is the log:
Launching lib\main.dart on Windows in debug mode...
lib\main.dart:1
√ Built build\windows\runner\Debug\example.exe.
Connecting to VM Service at ws://127.0.0.1:50354/PgO4aU0b_qk=/ws
Could not find platform independent libraries <prefix>
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'python'
isolated = 0
environment = 1
user site = 1
safe_path = 0
import site = 1
is in build tree = 0
stdlib dir = 'C:\Users\Anton\Documents\Flutter\dart_python_ffi\packages\python_ffi\example\Lib'
sys._base_executable = 'C:\\Users\\Anton\\Documents\\Flutter\\dart_python_ffi\\packages\\python_ffi\\example\\build\\windows\\runner\\Debug\\example.exe'
sys.base_prefix = 'C:\\Users\\Anton\\Documents\\Flutter\\dart_python_ffi\\packages\\python_ffi\\example'
sys.base_exec_prefix = 'C:\\Users\\Anton\\Documents\\Flutter\\dart_python_ffi\\packages\\python_ffi\\example'
sys.platlibdir = 'DLLs'
sys.executable = 'C:\\Users\\Anton\\Documents\\Flutter\\dart_python_ffi\\packages\\python_ffi\\example\\build\\windows\\runner\\Debug\\example.exe'
sys.prefix = 'C:\\Users\\Anton\\Documents\\Flutter\\dart_python_ffi\\packages\\python_ffi\\example'
sys.exec_prefix = 'C:\\Users\\Anton\\Documents\\Flutter\\dart_python_ffi\\packages\\python_ffi\\example'
sys.path = [
'C:\\Users\\Anton\\Documents\\Flutter\\dart_python_ffi\\packages\\python_ffi\\example\\build\\windows\\runner\\Debug\\python311.zip',
'C:\\Users\\Anton\\Documents\\Flutter\\dart_python_ffi\\packages\\python_ffi\\example',
'C:\\Users\\Anton\\Documents\\Flutter\\dart_python_ffi\\packages\\python_ffi\\example\\Lib',
'C:\\Users\\Anton\\Documents\\Flutter\\dart_python_ffi\\packages\\python_ffi\\example\\build\\windows\\runner\\Debug',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00000bfc (most recent call first):
<no Python frame>
Lost connection to device.
Exited (sigterm)
Originally posted by @pushkinman in #5 (comment)
When trying to run the command dart run dartpip install
on Windows 11, an exception was thrown:
PS python_ffi_poc> dart run dartpip install
supportDir: C:\Users\[...]\AppData\Local\Temp
Unhandled exception:
PythonFfiException: Failed to cache Python runtime for version 3.11.3 on windows
#0 PythonFfiCPythonDart._ensurePythonRuntime (package:python_ffi_cpython_dart/src/python_ffi_cpython_dart_base.dart:144:7)
<asynchronous suspension>
#1 PythonFfiCPythonDart.openDylib (package:python_ffi_cpython_dart/src/python_ffi_cpython_dart_base.dart:115:7)
<asynchronous suspension>
#2 PythonFfiCPythonMixin.initialize (package:python_ffi_cpython_dart/src/python_ffi_cpython_dart_base.dart:416:7)
<asynchronous suspension>
#3 solve (package:dartpip_solver/src/dartpip_solver_base.dart:49:3)
<asynchronous suspension>
#4 InstallCommand._collectAllDependencies (package:dartpip/commands/install.dart:113:58)
<asynchronous suspension>
#5 InstallCommand._downloadAllDependencies (package:dartpip/commands/install.dart:138:9)
<asynchronous suspension>
#6 InstallCommand.run (package:dartpip/commands/install.dart:190:34)
<asynchronous suspension>
#7 CommandRunner.runCommand (package:args/command_runner.dart:212:13)
<asynchronous suspension>
#8 main (file:///C:/Users/[...]/AppData/Local/Pub/Cache/git/dart_python_ffi-8c63ce86bc25d9bbeed222389088f30583d67db9/packages/dartpip/bin/dartpip.dart:14:3)
<asynchronous suspension>
Note: I do have Python 3.11.5 installed on the PC and I'm using the latest GitHub updates of the repo (main
branch).
PS C:\Users\[...]\Programming\python_ffi_poc> python --version
Python 3.11.5
So, I decided to try to use the download
command first, and then the bundle
command:
PS C:\Users\[...]\Programming\python_ffi_poc> dart run dartpip download openpyxl
Cache directory: null/.dartpip/cache/pypi
Downloading Python module 'openpyxl'...
Downloading https://files.pythonhosted.org/packages/42/e8/af028681d493814ca9c2ff8106fc62a4a32e4e0ae14602c2a98fc7b741c8/openpyxl-3.1.2.tar.gz...
Extracting null/.dartpip/cache/pypi/openpyxl-3.1.2.tar.gz...
PS C:\Users\[...]\Programming\python_ffi_poc> dart run dartpip bundle -r . -m python-modules
Bundling Python module 'basic_operations'...
Bundling Python module 'excel_test'...
From that, I noticed that the cache folder (null/.dartpip...
) was created in the root of the project and the openpyxl
files were extracted there. However, its dependency (et-xmlfile
) was not downloaded. Also, none of those files were included under python-modules
folder.
Flutter Doctor:
PS C:\Users\[...]\Programming\python_ffi_poc> flutter doctor -v
[✓] Flutter (Channel stable, 3.13.2, on Microsoft Windows [Version 10.0.22621.2134], locale pt-BR)
• Flutter version 3.13.2 on channel stable at C:\src\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision ff5b5b5fa6 (12 days ago), 2023-08-24 08:12:28 -0500
• Engine revision b20183e040
• Dart version 3.1.0
• DevTools version 2.25.0
[✓] Windows Version (Installed version of Windows is version 10 or higher)
[✗] Android toolchain - develop for Android devices
✗ ANDROID_HOME = C:\Users\[...]\AppData\Local\Android\Sdk
but Android SDK not found at this location.
[✓] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[✓] Visual Studio - develop Windows apps (Visual Studio Community 2019 16.11.5)
• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
• Visual Studio Community 2019 version 16.11.31729.503
• Windows 10 SDK version 10.0.19041.0
[!] Android Studio (not installed)
• Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
[✓] VS Code, 64-bit edition (version 1.81.1)
• VS Code at C:\Program Files\Microsoft VS Code
• Flutter extension version 3.46.0
[✓] Connected device (3 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22621.2134]
• Chrome (web) • chrome • web-javascript • Google Chrome 116.0.5845.141
• Edge (web) • edge • web-javascript • Microsoft Edge 116.0.1938.69
[✓] Network resources
• All expected network resources are available.
! Doctor found issues in 2 categories.
Hello @IVLIVS-III, I wanted to ask a couple of questions about the solution and I would be greatful for your responce:
Are there any potential memory leaks in the following code from your examples?
void primitivesSqrt() {
final PrimitivesModule primitivesModule = PrimitivesModule.import();
final int sqrt = primitivesModule.sqrt(9);
debugPrint("Sqrt: $sqrt");
}
Hi.
I want to run a script in windows release but when run project show an error:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PathAccessException: Cannot delete file, path = 'C:\Users\kondo\AppData\Roaming\ir.hezare\fishyar/python_ffi/lib/python3.11.zip' (OS Error: The process cannot access the file because it is being used by another process.
, errno = 32)
#0 _checkForErrorResponse (dart:io/common.dart:55:9)
#1 _File._delete.<anonymous closure> (dart:io/file_impl.dart:315:7)
<asynchronous suspension>
#2 PythonFfiCPython.copyPythonStdLib (package:python_ffi_cpython/python_ffi_cpython.dart:47:5)
<asynchronous suspension>
#3 PythonFfiCPythonMixin.initialize (package:python_ffi_cpython_dart/src/python_ffi_cpython_dart_base.dart:312:5)
<asynchronous suspension>
#4 PythonFfi.initialize (package:python_ffi/src/python_ffi_base.dart:26:7)
I do example's steps in my project.
Please help...
Thanks.
When i tried to run example flutter_package_import app in android or ios, im getting an error
I cant see any ios/android folder in example apps, Is it possible to use this library in iOS/Android apps?
E/flutter (11838): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: LateInitializationError: Field '_instance@287145062' has not been initialized.
E/flutter (11838): #0 PythonFfiDelegate._instance (package:python_ffi_interface/src/delegate.dart)
E/flutter (11838): #1 PythonFfiDelegate.instance (package:python_ffi_interface/src/delegate.dart:18:53)
E/flutter (11838): #2 PythonFfi.initialize (package:python_ffi/src/python_ffi_base.dart:39:31)
E/flutter (11838): #3 initialize (package:flutter_package_export/flutter_package_export.dart:9:28)
E/flutter (11838): #4 main (package:flutter_package_import/main.dart:6:9)
E/flutter (11838): #5 _runMain.<anonymous closure> (dart:ui/hooks.dart:301:23)
E/flutter (11838): #6 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
E/flutter (11838): #7 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter (11838):
If I decide to bundle Python files in external Flutter package B and reference it from Flutter app A the app successfully loads Python runtime (goes past PythonFfi.instance.initialize()
) yet it fails to load assets (and modules) defined in pubspec.yaml
of B. The reason for that is due to need to use different paths to assets inside package - module loading is failing in discoverPythonModules()
in python_ffi_cpython.dart
.
What I suggest in order to enable scenarios with external packages is to introduce optional param in discoverPythonModules()
, e.g.:
@override
Future<Set<PythonModuleDefinition>> discoverPythonModules(
[String packagePath = "",]) async {
try {
final ByteData modulesJsonRaw = await PlatformAssetBundle()
.load("${packagePath}python-modules/modules.json");
return _decodePythonModules(
utf8.decode(modulesJsonRaw.buffer.asUint8List()),
).toSet();
// ignore: avoid_catching_errors
} on FlutterError catch (e) {
debugPrint("Failed to load python modules: $e");
return <PythonModuleDefinition>{};
}
}
and also have it in python_ffi_base.dart
to be forwarded down to python_ffi_cpython.dart
FutureOr<void> initialize([String packagePath = ""]) async =>
await PythonFfiDelegate.instance.initialize(packagePath);
This way I could initialize python_ffi like that:
Future<void> init() async {
await PythonFfi.instance.initialize('packages/dns_engine/');
}
Could you please have a look at the issue? Or there might be a better way to implement my scenarios without changing anything.
Hello, guys!
I'm trying to use the "openpyxl" Python package in a POC app and when I try to run the application, I have this exception thrown: "PythonFfiException (PythonFfiException: Failed to import module excel_test)".
I understand that this library requires the package "et_xmlfile", so I downloaded it and included it as well in the modules declaration under pubspec.yaml.
Also, I have Python 3.11.5 installed.
I understand this package is supposed to be used to import "pure Python modules". To be honest, I'm not really sure about what this means, so my apologies if my error is related to that and I'd appreciate any explanations about it.
This is my exception traceback:
Repository link with source code.
Thank you in advance!
I have some ML commands I need to execute via Python and Python math libraries. Is dart python ffi a good use case for executing this script to then expect a return value?
Hey nice packages , it would be great if we can support other platforms as well somehow
Starting point: https://www.egenix.com/products/python/PyRun/
Quick question, is it possible to use dnspython with your solution? if yes, what is good way to integrate it?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.