Supporting python files not built in chaquopy - python

i'm working on an app for object detection from yolo model. I have some supporting .py files which are necessary for the detection. But the issue is I even have placed all of them in src/main/python directory where my myscript.py is placed which is called in java. But still it is not being built in android after build. Can anyone please help me out, what I am doing wrong or what should I do.
I've also tried #144
You can see my directory hierarchy here direcotry hierarchy
And i'm importing like this importing files
I am just importing those files in myscript.py the same way as well call in python, for e.g 'from torch_utils import select_device' etc. and facing this error
error:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.imagetovolley, PID: 3187
com.chaquo.python.PyException: FileNotFoundError: [Errno 2] No such file or directory: '/data/user/0/com.example.imagetovolley/files/chaquopy/AssetFinder/app/torch_utils.py'
at <python>.pathlib.stat(pathlib.py:1197)
at <python>.torch_utils.date_modified(torch_utils.py:43)
at <python>.torch_utils.select_device(torch_utils.py:58)
at <python>.myscript2.<module>(myscript2.py:41)
at <python>.importlib._bootstrap._call_with_frames_removed(<frozen importlib._bootstrap>:219)
at <python>.importlib._bootstrap_external.exec_module(<frozen importlib._bootstrap_external>:783)
at <python>.java.android.importer.exec_module(importer.py:507)
at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:671)
at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:975)
at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:991)
at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:1014)
at <python>.importlib.import_module(__init__.py:127)
at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModule(chaquopy_java.pyx:156)
at com.chaquo.python.Python.getModule(Native Method)
at com.example.imagetovolley.MainActivity$3.onClick(MainActivity.java:144)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 3187 SIG: 9
build.gradle (app)
plugins {
id 'com.android.application'
id 'com.chaquo.python'
}
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
compileSdkVersion 29
ndkVersion "25.1.8937393"
defaultConfig {
applicationId "com.example.imagetovolley"
minSdkVersion 23
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
abiFilters "armeabi-v7a", "x86"
}
python {
pip {
install "wheel"
install "pybind11"
install "decorator"
install "matplotlib"
install "numpy"
install "opencv-python"
install "Pillow"
install "PyYAML"
install "scipy"
install "torch"
install "torchvision"
install "tqdm"
install "pandas"
install "seaborn"
install "fuzzywuzzy"
install "imutils"
install "requests"
install "scikit-build"
install "pybind11"
// install "patch-ng"
}
buildPython "C:/Users/GCS/AppData/Local/Programs/Python/Python36//python.exe"
}
sourceSets {
main {
python.srcDir "src/main/python"
}
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.4.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'id.zelory:compressor:2.1.1'
implementation 'com.karumi:dexter:6.2.1'
implementation 'com.android.volley:volley:1.1.1'
implementation 'de.hdodenhof:circleimageview:3.1.0'
// implementation 'com.squareup.okhttp3:okhttp:3.14.7'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation 'com.squareup.retrofit2:retrofit:2.8.1'
implementation 'com.squareup.retrofit2:converter-gson:2.8.1'
implementation 'com.google.api-client:google-api-client:1.23.0'
// implementation 'com.google.android.gms:play-services:11.8.0'
// implementation 'com.google.android.gms:play-services:9.0.2'
implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'
implementation 'net.sourceforge.jtds:jtds:1.3.1'
implementation 'com.google.api-client:google-api-client:1.23.0'
implementation 'com.google.android.gms:play-services-location:11.0.1'
}
build.gradle (project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
maven {url "https://chaquo.com/maven"}
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath 'com.chaquo.python:gradle:9.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

Related

Module doesn't have member in JSII Python package

I am following AWS User Guide to create a Python package from TypeScript Source via JSII. My TypeScript source looks like this:
export interface GreeterProps {
readonly greetee: string;
}
export class Greeter {
private readonly greetee: string;
public constructor(props: GreeterProps) {
this.greetee = props.greetee;
}
public greet(): string {
return `Hello, ${this.greetee}!`
}
}
This is section from JSII config for Python:
"targets": {
"python": {
"distName": "jsii-test.jsii-test",
"module": "jsii_test.jsii_test"
}
}
The project builds without errors and, and Python package is created successfully. I uploaded the package (via twine) to AWS CodeArtifact, and installed (via pip). When I import it in interactive Python console (import jsii_test) it imports successfully, but it doesn't seem to have the members exported from the original TypeScript source (GreeterProps, Greeter). What am I missing?
Project source: https://github.com/YuriGal/jsii-test
Turned out pip was installing package into incorrect location (I have to sort out my python versions). When I added path where the package was installed
import sys
sys.path.append('/usr/local/lib/python3.10/site-packages')
everything worked.

Visual Studio Code "python.h: No such file or directory" windows gcc

I 'm a total beginner in C++ and getting crazy trying to embed Python in C++ using VS Code IDE and GCC compiler.
I am stock and now I 'm keep facing this silly error that says:
python.h: No such file or directory gcc
I have followed steps explaned in "Using GCC with MinGW in VS Code" in order to configure C++ in VS Code but I failed to install MinGW (The bin folder was empty) so I add already installed CodeBlocks MinGW to my path and it seems to work.
I have python 3.8 installed and tried other solutions and already put Python.h and python library path in project include path.
"C:/Users/MPC/AppData/Local/Programs/Python/Python38-32/include/"
and
"C:/Users/MPC/AppData/Local/Programs/Python/Python38-32/libs/"
here is the code that I want to compile:
#include <stdio.h>
#include <conio.h>
#include <python.h>
int main()
{
PyObject* pInt;
Py_Initialize();
PyRun_SimpleString("print('Hello World from Embedded Python!!!')");
Py_Finalize();
printf("\nPress any key to exit...\n");
if(!_getch()) _getch();
return 0;
}
and this is my c_cpp_properties.json. (C++ configuration file):
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Users/MPC/AppData/Local/Programs/Python/Python38-32/include/**",
"C:/Users/MPC/AppData/Local/Programs/Python/Python38-32/libs/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/Program Files (x86)/CodeBlocks/MinGW/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x86"
}
],
"version": 4
}
and this is tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:/Program Files (x86)/CodeBlocks/MinGW/bin/gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/Program Files (x86)/CodeBlocks/MinGW/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"C:/Program Files (x86)/CodeBlocks/MinGW/bin/gcc.exe\""
}
]
}
The problem was a combinations of simple mistakes and as a beginner I solved it by some digging in gcc -l -L option flags for library link example and gcc arguments docs.
There are several simple and important points that should be observed:
1. The order of options is really important. If its wrong, compiler wont work. It should be like -I(capital i),-g,-L,-l(lower case L),-o.
2. To embed python in C++ firstly the path to python include files have to be known to compiler by -I option. Then the path to python libraries by a -L option. Then name of the each required python .lib file contained in libs folder of python.
Here is an example of it:
"-IC:/Users/MPC/AppData/Local/Programs/Python/Python38-32/include",=>(path to python include files)
"-g",=>(debug option specified by ide)
"${file}",
"-LC:/Users/MPC/AppData/Local/Programs/Python/Python38-32/libs",=>(path to python libraries)
"-lpython38",=>(python lib contained in python libs)
"-lpython3",=>(python lib contained in python libs)
"-l_tkinter",=>(python lib contained in python libs)
"-o",=>(output argument specified by ide)
"${fileDirname}\\${fileBasenameNoExtension}.exe"
I hope that with this answer, the beginners like me, get to the conclusion faster.
There is also a good youtube tutorial of Embedding python in C++ with CMake and Pybind11 and VS Code
Ok so you should probably go through like a intermediate c++ course first, but if u are a begginer and you really want to do this I recommend using visual studio 2019 or 2022, because it is way more begginer-friendly and the dependencies are all set in a gui interface.
BTW i would've commented this but I don't have 50 reputation yet so sorry.

SublimeText linting Python3 with pyflakes in virtualenv

When I open my Python files in Sublime and delete simple things like : at the end of def myFunction():, then I expect the linter to add a red circle in the gutter. But I see nothing in the gutter.
I notice at the bottom of the Sublime window it says "pyflakes(erred)"
Installing the dependencies
# Sublime v 3.1.1
# Sublime package install SublimeLinter
# Sublime package install SublimeLinter-pyflakes
pip3 install pyflakes
which pyflakes
# /Users/macbook/Desktop/my_virtual_env/bin/pyflakes
Trying to tell my linter the path to use in "MyProj.sublime-project" settings
{
"folders":[
{
"path": "."
}
],
"SublimeLinter.linters.pyflakes.python": "/Users/macbook/Desktop/my_virtual_env/bin/python3"
}
UPDATE: Sublime console output upon loading views.py
SublimeLinter: #10 linter.py:907: 'pyflakes' is linting 'views.py'
SublimeLinter: #10 python_linter.py:42: pyflakes: wanted python is 'None'
SublimeLinter: #10 python_linter.py:93: pyflakes: trying to use globally installed pyflakes
SublimeLinter: #10 python_linter.py:101: WARNING: cannot locate 'pyflakes'. Fill in the 'python' or 'executable' setting.
These settings go under the key settings. E.g.
{
"folders": [
{
"path": "."
},
],
"settings": {
"SublimeLinter.linters.pyflakes.python": "..."
}
}

Activate a Python virtualenv from a Node script

I'm writing a Node script where I install Python modules using pip. I'd like to activate a virtualenv and then install pip modules in that virtualenv. I could do something like the following:
proc.execFile("virtualenv", { args: "venv" }, function() {
proc.execFile("source", { args: "venv/bin/activate" }, function() {
proc.execFile("pip", { args: ["install", "myPipModule"]}, function() {
// do stuff
});
});
});
The issue with this is that it would lose the context of my virtualenv and so wouldn't install the modules where I want them. How can I keep the context of my virtualenv in my Node script so pip install puts modules in the right place?
Note: Similar to this question for Python but I'm using Node.
You don't need to activate a virtual env to run pip in it, you just use the path of the pip binary in the virtualenv and it will install it within that virtualenv.
proc.execFile("myapp/venv/bin/pip", { args: ["install", "myPipModule"]}, function() {
// do stuff
});

Boost python shared library issue in specific case

I am trying to extend my c++ library which is created using qtcreator to python with boost::python. I create a setup.py like following:
from distutils.core import setup
from distutils.extension import Extension
module1 = Extension('FOO',
include_dirs = ['/usr/include/python3.4', '/home/user/cppProjects/FOOLib','/usr/include', '/usr/local/lib/python3.4/dit-packages'],
libraries = ['python3.4', 'boost_python-py34', 'boost_thread', 'boost_system', 'FOOLib'],
library_dirs = ['/usr/lib/x86_64-linux-gnu/', '/usr/lib/python3.4/config-3.4m-x86_64-linux-gnu/','/home/skywalker/cppProjects/build-FOO-Desktop-Debug/'],
sources = ['pythonprovider.cpp'],
extra_compile_args=['-std=c++11'])
setup(name="PackageName",
ext_modules=[module1])
and Bar.h
class Bar
{
public:
Bar(boost::python::list args)
{
...
}
static void SetN(int n)
{
Bar::N = n;
}
static int N;
};
and Bar.cpp
int Bar::N;
pythonprovider.cpp is
BOOST_PYTHON_MODULE(FOO)
{
class_<Bar>("Bar", init<list>());
}
Compiling the library and using in c++ app is working fine.
I am typing the following code in terminal and installation of python module is working fine.
sudo python3 setup.py install
Including the package in python like
from FOO import *
working fine. But when i modify the pythonprovider.cpp with static method
BOOST_PYTHON_MODULE(FOO)
{
class_<Bar>("Bar", init<list>())
.def("SetN", &Bar::SetN)
.staticmethod("SetN");
}
compiling and installation working fine but importing the module assert an error
ImportError: libFOO.so.1: cannot open shared object file: No such file or directory
This happen to me before. When i move to body of constructor from cpp file to header file problem is solved but in this case it is not solve my problem.

Categories