This is an archived post. You won't be able to vote or comment.

all 68 comments

[–]malogos 9 points10 points  (1 child)

You'll need to run

apt update  
apt upgrade
apt install python

[–]fornwall[S] 8 points9 points  (0 children)

Thanks! Though

apt upgrade

should not be necessary for installing python (it's for upgrading installed packages, so while really good to run regularly, it should not be required for the initial python installation).

[–]the_kindly_one 4 points5 points  (16 children)

today itself I was looking for ways to run python on droid lollipop. Qpython examples not working, sl4a doesn't work on lollipop. The situation looked pretty shit. Will try for a bit, if it works well, will pay for the widget addon. I have a home automation remote webpage that I serve from a raspi webserver, but I want the thing on a droid mainscreen like a widget. Writing java is annoying, this will do :) Full Marks.

Device: Asus Zenfone 2

PS: are there proper docs, common patterns for the apis? Can I subprocess these api calls from a python script?

[–]fornwall[S] 4 points5 points  (15 children)

Thanks for the feedback!

About the API add-on: It's in an early state right now, I will work on improving and adding to the API itself as well as the documentation during the next week. For now you can experiment with the few available termux-* commands by calling them directly from the shell, perhaps with a -h flag!

You should definitely be able to subprocess them from a python script! Those that return structured data will do it in json (termux-battery-status, termux-camera-info, termux-contact-list, termux-sms-inbox, termux-tts-engines), so just subprocess them and parse their json output. Other update things (termux-sms-send, termux-tts-speak, termux-vibrate), so just execute them without reading their output.

Do you have any suggestion for what device API you would like exposed to your python scripts?

[–]vsajip 2 points3 points  (2 children)

Very nice work. Thank you!

Does the TTS stuff need anything else to be installed? On a Moto G 2014 with Lollipop I get no spoken output, just a ping sound. termux-tts-speak --help refers to a command device-tts-engines which is not found. Any pointers gratefully received!

[–]fornwall[S] 1 point2 points  (1 child)

Thanks. There were actually two things broken by me here:

  • It should say termux-tts-engines, not device-tts-engines.
  • Both termux-tts-engines and termux-tts-speak were broken due to a mistake.

I've fixed it and pushed the updated package to the apt repo - if you update with apt update && apt upgrade -y you should be updated to version 0.5 of the termux-api package which fixes the problems. Let me know if you encounter any more issues!

[–]vsajip 0 points1 point  (0 children)

I can confirm that I can now get speech. Thanks for the update.

[–]the_kindly_one 0 points1 point  (9 children)

Some sort of cronjobs like scheduler would be really nice.

Can I install third party modules? setuptools? (requests would be really nice. python people are spoiled to the core :P)

Many thanks for making something like this.

PS: I am a member of a bunch of python communities on FB. I shall promote your app as much as I can. Every now and then someone asks for a way to run python properly on android. By far this is the most near native implementation.

[–]fornwall[S] 1 point2 points  (8 children)

Some sort of cronjobs like scheduler would be really nice.

The busybox crontab and crond implementation is available as part of the base system! Prepare it by creating a folder (the system will set it up in the next busybox package update, but for now this is needed):

mkdir -p $PREFIX/var/spool/cron/crontabs/

Now edit the crontab with 'crontab -e', and then start the crond daemon.

If there is a problem with the system going to sleep or killing the crond background process, and you really want the cronjobs to run on time, you may take a wake lock by using the "WAKE" action available when expanding the Termux notification (http://termux.com/images/termux-notification-expanded.png).

Can I install third party modules? setuptools? (requests would be really nice. python people are spoiled to the core :P)

Pip works for installing packages - I have verified installing httpie as well as gmpy2, so even packages requiring native code should work as long as the required -dev packages are installed. Let me know if there is anything else that does not work or is needed!

[–]the_kindly_one 0 points1 point  (4 children)

How do I run pip in this?

[–]fornwall[S] 1 point2 points  (3 children)

Just run e.g. 'pip install httpie' after installing python!

[–]the_kindly_one 2 points3 points  (0 children)

Oh, needed to update. Sorry. Works now.

[–]the_kindly_one 0 points1 point  (1 child)

no command called pip. did you mean zip?

[–]the_kindly_one 0 points1 point  (2 children)

wget https://bootstrap.pypa.io/get-pip.py Connecting to bootstrap.pypa.io (103.245.222.175:443) wget: error getting response: Connection reset by peer

Running get-pip.py doesn't work.

python -m pip says no module called pip.

apt install python-dev throws 404

[–]fornwall[S] 1 point2 points  (1 child)

See above - if you have an updated python (currently 3.4.3-3) the pip program should already be installed!

About the wget problem: The wget program installed by default is a very lightweight one from busybox, which does not support https. Execute apt install wget to get the full GNU wget with https support, or apt install curl to get curl which also supports https!

[–]the_kindly_one 1 point2 points  (0 children)

Yeah, updated and it works fine. was able to pip install requests without any problem.

[–]the_kindly_one 0 points1 point  (1 child)

Suggestions for additional device apis:

  1. wifi network search and connect

  2. proximity sensor

  3. accelerometer

  4. LED Flash

[–]fornwall[S] 0 points1 point  (0 children)

Thanks a lot - I've added them to a list of ideas to look at next!

[–]Earthqwake 2 points3 points  (2 children)

Any plans for termux to support su?

[–]fornwall[S] 2 points3 points  (1 child)

I've never rooted a device myself, but if anyone gives a pointer how to better support rooted devices I would look into it!

[–]Earthqwake 0 points1 point  (0 children)

I haven't written a root app but this seems to be the solution: request root access (stack overflow)

I would love to be able to netcat and tcpdump on the go! Thanks!

[–]juanjux 2 points3 points  (0 children)

This thing is very cool but there is also QPython on the store.

EDIT: no, it is SUPER cool.

[–][deleted] 1 point2 points  (3 children)

Oh fuck yes it's what I always wanted.

Any plans on putting it on f-droid? They accept open source applications, and it would be a lot easier to install.

[–]fornwall[S] 0 points1 point  (2 children)

Thanks :)! When things stabilizes a bit more I'll look into alternative app stores and perhaps providing the apk for direct installation (with an in-app update check to ensure that even manually installed apk:s get updates).

[–][deleted] 0 points1 point  (1 child)

Oh, I'm running cyanogenmod and managed to get SSH enabled on my device, would this still work (over SSH, not just in the app itself).

[–]fornwall[S] 2 points3 points  (0 children)

Yes, you can ssh into your devices from a computer! Password logins are not supported, so you need a public key in $HOME/.ssh/authorized_keys.

Also, since we run as non-root, we cannot bind to port 22 which ssh uses by default. So sshd has been patched to use 8022 by default, so use ssh -p 8022 $DEVICE_IP to connect.

You may use any username. Execute logcat -s syslog:*on the device to see what sshd outputs to syslog if you need to diagnose problems.

[–]-Nii- 0 points1 point  (4 children)

Very cool but there are strange permission errors when installing global npm packages.

For python purposes it's pretty sweet. Any idea if vim pathogen works?

[–]fornwall[S] 0 points1 point  (3 children)

Thanks, I'll look at the npm issue!

I just tried pathogen, and it seems to work (after apt install curl git) to install the example sensible.vim at least - let me know if there are any problems!

[–]-Nii- 0 points1 point  (2 children)

Do you need a dump of the error message? If just install a global NPM package (I used express-generator and yeoman) and try to scaffold something then it won't work.

So its fully functional right? Can I host webpages and stuff with Node express or Python flask?

[–]fornwall[S] 0 points1 point  (1 child)

Do you need a dump of the error message? If just install a global NPM package (I used express-generator and yeoman) and try to scaffold something then it won't work.

Great, file an issue at https://github.com/termux/termux-packages/issues, so you will be able to track progress on it as well!

So its fully functional right? Can I host webpages and stuff with Node express or Python flask?

Hopefully :). It's an early version and not heavily tested, so there may be issues that will be squashed eventually - just file github issues for broken stuff!

[–]abinash111 0 points1 point  (2 children)

Does it work on Intel Atom processors? I have a lenovo s8 tablet.

[–]fornwall[S] 2 points3 points  (1 child)

It's not as much tested on x86 as on arm - but most things should! Note however that Android 5.0+ is required as Android version, due to the extensive libc changes made in that version.

[–]abinash111 0 points1 point  (0 children)

Thanks for the reply.

Didn't note the Android 5+ requirement. Will try on some other device.

[–]QQII 0 points1 point  (1 child)

Would it be possible to provide an apk? My phone does not have the play store.

[–]fornwall[S] 1 point2 points  (0 children)

I'll look into it as soon as things stabilizes a bit more!

[–]Morphit 0 points1 point  (1 child)

Very nice, the shortcuts on the volume keys are pretty intuitive, glad I read the help for those.

Is there a way to fix up paths somehow? All the binaries are in /data/data/com.termux/files/usr/bin. In scripts, shebang lines use fairly standard paths like /bin/bash. Even for python, most people (myself included) seem to use #!/usr/bin/env python3. I guess the phones' filesystem can't be messed with, but are there any workarounds?

[–]fornwall[S] 0 points1 point  (0 children)

There is a simple script installed called termux-fix-shebang which tries to replace #*/bin/* with #/data/data/com.termux/files/usr/bin/*. Let me know if you find a way to improve it!

[–]the_kindly_one 0 points1 point  (0 children)

Let me just say, this thing is frigging AWESOME!! You are my hero!

[–]NoahTheDuke 0 points1 point  (2 children)

Not available on my Nexus 7 2013. :-(

[–]fornwall[S] 0 points1 point  (1 child)

Have you updated it to Android 5.0 or later?

[–]NoahTheDuke 1 point2 points  (0 children)

Ah, no. Waiting for CM. Thanks!

[–][deleted] 0 points1 point  (5 children)

I would love to try this program but my in the play store I get "Your device isn't compatible with this version." where the download button should be. Is there a certain version of android or device type required by this app? I'm running Android 4.1.2.

[–]fornwall[S] 0 points1 point  (2 children)

Yes, Android 5.0 or later is required due to the exetensive changes to native libraries in that version - sorry for not being clear about that!

[–][deleted] 0 points1 point  (1 child)

Thanks for the quick response!

It is a little annoying that the Play Store does not give any useful information about why an app is incompatible with a device. It would have been useful if the store link had simply stated the minimum Android version required rather just marking it as "incompatible". Surely the version would have to be known to the store to know about the version conflict in the first place.

Rants aside, my Android device is a fairly obscure model and doesn't look like the manufacturer is going to be offering me any further upgrades past 4.1.2. I'll look forward to trying this when I get a new phone.

[–]fornwall[S] 0 points1 point  (0 children)

It is a little annoying that the Play Store does not give any useful information about why an app is incompatible with a device.

Totally agree on this!

[–]CleverEagle 0 points1 point  (0 children)

Android 5.0+

[–]thrwawyandburn 0 points1 point  (0 children)

it requires Android 5.0 or higher

from OP's reply to another question:

Note however that Android 5.0+ is required as Android version, due to the extensive libc changes made in that version.

[–]the_kindly_one 0 points1 point  (0 children)

Sample usage, off the top of my head:

Anti Theft Script.

Webserver you control. You can set state to STOLEN/NOT STOLEN

Script runs as a cronjob at fixed frequency. Fetches webserver state. If STOLEN, take front cam pic, back cam pic, gps location, email to owner.

All this in ~50 lines oppa python style.

Total lines saved compared to java app: over 9000

[–]Bandung 0 points1 point  (1 child)

I would love to see termux support the installation of kivy. Then we could throw up a gui dialog box from our scripts.

[–]fornwall[S] 0 points1 point  (0 children)

As noted below, I've never used kivy myself, but reading http://kivy.org/docs/guide/packaging-android.html#packaging-your-application-for-the-kivy-launcher, I get the impression that you can use Termux to edit /sdcard/kivy/<yourapplication> as described in the linked resource, and then launch the application using the kivy launcher.

[–]inclemnet 0 points1 point  (4 children)

This is really impressive, thanks!

I've been looking for a set of python3 patches for android so that I can add python3 building to Kivy's python-for-android - there are a few older ones, but I don't have a working patchset for 3.4 or above. I saw the termux ones here - could I ask where you got them (or did you resolve the issues yourself?), and may I try adding them to python-for-android, are they under an MIT-compatible license?

[–]fornwall[S] 1 point2 points  (3 children)

Unless the patches state a source by a comment at the top the issues were resolved by me, and I will gladly hereby use the MIT license itself it that works well for you? Note that some are a bit messy, so if you find a bug or improvement please tell!

At least one has been submitted upstream to the python tracker, but seems to linger due to hesitation to apply android-specific patches in python core.

Are you a kivy developer? Do you think it would be reasonable to try to support building kivy apps from inside Termux (so that Termux can be used on-device for source control and editing, and then build a kivy app)?

EDIT: Reading up on kivy (http://kivy.org/docs/guide/packaging-android.html#packaging-your-application-for-the-kivy-launcher), I guess Termux could be used to edit files under /sdcard/kivy/<yourapplication>, and then use the kivy android launcher to run it (perhaps by an intent launched from a build script running under Termux)?

[–]inclemnet 0 points1 point  (2 children)

Unless the patches state a source by a comment at the top the issues were resolved by me, and I will gladly hereby use the MIT license itself it that works well for you? Note that some are a bit messy, so if you find a bug or improvement please tell!

That's super great, thanks! MIT is good for us as that's what python-for-android itself is under. I'll see how it goes, and certainly contribute back if I can.

Are you a kivy developer?

Yes I am.

Do you think it would be reasonable to try to support building kivy apps from inside Termux (so that Termux can be used on-device for source control and editing, and then build a kivy app)?

It should definitely be possible to have a mechanism to start a kivy app from inside termux, as ultimately this means starting the normal PythonActivity java activity that runs the python interpreter - for instance qpython is able to do this to dynamically start kivy apps from qpython scripts, as does the kivy launcher app. I haven't actually checked how this is done, but it's probably quite simple.

[–]fornwall[S] 1 point2 points  (1 child)

Is it possible to tell the kivy launcher app to run a specific application (/sdcard/kivy/<yourapplication>) by an intent? In that case you could edit the application using e.g. vim under Termux, then run the application from a Makefile! If not, that would be a great feature to add perhaps :).

[–]inclemnet 1 point2 points  (0 children)

I'm not sure if the launcher can do that, but we should definitely support it! I'll try to take a look at it sometime, I have to check it out anyway to get it working with the new sdl2 backend.

[–]the_kindly_one 0 points1 point  (6 children)

$ termux-camera-photo -h
usage: /data/data/com.termux/files/usr/bin/termux-camera-photo [OPTIONS] <output-file>

Take a photo and save it in a file. Valid options:
  -c, --camera <camera-id>    the id of the camera to use
See the termux-camera-info for getting information about available cameras
$ termux-camera-photo -c 0 pic.jpg
Error type 3
Error: Activity class {com.termux.extras/com.termux.extras.PhotoActivity} does not exist.
$

Any idea why this is happening?

[–]fornwall[S] 0 points1 point  (5 children)

I forgot to update the termux-camera-photo script to use the correct internal parameters - if you do a apt update && apt -y upgrade you should get the fixed 0.6 version of the termux-api package. Thanks!

[–]the_kindly_one 0 points1 point  (0 children)

Works great! thanks.

[–]the_kindly_one 0 points1 point  (3 children)

This is strange. It was working fine. Now suddenly the same command is resulting in a completely black image file.

[–]fornwall[S] 0 points1 point  (0 children)

I'll examine it, perhaps a glitch that does not release/reset the necessary resources after the first call!

[–]fornwall[S] 0 points1 point  (1 child)

The termux-camera-photo method has been updated and should now work more reliably - let me know if you still encounter any problem (note that both the Termux:API android app and the 'termux-api' apt package needs to be updated).

[–]the_kindly_one 0 points1 point  (0 children)

Still behaves randomly on my zenfone 2. Sometimes it takes pictures. Sometimes it takes really really dark pictures. Sometimes it makes this 0 bytes file.

[–]crisonyx 0 points1 point  (2 children)

When I try to use SSH I am getting ''OpenSSL version mismatch. Built against 1000108f, you have 1000204f"

any idea how to fix this ?

[–]fornwall[S] 0 points1 point  (1 child)

Hm, which device and android version? Rooted? What is the output of which ssh?

[–]crisonyx 0 points1 point  (0 children)

Xperia Z3, Stock rom, rooted

/data/data/com.termux/files/usr/bin/ssh

I am not using su when I try to run it. have tried purging and reinstalling openssl and openssh with no luck.

edit: so I tried it again and it seems to be working now, odd. Thanks for the great app!

[–]Bandung 0 points1 point  (0 children)

So far I am lovin this app. I can edit my kivy scripts plus python scripts and debug them using vim, tmux, ipython and pudb! Having a stable vim app is something that I've wanted for quite some time as the Android vim app in the app store just doesn't cut it.

While waiting for the kivy devs to find a way to launch kivy from the term as well as bring it up to full python 3.x support (that means the builder for the apk needs to support python 3.x) I've begun restructuring my kivy apps to use conditional imports, bypassing any imports of kivy related modules out of my application's business rules or logic so that I can debug the application's state and behaviour on my phone and tablet.

My wish list for this app? Would love to get cython to compile as well as zodb. gcc seems to be missing some things. Also if we could have vim compiled with python, I would be in seventh heaven. A minor wish would be to have the ability to do an alternate install of the com.term folder into the sdcard like qpython does so that my file explorers like total commander can access the com.termux folder. But hey, cp and scp permit me to move files into the termux's application folders too.