Search code examples
pythonjavachaquopy

Why does chaquopy not recognize the Request library?


I tried to write my project using the Request library. Everything is fine and the chaquopy is well installed and runs other libraries like OS very quickly but does not even find Request library. what is the problem?

error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.imen_yar, PID: 6169
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imen_yar/com.example.imen_yar.MainActivity3}: com.chaquo.python.PyException: ModuleNotFoundError: No module named 'requests'
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        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)
     Caused by: com.chaquo.python.PyException: ModuleNotFoundError: No module named 'requests'
        at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:973)
        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_getModuleNative(chaquopy_java.pyx:155)
        at com.chaquo.python.Python.getModuleNative(Native Method)
        at com.chaquo.python.Python.getModule(Python.java:84)
        at com.example.imen_yar.MainActivity3.onCreate(MainActivity3.java:27)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        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) 

MainActivity code:

package com.example.imen_yar;

import static com.chaquo.python.Python.getInstance;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;

public class MainActivity3 extends AppCompatActivity {
    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.link_check);
        EditText Ed_text = findViewById(R.id.Ed_text);
        TextView text = findViewById(R.id.text);
        if(!Python.isStarted()){
            Python.start(new AndroidPlatform(this));
        }
        PyObject re = getInstance().getModule("requests");
        String address = "https://google.com";
        int address_login = re.callAttr("get", address).toInt();
        String local_address = "";
        int test = 0;
        for(int num = 6; num > 0; num = num - 1){
            char[] address_char = address.toCharArray();
            local_address = local_address + address_char[0] + address_char[1] + address_char[2] + address_char[3] + address_char[4] + address_char[5];
        }
        if(address_login != 200){
            test+=1;
        }
        if(!local_address.equals("https")){
            test+=1;
        }
        if(test == 0){
            text.setText("link is safe");
        }else{
            text.setText("link isn't safe");
        }
    }
}

Solution

  • requests isn't part of the Python standard library, so you'll need to add it to your app by following the instructions here.