Module doesn't have member in JSII Python package - python

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.

Related

Supporting python files not built in chaquopy

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
}

Installing python packages in Serverless Dataproc GCP

I wanted to install some python packages (eg: python-json-logger) on Serverless Dataproc. Is there a way to do an initialization action to install python packages in serverless dataproc? Please let me know.
You have two options:
Using command gcloud in terminal:
You can create a custom image with dependencies(python packages) in the GCR(Google Container Registry GCP) and add uri as parameter in the command below:
e.g.
$ gcloud beta dataproc batches submit
--container-image=gcr.io/my-project-id/my-image:1.0.1
--project=my-project-id --region=us-central1
--jars=file:///usr/lib/spark/external/spark-avro.jar
--subnet=projects/my-project-id/regions/us-central1/subnetworks/my-
subnet-name
To create custom container image for Dataproc Serveless for Spark.
Using operator DataprocCreateBatchOperator of airflow:
Add to python-file the script below, it will install the desired package and then load this package into the container path (dataproc servless), this file must be saved in a bucket, this uses the secret manager package as an example.
python-file.py
import pip
import importlib
from warnings import warn
from dataclasses import dataclass
def load_package(package, path):
warn("Update path order. Watch out for importing errors!")
if path not in sys.path:
sys.path.insert(0,path)
module = importlib.import_module(package)
return importlib.reload(module)
#dataclass
class PackageInfo:
import_path: str
pip_id: str
packages = [PackageInfo("google.cloud.secretmanager","google-cloud-secret-manager==2.4.0")]
path = '/tmp/python_packages'
pip.main(['install', '-t', path, *[package.pip_id for package in packages]])
for package in packages:
load_package(package.import_path, path=path)
...
finally the perator calls the python-file.py
create_batch = DataprocCreateBatchOperator(
task_id="batch_create",
batch={
"pyspark_batch": {
"main_python_file_uri": "gs://bucket-name/python-file.py",
"args": [
"value1",
"value2"
],
"jar_file_uris": "gs://bucket-name/jar-file.jar",
},
"environment_config": {
"execution_config": {
"subnetwork_uri": "projects/my-project-id/regions/us-central1/subnetworks/my-subnet-name"
},
},
},
batch_id="batch-create",
)

How to build a python script that can run with a web UI

I have a Python script that can create an excel file with information from the input given by user from the HTML UI I created with a few html pages, css and javascript. I need this to work offline and I need to distribute it to clients.
How can I bundle this HTML UI and Python file so that the client doesn't require to install Python or any dependencies to work with my app? I think eel does the job by not requiring the client to install python to work with this but in eel the client should already have chrome installed right?
Edit:
my js:
function elect() {
console.log('Im in function right now');
let {PythonShell} = require('python-shell');
var path = require('path');
var options = {
scriptPath : path.join(__dirname, '/../engine/')
}
var axiexe = new PythonShell ('test sort.py', options);
axiexe.on('message', function (message) {
swal(message);
})
}
My dir is E:\Web\testing my app\GUI inside it I have the folder node_modules with #electron, .bin, python-shelletc folders.
I am working right now on the Electron app supplied with Python backend.
My flow is following:
1. install Pyshell as npm module.
2. create your Electron UI
3. Compile your python code using pyinstaller.
then comes the trick. Pyshell in fact uses also a python installed on the machine.
Alternative is to run a server in Python, I raised a question to myself. Why should I if I have very trivial functions?
Solution:
modify python-shell to make it run exe files, also I use a trick that I say path to python is my compile python_code.exe
and my script is null, then I commented lines in python-shell where check for script length occurred
In communicator.js for the build version use:
let options = {
mode: 'text',
pythonPath: "resources/app/python_build/electron_backend.exe"
};
pyshell = new PythonShell(' ', options);
while for development Pyshell will grab your Python installation from env. variables. So we need only to provide a .py file path
pyshell = new PythonShell('electron_backend.py');
In order to make it run please change source of the pyshell module, comment following:
// if (scriptPath.trim().length == 0)
// throw Error("scriptPath cannot be empty! You must give a script for python to run");

Is there a NODE_ENV equivalent in Python

Is there a NODE_ENV equivalent in Python?
I want to dynamically load JSON configurations to the python application based on the executing environment. In nodeJs, I do this by using the process.env.NODE_ENV.
For example,
I start the app like this,
NODE_ENV=production node server.js
And use the variable in the application like this,
if(process.env.NODE_ENV == "production") {
// Load the production config file here (eg: database.json)
} else {
// Load the development config file here (located in a different directory)
}
How can I achieve the same in Python? Or can I make use of python virtualenv or python setuptools to have a workaround?
For starters, you could do something like this.
import os
env = os.environ.get("PYTHON_ENV")
if(env =="production"):
//
else :
The script can be run with PYTHON_ENV="production" python myscript.py. Or you could use some library like dotenv(https://github.com/theskumar/python-dotenv).
you can try environs library in python
pip install environs

Sublime CodeIntel, adding django path

I'm trying to configure Sumblime CodeIntel so that it works with django. The official docs state that:
For adding additional library paths (django for example), either add those paths as folders to your project, or create
an optional codeintel configuration file in your home or in your
project's root.
Configuration files (~/.codeintel/config or
project_root/.codeintel/config). All configurations are optional.
I localized and edited the ~/.codeintel/config file, so that config now looks like this(added the bolded part):
{
"Python": {
**"python": "C:/Python27/django-tom/Lib/site-packages",**
"pythonExtraPaths": [
"libs",
"~/Applications/Sublime Text 2.app/Contents/MacOS",
"/Applications/Sublime Text 2.app/Contents/MacOS",
]
}
}
`django-tom is my virtualenv django folder. However the autocompletion is not working for django (but works fine with the rest of python).
this is how i have it
{
"Python": {
"python": 'path to python/bin/python',
"pythonExtraPaths": ['path to pytho/python2.7/site-packages/',
]
},
}
if you're using env you can have hooks with virtualenvwrapper
let me know if you need help on setting that up
also there a sublimeRope package which is python specific

Categories