top of page

09.20.2018: Running a Python program on an Android phone and syncing it with PC


Today's soundtrack is Connie Francis: Rocksides. I heard her song "My Best Friend Barbara" being played when I visited Nuggets Used Books in Chilliwack, and was immediately enthralled with the classic sound. It's a really fun album.


This evening, I'm revisiting the Randomizer to try to figure out whether I can get it to run on my Android phone. I'm going to be attempting this on my LG G5 running Android Oreo 8.0.0.


Now, because my current version runs in the Tkinter GUI, I'm expecting that I'll need to make a new version just for Android that runs without using that interface, but we'll see what happens.

 

Attempt 1: Running the program as-is


I copied Randomizer.pyw along with its associated text documents (Books.txt and Albums.txt) into Google Drive, then downloaded them to my phone. I realized that I'd tried copying the shortcuts of the files, so they downloaded with a .lnk extension. So I tried again, this time copying the actual files. I clicked on the file on my phone, and it gave me an option to open the file in either Chrome, Docs, or something called HTML viewer. None of those options did anything. Using my phone's file manager, I dropped the three files into a new folder called "Randomizer." I gave one last attempt at opening the file without any extra programs: I tried opening it through the file manager, rather than the downloads directory. The file manager, however, was even less helpful; it simply said "File type is not supported."

 

Attempt 2: Using a Python IDE app to open the original file


I searched the Play Store for "Python IDE," and the top result was a promising-looking port of Python 3 called "QPython3 - Python3 for Android," a 12.02mb app. I downloaded it and opened it up, and was presented with nine options: Console, Editor, Programs, QPYPI, course [sic], and Community. I tapped the hamburger bar to see whether it gave an option to open an existing Python file, and was presented the option to download the newer beta version of the app, QPy3.6. I downloaded that version and opened it up. It greeted me with a screen that said "INSTALL QPYTHON FIRST." I clicked the button and was given an option to open it with either Chrome or Qpython3. I chose the latter. It said "Python script is running," then sent me back to the "INSTALL QPYTHON FIRST" screen.


Okay.


So I uninstalled Qpy3.6, and re-opened QPython3 back up. I opened the editor and tried to navigate to my "Randomizer" folder; however, it limited me to folders within the predesignated Qpython folder set. This might mean that I need to try moving my folder within the app's folder, but I decided to try opening the app from within the file manager instead, thinking that maybe now it would give me the option to open it with Qpython3. Since the file manager didn't give the option to choose how to open a file, I moved Randomizer.pyw, Albums.txt, and Books.txt back to the downloads folder. Once again, I was presented with the same three options as before: Chrome, HTML Viewer, or Docs. So I used the file manager to move the three files into the Qpython folder section and once again tried to use the Qpython3 editor to open the randomizer.pyw file. I found it, opened it, and tried to run it, but got an error message saying "QEdit only supports to preview/execute the following file types .py, .sh, .ua, .html, .md." I went back to IDLE on my computer and created a copy of my program with the .py extension, sent it to my phone, and tried again. I got another error; this time, it said "No module named _tkinter." Ah, I thought that might come back to bite me. Before modifying my program to not rely on tkinter, though, I ran a Google search to see if there was a way to install modules in Qpy3.6. On StackOverflow, user Aashish said that using an app called AIPY would allow users to install modules in Qpy3.6. I downloaded it and was met with the familiar screen saying "INSTALL QPYTHON FIRST." I learned that this app is only compatible with the Qpy2.7 app. The developers said that they were working making AIPY compatible with QPy3.6 in December of 2017, but that doesn't help me much now. Back to the drawing board.

 

Attempt 3: Using Qpython3 to open the modified app without tkinter running


I went back to Python's IDLE editor on my computer and opened up my Randomizer.py file. To make it run on QPy3.6, I needed to remove its reliance on tkinter. The funny thing is that my first version of this program was cobbled together with bits and pieces that I learned from DK's Coding Projects in Python, and that version didn't use tkinter. Unfortunately, I didn't keep that original file when I upgraded my operating system to Windows 10 a few months back. Oh well. It was fun revisiting the basics of Python, and it only took me about 15 minutes to do it. I saved the updated version as "randomizerandroid.py," put it on my phone in the Qpy3.6 app, and gave it a try. I got an error: "No such file or directory: 'albums.txt'." Hmm. I Googled the error code I got, and found a thread on StackOverflow where another user encountered a similar problem. It wasn't exactly the same problem, but I was able to figure out that my problem was that QPy3.6, unlike Python on the computer, doesn't simply recognize an unspecified location of a file as being within the same folder where the program is running from. So I entered the directory and...


IT WORKED!


It isn't pretty, but that's the least of my concerns. What has me really excited right now is that it does work, and I'm no longer tied to my computer to use the Randomizer.

 

The final step in my project was to sync my two text files from my computer to my phone. Following this tutorial on Online Tech Tips, I set up a symbolic link from my computer's Randomizer folder to a folder in OneDrive, then used FolderSync to sync that OneDrive folder onto my phone. Finally, I changed the Python program's directories for the albums accordingly. This way, if I come across an album while out and about and want to add it to my Randomizer's list, I can do it right from my phone. Similarly, if I finish a book, I can remove it from the list on my phone. In both instances, my PC's respective lists will be updated accordingly on the next sync.

bottom of page