I want to develop and test my project on the up-to-date version of Python 2.7 (say 2.7.18), but I want my project to be still fully usable on earlier versions of 2.7 (say 2.7.7). Setting up many variants of 2.7 locally or/and on CI for testing can be redundant.
So there are the following questions about compatibility of 2.7.X.
Can there be any changes in syntax which make code not working?
Can there be any changes in available standard imports, for example, can some imports from __future__ be unavailable in earlier versions?
Since I have to distribute compiled Python files (.pyc, compiled via py_compile module), I'm also wondering if there can be any changes in Python bytecode which block code execution in earlier versions.
I guess if all the answers are "no", I can develop and test my project only on a single 2.7 version without worries.
I've tried to search it but there is no success. Please share your experience and/or links.
UPD 1: I should have clearly said from the beginning that it's not my desire to use 2.7, it's a requirement from the environment.
At least Python 2.7.9 introduced massive changes to the 'ssl' module, so trying to use code using SSL for 2.7.18 on Python older than 2.7.9 will fail. So a clear "yes" to number 2.
In general compatbility for most projects works the other way round, use the oldest version you need to support and work upwards from old to new, not downwards from newer to older. I do not know of any software project that makes the guarantees in the other direction.
Note that Python 2.7 dropped out of support with 2.7.18, so unless you use a compatible version like PyPy (https://www.pypy.org/) your freshly developed project will run on outdated Python versions from the start.
If you want to provide a shrink wrapped product, maybe have a look at the usual solution for this like pyinstaller (https://www.pyinstaller.org/) or freeze (https://wiki.python.org/moin/Freeze)
The #3 may work, if you study the list of bytecode opcodes which do not change that much over time (https://github.com/python/cpython/commits/2.7/Include/opcode.h) but no idea if the on-disk format changed.
This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
We are working on an S60 version and this platform has a nice Python API..
However, there is nothing official about Python on Android, but since Jython exists, is there a way to let the snake and the robot work together??
One way is to use Kivy:
Open source Python library for rapid development of applications
that make use of innovative user interfaces, such as multi-touch apps.
Kivy runs on Linux, Windows, OS X, Android and iOS. You can run the same [python] code on all supported platforms.
Kivy Showcase app
There is also the new Android Scripting Environment (ASE/SL4A) project. It looks awesome, and it has some integration with native Android components.
Note: no longer under "active development", but some forks may be.
Yes! : Android Scripting Environment
An example via Matt Cutts via SL4A -- "here’s a barcode scanner written in six lines of Python code:
import android
droid = android.Android()
code = droid.scanBarcode()
isbn = int(code['result']['SCAN_RESULT'])
url = "http://books.google.com?q=%d" % isbn
droid.startActivity('android.intent.action.VIEW', url)
Pygame Subset for Android
Pygame is a 2D game engine for Python (on desktop) that is popular with new programmers. The Pygame Subset for Android describes itself as...
...a port of a subset of Pygame functionality to the Android platform. The goal of the project is to allow the creation of Android-specific games, and to ease the porting of games from PC-like platforms to Android.
The examples include a complete game packaged as an APK, which is pretty interesting.
As a Python lover and Android programmer, I'm sad to say this is not a good way to go. There are two problems:
One problem is that there is a lot more than just a programming language to the Android development tools. A lot of the Android graphics involve XML files to configure the display, similar to HTML. The built-in java objects are integrated with this XML layout, and it's a lot easier than writing your code to go from logic to bitmap.
The other problem is that the G1 (and probably other Android devices for the near future) are not that fast. 200 MHz processors and RAM is very limited. Even in Java, you have to do a decent amount of rewriting-to-avoid-more-object-creation if you want to make your app perfectly smooth. Python is going to be too slow for a while still on mobile devices.
Scripting Layer for Android
SL4A does what you want. You can easily install it directly onto your device from their site, and do not need root.
It supports a range of languages. Python is the most mature. By default, it uses Python 2.6, but there is a 3.2 port you can use instead. I have used that port for all kinds of things on a Galaxy S2 and it worked fine.
API
SL4A provides a port of their android library for each supported language. The library provides an interface to the underlying Android API through a single Android object.
from android import Android
droid = Android()
droid.ttsSpeak('hello world') # example using the text to speech facade
Each language has pretty much the same API. You can even use the JavaScript API inside webviews.
let droid = new Android();
droid.ttsSpeak("hello from js");
User Interfaces
For user interfaces, you have three options:
You can easily use the generic, native dialogues and menus through the
API. This is good for confirmation dialogues and other basic user inputs.
You can also open a webview from inside a Python script, then use HTML5
for the user interface. When you use webviews from Python, you can pass
messages back and forth, between the webview and the Python process that
spawned it. The UI will not be native, but it is still a good option to
have.
There is some support for native Android user interfaces, but I am not
sure how well it works; I just haven't ever used it.
You can mix options, so you can have a webview for the main interface, and still use native dialogues.
QPython
There is a third party project named QPython. It builds on SL4A, and throws in some other useful stuff.
QPython gives you a nicer UI to manage your installation, and includes a little, touchscreen code editor, a Python shell, and a PIP shell for package management. They also have a Python 3 port. Both versions are available from the Play Store, free of charge. QPython also bundles libraries from a bunch of Python on Android projects, including Kivy, so it is not just SL4A.
Note that QPython still develop their fork of SL4A (though, not much to be honest). The main SL4A project itself is pretty much dead.
Useful Links
SL4A Project (now on GitHub): https://github.com/damonkohler/sl4a
SL4A Python 3 Port: https://code.google.com/p/python-for-android/wiki/Python3
QPython Project: http://qpython.com
Learn SL4A (Tutorialspoint): https://www.tutorialspoint.com/sl4a/index.htm
Cross-Compilation & Ignifuga
My blog has instructions and a patch for cross compiling Python 2.7.2 for Android.
I've also open sourced Ignifuga, my 2D Game Engine. It's Python/SDL based, and it cross compiles for Android. Even if you don't use it for games, you might get useful ideas from the code or builder utility (named Schafer, after Tim... you know who).
Termux
You can use the Termux app, which provides a POSIX environment for Android, to install Python.
Note that apt install python will install Python3 on Termux. For Python2, you need to use apt install python2.
Some demos: https://www.youtube.com/watch?v=fqqsl72mASE
The GitHub project: https://github.com/termux
Kivy
I wanted to add to what #JohnMudd has written about Kivy. It has been years since the situation he described, and Kivy has evolved substantially.
The biggest selling point of Kivy, in my opinion, is its cross-platform compatibility. You can code and test everything using any desktop environment (Windows/*nix etc.), then package your app for a range of different platforms, including Android, iOS, MacOS and Windows (though apps often lack the native look and feel).
With Kivy's own KV language, you can code and build the GUI interface easily (it's just like Java XML, but rather than TextView etc., KV has its own ui.widgets for a similar translation), which is in my opinion quite easy to adopt.
Currently Buildozer and python-for-android are the most recommended tools to build and package your apps. I have tried them both and can firmly say that they make building Android apps with Python a breeze. Their guides are well documented too.
iOS is another big selling point of Kivy. You can use the same code base with few changes required via kivy-ios Homebrew tools, although Xcode is required for the build, before running on their devices (AFAIK the iOS Simulator in Xcode currently doesn't work for the x86-architecture build). There are also some dependency issues which must be manually compiled and fiddled around with in Xcode to have a successful build, but they wouldn't be too difficult to resolve and people in Kivy Google Group are really helpful too.
With all that being said, users with good Python knowledge should have no problem picking up the basics quickly.
If you are using Kivy for more serious projects, you may find existing modules unsatisfactory. There are some workable solutions though. With the (work in progress) pyjnius for Android, and pyobjus, users can now access Java/Objective-C classes to control some of the native APIs.
Using SL4A (which has already been mentioned by itself in other answers) you can run a full-blown web2py instance (other python web frameworks are likely candidates as well). SL4A doesn't allow you to do native UI components (buttons, scroll bars, and the like), but it does support WebViews. A WebView is basically nothing more than a striped down web browser pointed at a fixed address. I believe the native Gmail app uses a WebView instead of going the regular widget route.
This route would have some interesting features:
In the case of most python web frameworks, you could actually develop and test without using an android device or android emulator.
Whatever Python code you end up writing for the phone could also be put on a public webserver with very little (if any) modification.
You could take advantage of all of the crazy web stuff out there: query, HTML5, CSS3, etc.
Not at the moment and you would be lucky to get Jython to work soon. If you're planning to start your development now you would be better off with just sticking to Java for now on.
QPython
I use the QPython app. It's free and includes a code editor, an interactive interpreter and a package manager, allowing you to create and execute Python programs directly on your device.
Here are some tools listed in official python website
There is an app called QPython3 in playstore which can be used for both editing and running python script.
Playstore link
Another app called Termux in which you can install python using command
pkg install python
Playstore Link
If you want develop apps , there is Python Android Scripting Layer (SL4A) .
The Scripting Layer for Android, SL4A, is an open source application that allows programs written in a range of interpreted languages to run on Android. It also provides a high level API that allows these programs to interact with the Android device, making it easy to do stuff like accessing sensor data, sending an SMS, rendering user interfaces and so on.
You can also check PySide for Android, which is actually Python bindings for the Qt 4.
There's a platform called PyMob where apps can be written purely in Python and the compiler tool-flow (PyMob) converts them in native source codes for various platforms.
Also check python-for-android
python-for-android is an open source build tool to let you package Python code into standalone android APKs. These can be passed around, installed, or uploaded to marketplaces such as the Play Store just like any other Android app. This tool was originally developed for the Kivy cross-platform graphical framework, but now supports multiple bootstraps and can be easily extended to package other types of Python apps for Android.
Try Chaquopy
A Python SDK for Android
Anddd... BeeWare
BeeWare allows you to write your app in Python and release it on multiple platforms. No need to rewrite the app in multiple programming languages. It means no issues with build tools, environments, compatibility, etc.
From the Python for android site:
Python for android is a project to create your own Python distribution including the modules you want, and create an apk including python, libs, and your application.
Chaquopy
Chaquopy is a plugin for Android Studio's Gradle-based build system. It focuses on close integration with the standard Android development tools.
It provides complete APIs to call Java from Python or Python from Java, allowing the developer to use whichever language is best for each component of their app.
It can automatically download PyPI packages and build them into an app, including selected native packages such as NumPy.
It enables full access to all Android APIs from Python, including the native user interface toolkit (example pure-Python activity).
This used to be a commercial product, but it's now free and open-source.
(I am the creator of this product.)
Yet another attempt: https://code.google.com/p/android-python27/
This one embed directly the Python interpretter in your app apk.
You can run your Python code using sl4a. sl4a supports Python, Perl, JRuby, Lua, BeanShell, JavaScript, Tcl, and shell script.
You can learn sl4a Python Examples.
You can use QPython:
It has a Python Console, Editor, as well as Package Management / Installers
http://qpython.com/
It's an open source project with both Python 2 and Python 3 implementations. You can download the source and the Android .apk files directly from github.
QPython 2: https://github.com/qpython-android/qpython/releases
QPython 3: https://github.com/qpython-android/qpython3/releases
Another option if you are looking for 3.4.2 or newer (3.9.6 as of this writing) is this archive on GitHub.
Python3-Android 3.4.2 or Python3-Android 3.9.6
I believe the original archive supports Python 3.4.2, the latest GRRedwings branch support 3.9.6 and the 22b version of the NDK. Older branches support other versions, but are not as easy to compile with docker.
The older version you simply clone the archive, run make and you get the .so or the .a
The newer versions follow the ReadMe, but it uses docker for consistent builds.
I currently use this to run raw Python on android devices. With a couple modifications to the build files you can also make x86 and armeabi 64 bit
Take a look at BeeWare. It has grown significantly. It is awarded with PSF (Python Software Foundation) Education Grant.
Beeware's aim is to be able to create native apps with Python for all supported operating systems, including Android.
Official Website: Beeware
Github Repo: https://github.com/beeware
Didn't see this posted here, but you can do it with Pyside and Qt now that Qt works on Android thanks to Necessitas.
It seems like quite a kludge at the moment but could be a viable route eventually...
http://qt-project.org/wiki/PySide_for_Android_guide
One more option seems to be pyqtdeploy which citing the docs is:
a tool that, in conjunction with other tools provided with Qt, enables
the deployment of PyQt4 and PyQt5 applications written with Python
v2.7 or Python v3.3 or later. It supports deployment to desktop
platforms (Linux, Windows and OS X) and to mobile platforms (iOS and
Android).
According to Deploying PyQt5 application to Android via pyqtdeploy and Qt5 it is actively developed, although it is difficult to find examples of working Android apps or tutorial on how to cross-compile all the required libraries to Android. It is an interesting project to keep in mind though!
Check out enaml-native which takes the react-native concept and applies it to python.
It lets users build apps with native Android widgets and provides APIs to use android and java libraries from python.
It also integrates with android-studio and shares a few of react's nice dev features like code reloading and remote debugging.
Recently I was working on my Django web app, when I discovered that for some reason Django only worked when I used a python 2.x. Interpreter, but all the while I had thought it had been configured for Python 3 and thus was coding like so.
So I tested it with print(sys.version()) and was surprised when Python 2.7 came out. After a little digging I discovered that Django uses the six module. My first question is why does Django use this? Is there any reason other than just making it easier on the programmer? My second question is, since I've been treating it like Python 3 should I go back and change my code so that it is pure Python 2? Or does it not matter?
P.s. I kinda understand how six works, but it would be great to know a little more about it.
Thanks.
Django 1.5 is the first version of Django to support Python 3. The same code runs both on Python 2 (≥ 2.6.5) and Python 3 (≥ 3.2), thanks to the six compatibility layer.
Writing compatible code is much easier if you target Python ≥ 2.6. Django 1.5 introduces compatibility tools such as django.utils.six, which is a customized version of the six module. For convenience, forwards-compatible aliases were introduced in Django 1.4.2. If your application takes advantage of these tools, it will require Django ≥ 1.4.2.
For More read through Porting to Python 3
Which Python version your system uses has nothing whatsoever to do with the six module. That's something that Django uses internally to be able to work with both 2.7 and 3.x.
If you want to use Python 3 locally, you need to configure your system to do so. That might just mean creating your virtualenv with Python 3, for example.
I am going through a tutorial on building a website with django. It suggests using mod_python but I have heard to stay away from that and use wsgi instead. Problem is I am running python 3.3 (and apache 2.4.2 for that matter, everything seems to be compatible with apache 2.2). Is there any way to get all of this working on 3.3? Or is my best bet to go back to python 2.7? Thanks.
Edit: I am on Windows, so that seems to be another roadblock.
You could use nginx + uwsgi to depoly your django site instead of Apache+mod_wsgi. Here's a tutorial.
As many tutorials is about how to configure the environment in Unix-like environment, you could use cgywin to simulate a Unix-like environment on Windows.
The version of Python you use is not much critical when you develop a site using Django except that you have to use some libraries that don't support Python-3.x.
It will be hard, between python2 and python3 there is a lot of incompatibility, and somehow the developers of the most python frameworks somehow won't understand, why they should port their software to the newer version of the language.
The simplest way if you use python 2.
The best way were to start an independent, python 3 fork of your most loved python framework.
EDIT: newer django supports python3, thus it should work.
I've been looking really hard at all of the way**(s)** one can develop web applications using Python. For reference, we are using RHEL 64bit, apache, mod_wsgi.
History:
PHP + MySQL years ago
PHP + Python 2.x + MySQL recently and current
Python + PostgreSQL working on it
We use a great library for communicating between PHP and Python (interface in PHP, backend in Python)... However, with a larger upcoming project starting, using 100% python may be very advantagous.
We typically prefer not to have a monolithic framework dictating how things are done. A collection of useful helpers and utilities are much preferred (be it PHP or Python).
Question 1:
In reading a number of answers from experienced Python users, I've seen Werkzeug recommended a number of times. I would love it if several people with direct experience using Werkzeug to develop professional web applications could comment (in as much detail as their fingers feel like) why they use it, why they like it, and anything to watch out for.
Question 2:
Is there a version of Werkzeug that supports Python 3.1.1. I've succefully installed mod_wsgi on Apache 2.2 with Python 3.1.1.
If there is not a version, what would it take to upgrade it to work on Python 3.1?
Note: I've run 2to3 on the Werkzeug source code, and it does python-compile without
Edit:
The project that we are starting is not slated to be finished until nearly a year from now. At which point, I'm guessing Python 3.X will be a lot more mainstream. Furthermore, considering that we are running the App (not distributing it), can anyone comment on the viability of bashing through some of the Python 3 issues now, so that when a year from now arrives, we are more-or-less already there?
Thoughts appreciated!
mod_wsgi for Python 3.x is also not ready. There is no satisfactory definition of WSGI for Python 3.x yet; the WEB-SIG are still bashing out the issues. mod_wsgi targets a guess at what might be in it, but there are very likely to be changes to both the spec and to standard libraries. Any web application you write today in Python 3.1 is likely to break in the future.
It's a bit of a shambles. Today, for webapps you can only realistically use Python 2.x.
I haven't used Werkzeug, so I can only answer question 2:
No, Werkzeug does not work on Python 3. In fact, very little works on Python 3 as of today. Porting is not difficult, but you can't port until all your third-party libraries have been ported, so progress is slow.
One big stopper has been setuptools, which is a very popular package to use. Setuptools is unmaintained, but there is a maintained fork called Distribute. Distribute was released with Python 3 support just a week or two ago. I hope package support for Python 3 will pick up now. But it will still be a long time, at least months probably a year or so, before any major project like Werkzeug will be ported to Python 3.
I can only answer question one:
I started using it for some small webstuff but now moved on to rework larger apps with it. Why Werkzeug? The modular concept is really helpful. You can hook in modules as you like, make stuff easily context aware and you get good request file handling for free which is able to cope with 300mb+ files by not storing it in memory.
Disadvantages... Well sometimes modularity needs some upfront thought (django f.ex. gives you everything all at once, stripping stuff out is hard to do there though) but for me it works fine.