23

I am trying to se python in android studio as a backend code for working on variables and producing answer. My MainActivity is in Java and a directory is created for python code. when I try to write code in python is tells that "no python interpreter configured for the module" I have added Chaquopy and Python Community Plugin. Also I want to know how can I send variables for my MainActivity to python and viceversa.

I am trying to add python in android studio. I have tried Chaquopy and also Python Community Plugin. I have tried to find answers on different places and guides too. But no Luck till now.

buildscript {
    repositories {
        google()
        jcenter()
        maven{url "https://chaquo.com/maven"}
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'
        classpath "com.chaquo.python:gradle:0.5.0"
    }
}

this is MainActivity and I want to send my variable "a" to python file to work on it.

package com.example.testingpython;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int a=2;
    String[] array={"My","Name","Java"};

}

}

Zohaib Imtiaz
  • 369
  • 1
  • 3
  • 8

5 Answers5

18

These are steps that work:

  1. Install the JetBrains Python Community Edition.
  2. Click Tools -> Sync Python Requirements...
  3. A notification popup appears saying you need to configure your interpreter. Click the link in that popup. That link takes you to the necessary dialog window (the one you also see in Jiya's answer.) (That the link in the notification bar takes you to a different one is probably a bug.)

config

Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
YouCii
  • 221
  • 2
  • 7
  • That worked the first time to set up the interpreter, so thanks a million! Now how do we change the interpreter? If I click that again now that I have an interpreter, it takes me to a different dialog. – FuriousGeorge May 20 '22 at 14:39
  • There is no such toolbar option in Android Studio. – IgorGanapolsky Nov 22 '22 at 18:30
6

when I try to write code in python is tells that "no python interpreter configured for the module"

Only a few features of the Python Community Edition plugin will work properly in Android Studio. You can still write your Python code in Android Studio if you want, but most of the IDE assistance will be unavailable.

Even if the code displays error indicators, you can still go ahead and run your app, and if any of the errors are real, the details will be displayed in the Logcat.

how can I send variables for my MainActivity to python and viceversa.

See the example code in the Chaquopy documentation and demo app.

mhsmith
  • 6,675
  • 3
  • 41
  • 58
  • but how to configure python interpreter? – Samudra Ganguly Nov 20 '20 at 22:27
  • @SamudraGanguly You can't configure it, but it doesn't matter. Just go ahead and run your app as my answer explains. – mhsmith Nov 21 '20 at 02:34
  • Sir, I am unable to understand this. Let me explain the whole scenario from the very beginning. I want to run a Python program from MainActivity using Process.getRunTime().exec("Test.py"). Then I tried to create a file. When I created a file by the name Test.py, Android Studio automatically downloaded Python plugins for me and it is able to recognize a Python file. But when I write a code in the Python file, it shows errors (red lines) and the message "No Python interpreter configured for the module" is displayed. – Samudra Ganguly Nov 21 '20 at 06:09
  • Like I said, there's currently no way to configure the Python IDE support in Android Studio, but you can just ignore the message and the error indicators and your app will still work. Or if you prefer, you can edit your Python code in a different program such as PyCharm. – mhsmith Nov 21 '20 at 13:09
  • it says "Cannot run python error=13 permission denied". Any solution for that, sir? – Samudra Ganguly Nov 21 '20 at 15:48
  • Please create an issue on [our GitHub page](https://github.com/chaquo/chaquopy/issues) and include full details of what happened. – mhsmith Nov 21 '20 at 19:24
3

I already have Python Community Edition Plugin But still didn't work...
This solved my problem :

enter image description here

• Open "project structure"

• Click New...

• Select Python SDK

enter image description here

• Add Interpreter and click Ok

Jiya
  • 745
  • 8
  • 19
  • 2
    As discussed in the comments below Nick Fortescue's answer, most people seem to have a different version of the Project Structure dialog which doesn't have these settings. For me in Android Studio 4.0.1, the Project page only has settings for the Android Gradle Plugin Version and Gradle Version. See [this page](https://navigatingandroidstudio.blogspot.com/2019/08/how-to-avoid-death-by-gradle-painless.html) for a screenshot (search for "Project Structure"). – mhsmith Sep 30 '20 at 16:20
  • 1
    Even I use V4.0.1, Just checked Project settings are different for flutter projects... – Jiya Sep 30 '20 at 18:24
0

If you have installed the JetBrains Community Edition Python plugin then you can set it up following the Instructions on the JetBrain site.

On a mac:

  • File menu
  • Project structure
  • Go down to "Platform Settings"
  • SDKs
  • press the + button
  • Choose "Python SDK"
  • I chose "System Interpreter" but a virtual envs environment will usually be a better choice
  • Once the SDK has been set up, Click on "Modules" under "Project settings"
  • Press +
  • Choose "Python" for your module type

Done!

A comment suggested SDKs doesn't exist in Android Studio. A screenshot demonstrating it for me: enter image description here

Nick Fortescue
  • 43,045
  • 26
  • 106
  • 134
  • 9
    This answer is valid for IntelliJ IDEA, but unfortunately in Android Studio the Project Structure dialog is different, and the "SDKs" section doesn't exist. – mhsmith Apr 12 '20 at 12:24
  • I just did exactly this in Android Studio 3.6.2 – Nick Fortescue Apr 15 '20 at 18:36
  • Strange, for me it looks completely different, like the screenshot at https://developer.android.com/studio/releases#psd. Maybe it's because you're using Flutter. – mhsmith Apr 16 '20 at 11:30
  • Really strange. My experience matches @mhsmith - my project settings looks like the one in the developer.android.com site and I am unable to configure the python interpreter. I am not using Flutter, this is a stock Android project – Mike Hardy May 15 '20 at 18:49
  • Maybe adding the flutter plugin gives you some extra options which stock android studio doesn't have. It would be strange if true, but I guess it is possible. – Nick Fortescue May 30 '20 at 19:52
  • 2
    Very limited fields for me, AS 4.0, windows 10, no Flutter. https://ibb.co/cLrkvDM – John Jun 12 '20 at 20:39
0

Android Studio Dolphin | 2021.3.1 Patch 1. works like @Jiya. But Notice you must open the python project directory in a new studio window. That will make studio to identify the project is a python project not a android project and will activate the Python Community Edition plugin

Victor Choy
  • 4,006
  • 28
  • 35