I want to read a file using Chaquopy. After some errors like "PermissionError" I wrote the same code in Python Idle and Chaquopy:
This code in Idle writes 'True' in output.
import xlrd, os
filename = 'F:/q1.xlsx'
print(os.path.isfile(filename))
And this one in Chaquopy outputs 'False':
import xlrd, os
filename = 'F:/q1.xlsx'
self.findViewById(R.id.label).setText(str(os.path.isfile(filename)))
I tried all the described combinations of '\' and '/'. It also cannot open a file in the same folder with Python activity if I write only its relative path. How to make Chaquopy work with files correctly?
You can access files either using pkgutil or by using the extractPackages setting. See the documentation for details.
Related
So I use chaquopy to get simple python programs functioning in an old (jelly bean) tablet (I replace the example console app's main.py in the src directory). Not bad for a beginner's start and I'm very happy.
But now for a test I try to display a matplotlib graph like this:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def main():
image = mpimg.imread("/storage/emulated/0/Documents/test.png")
plt.imshow(image)
plt.show()
The matplotlib library was installed from within android studio (albeit with a few missing elements, manual pip3 install and usage of local files). Now the build and program finish without errors, but there is no graph. Installation of pillow and use of other graph types no avail.
Can this be handled in python, or is a dive into android studio / java required?
Thanks for any advice
You'll have to include an ImageView in your app's layout, and then load the image file into it, as in this answer.
For an image which is generated dynamically by matplotlib, either save it to a file and then load from that file, or save it to a bytes object like this:
import io
bio = io.BytesIO()
plt.savefig(bio, format="png")
b = bio.getvalue()
... and then load that bytes object into the ImageView like in this app.
I can't run my python program on my android device using qpython3.
The first step in he program is to create a text file to save data to it. But I get an I/O error (file system read only)
This is the function used to create / or be sure that the file exists easily.
def filecreate(file): # creates the text file
f = open(file, 'a')
print('file created successfully\n')
print()
f.close()
How to overcome this problem in android ?
QPython by default runs your programs from the root directory which is not writable by non-root users. If you check the ftp utility (under About menu) you will find the name of the directory that is writable by QPython. On my HTC it is:
/storage/emulated/0/com.hipipal.qpyplus
So you need to do something along the lines of:
RootPath='/storage/emulated/0/com.hipipal.qpyplus'
...
import os
f = open(file, os.path.join(RootPath,'a'))
...
Example of a similar problem with QPython and SQLite3
Just create the file with QPython before use in your script.
You can create an txt file with QPython.
In My case I create the file whith name "pontoText.txt"
Inside folder "MyPythonScripts" (Im create that folder to put my python files)
so the file is in the same folder of the script and are created in QPython
Now an example code im make ton test:
import time
import os
a = input("1 to save date on file or 2 too see records on file. ")
folder= '/storage/emulated/0/MyPythonScripts'
if(a == "1"):
pontoTxt=open(os.path.join(folder,'pontoText.txt'),'w')
pontoTxt.write(time.strftime("%I %M %p on %A, %B %e, %Y"))
if(a == "2"):
pontoTxt=open(os.path.join(folder,'pontoText.txt'),'r')
exibe = pontoTxt.read()
print(exibe)
Your app may not have required permission to manage that file. Check your manifest, specify Android version and file path. Check documentation: http://developer.android.com/training/basics/data-storage/files.html
You have to actually find the file path, as it doesn't infer the directory that the file is in as the destination directory, unlike in Python on the PC.
Use what they're changed.
The file you have is inside /accounts/mastercard root and the file you're inside is /accounts/main.py you only need to use is this
/mastercard/xxxxx like my code
from django.shortcuts import render
from django.http import HttpResponse
from . import models
# Create your views here.
def index(request):
return render(request,"mastercard/Templates/mastercard/Base.html")
def about(request):
return render(request,"mastercard/Templates/mastercard/index.html")
I have installed QPython in my Android mobile.
I written a statement in the QEdit to read a text file from the below path
/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt
I used the below statement
fob=open('/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt','r')
fob.read()
If I run the statement, it is throwing error as:
IOError:[Errno 2] No such file or directory: '/storage/emulated/0/com.hipipal.qpyplus/script3/File1.txt'
1|uo_a116#cancro:/ $
Is the above statement correct?
fob=open('File1.txt','r')
Is not working in version 1.0.4.
fout=open('File2.txt','w')
Was working on version 0.9.6, but is not working in 1.0.4.
The "error" is Read only file system.
It looks like restrictions in the (new 1.0.4) file system library. I post a mail to the editor, but no answer at this time.
For testing, try to write absolute path to your files pointing, for example, to sdcard (/sdcard/out.txt).
I had problems on this versions (>=1.0.4) because launch process of script changes and execution directory is not the same as script directory.
I had to change my scripts to point to absolute paths.
It was tested with qpython developer.
Check this link:
https://github.com/qpython-android/qpython.org/issues/48
You can also try as simple as:
fob=open('File1.txt','r')
fob.read()
Just if the script is in the same folder of the file.
You can change the current working directory to path with script before read file:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
I haven't been able to find much on this topic.
I am trying to automate application testing, to where I place an app in a particular folder and I run the script: monkeyrunner.bat -v ALL myscript.py, and the script executes on whatever apk is in the folder called apkrepository. This makes it to where I do not have to alter my python script every time I test a new application.
The part where I am running into trouble is I am trying to use a variable for device1.installPackage()
See below for the code leading upto it.
installme = os.popen(r'dir C:\users\uname\desktop\apkrepository /A:-d /B').read()
print installme
# => com.application.android.apk #or whatever the package name is
filepath = r'C:\users\uname\desktop\apkrepository'
androidapp = filepath + '\\\' + installme
print androidapp
# => C:\users\uname\desktop\apkrepository\com.application.android.apk
#This exactly what I type below manually to get it to work
device1= MonkeyRunner.waitForConnection(15, "emulator-5554")
#Emulator was started in previous section of code, which is not shown here.
device1.installPackage(androidapp)
#DOES NOT WORK!!
device1.installPackage('c:\users\uname\desktop\apkrepository\com.application.android.apk')
#The only way it works seems to be to write the path in manually everytime.
I have tried many different ways to get this to work correctly, and I wasn't sure if it was something in the way(s) I was/were trying to do it. If the variable prints the correct file path I do not see how it would have issues working. This is probably something really easy, but this is where I am stuck. The error it gives:
E/Device: Error dyring Sync: Local Path does not exist. Error installing package C:\users\uname\desktop\apkrepository\com.application.android.apk
I am using windows 7 64 bit with python2.7 and the android sdk.
Thank you for any input/assistance provided! I have been stumped by this for a couple days.
Variables DO work for other places (monkeyrunner affiliated classes), like the device1.startActivity(component=runcomponent), where runcomponent is a combination of package + activity variables. Also note: I showed both device1.installPackages side by side for easy viewing. I do not run both in a row on my script.
I tried the same in linux machine, it works. Please have a look at the monkeyrunner script.
#! /usr/bin/env monkeyrunner
import re
import sys
import os
import java
import glob
import os
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device1= MonkeyRunner.waitForConnection(15, "emulator-5554")
mydir="/home/user/apk"
os.chdir(mydir)
for files in glob.glob("*.apk"):
print files
print "path " ,os.path.abspath(mydir+"/"+files)
device1.installPackage(mydir+"/"+files)
I just can't find the help.py file in order to create the API reference for the monkeyrunner. The command described at the Android references
monkeyrunner <format> help.py <outfile> does not work when i call monkeyrunner html help.py /path/to/place/the/doc.html.
It's quite obvious that the help.py file is not found and the monkeyrunner also tells me "Can't open specified script file". But a locate on my system doesn't bring me a help.py file that has anything to do with monkeyrunner or Android.
So my question is: Where did they hide the help.py file for creating the API reference?
I cannot find it either. But one can assume that it is simply calling MonkeyRunner.help() with the passed in arguments. If you just want to get something quick use this script I created also named help.py:
#!/usr/bin/env python
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
text = MonkeyRunner.help("html");
f = open('help.html', 'w')
f.write(text);
f.close();
Run it just like any other monkeyrunner script:
$ monkeyrunner help.py
After I have all codes in my machine (i.e, repo sync), it is at mydroid/sdk/monkeyrunner/scripts along with other three:
help.py monkey_playback.py monkey_recorder.py mr_pydoc.py
This is brilliant answer https://stackoverflow.com/a/4470513/551383 but if you really want this file is in android source i.e. http://androidxref.com/4.2_r1/xref/sdk/monkeyrunner/scripts/help.py
http://androidxref.com/source/xref/sdk/monkeyrunner/scripts/help.py
I believe the documentation on the website starts from that script, but I'm pretty sure somebody edits it a bit afterwards as well.
There's an error in monkeyrunner's help documentation (monkeyrunner Built-in Help), you should use parameters in another order:
monkeyrunner help.py <format> <outfile>
And don't forget about specifying a full path to the script, if you're running it outside of the monkeyrunner.bat directory (android monkeyrunner scripts).
If you don't have Repo Sync, described by users above, you can find the sources (including help.py), for example, here: monkeyrunner scripts.
I opened an issue at Google Code (Issue 26259: monkeyrunner Built-in Help Description Error) and I hope that they'll fix it soon.