What are my options (if any) for MySQL with Python 3? - python

I recently starting playing around with the new features of Python 3.1 and porting one of my 2.6 apps. I was surprised to find that MySQLdb does not yet support any of the 3.x versions of Python. My app uses MySQL extensively, so, as you can imagine, I didn't get too far!
What are my options (if any) for working with MySQL and Python 3.1? I've been out of the Python 3k loop, but cursory search did not yield any evidence of a release date for 3.1 support in MySQLdb or any other alternatives.

mypysql doesn't follow the Python DB API standard, but does support Mysql and Python 3.

You could use pymysql. "The goal of PyMySQL is to be a drop-in replacement for MySQLdb". Check the docs here.
pip install mysqlclient pymysql
Once these libraries are installed, just add the lines in your project wherever MySQLdb is used.
import pymysql
pymysql.install_as_MySQLdb()

Related

Unable to install mysqldb on python

I am trying to install mysqldb for python on my windows PC but come across this error: (Note I know I am using an outdated version of python but this is the version I need to use for now)
I don't know if you need that specific module/package. But you can get the official mysql-connector for python by specifying this package:
mysql-connector-python-rf
Works with both Python 2, and 3. (link)
The problem is more so that MySQL-Python does not support any version of Python 3. You should look for an alternative connector library. The error message does not mean anything, only that in the past MySQL-Python at least tried to support 3.0 - 3.2 (before 3.3 was ever released), however they gave up.
Unfortunately, I personally am not using MySQL so I cannot comment about the alternatives; however this question could be of use.

Can't change system default SQLite binary

Using Django and SQLite I want to run most recent SQLite version; most recent SQLite binary, not the SQLite Python library. I have an SQLite binary that is not the system default and can't change the default version.
I'm not using Django's ORM but replaced it with a standalone SQLAlchemy version. Related (but has to do with running the most recent Python SQLite library).
The simplest option if you just need a recent version of SQLite from python is now to install the pysqlite3 package:
pip install pysqlite3-binary
This comes with a recent version of SQLite statically-linked. You can use it in a venv without affecting any other package.
You can use it like this:
from pysqlite3 import dbapi2 as sqlite3
print(sqlite3.sqlite_version)
If you happen to be using peewee, it will pick it up automatically.
If you need a specific version of SQLite, you can build this package, as suggested in Aaron Digulla's answer.
Python can't use the sqlite3 binary directly. It always uses a module which is linked against the sqlite3 shared library. That means you have to follow the instructions in "How to upgrade sqlite3 in python 2.7.3 inside a virtualenv?" to create a version of the pysqlite module in your virtualenv.
You can then use this import
from pysqlite2 import dbapi2 as sqlite
to shadow the system's default sqlite module with the new one.
Another option would be to get Python's source code, compile everything and copy the file sqlite.so into your virtualenv. The drawback of this approach is that it's brittle and hard to repeat by other people.
Try this easy route first if you're on windows: grab the dll from the sqlite download page (it will be under the "Precompiled Binaries for Windows" heading) and add it to Anaconda's dll path (like C:\Users\YourUserName\Anaconda3\DLLs). The new versions there come with goodies like FTS5 already enabled.
If you're on Linux, then refer to Install Python and Sqlite from Source and Compiling SQLite for use with Python Applications

pyodbc and python 3.4 on Windows

pyodbc is a very nice thing, but the Windows installers only work with their very specific python version. With the release of Python 3.4, the only available installers just stop once they don't see 3.3 in the registry (though 3.4 is certainly there).
Copying the .pyd and .egg-info files from a 3.3 installation into the 3.4 site-packages directory doesn't seem to do the trick. When importing pyodbc, an ImportError is thrown: ImportError: DLL load failed: %1 is not a valid Win32 application.
Is there a secret sauce that can be added to make the 3.3 file work correctly? Or do we just need to wait for a 3.4 installer version?
The different versions of Python are (for the most part) not binary-compatible, and thus any compiled extensions (such as pyodbc) will only work for a specific version.
Note that pure-Python packages (the ones that are completely written in Python, and have no non-Python dependencies) do not need to be compiled, and thus can be written to support multiple Python versions.
Also note that it is technically possible for a compiled extension to be written such that it works for Python 3.2 as well as 3.3, 3.4, and the future 3.x's to come, but they have to limit themselves to the "stable ABI" as specified by PEP 384, and most extensions do not do this. As far as I know, pyodbc is not limited to the stable ABI and must be compiled separately for each Python version.
That said, it is also possible to compile your own version of pyodbc from source, as long as you have the required tools and expertise. (But I'm guessing if you're asking this question, you don't. I don't either, otherwise I'd include some tips in this answer.)
As you have already commented, pypyodbc may be your best bet, as it is a pure-Python package.
Installing pypyodbc can be done via the commandline:
C:\Python34\Scripts>pip install pypyodbc
Using it as drop-in replacement of pyodbc can be done using:
import pypyodbc as pyodbc
[The current version of pyodbc at the time of this edit is 3.0.10, and it does support Python 3.4. Of course, it's still useful to be aware of pypyodbc in case pyodbc falls behind again when future versions of Python are released.]
Did you try to download from here? It has an unofficial build for 3.4. I did a quick test myself, looks like it's working fine for me.
I fixed this by installing pyodbc 3.0.10. The latest version of pyodbc didn't work on Windows with Python 3.4
However pyodbc 3.0.10 did work for me
Install command on command prompt : pip install pyodbc 3.0.10

What was the first version of Python that included SQLite?

What was the first version of Python that included SQLite?
What version of SQLite was included?
I thought Python 2.5 was the first version to include SQLite, but I was hoping someone could confirm that and the version of SQLite that was first included.
From the docs:
11.13. sqlite3 — DB-API 2.0 interface for SQLite databases
New in version 2.5.
As others have noted, a snapshot of the pysqlite2 was merged into the Python standard library and released as the sqlite3 module in Python 2.5. There has been subsequent independent development on pysqlite2 which has been merged back into sqlite3 at various points for subsequent Python releases and sqlite3 has received its own bug fixes.
There is no one correct answer to the question of which version of SQLite was included because the Python source code distribution does not include the source for SQLite. The build process, for Unix-style builds, depends on an externally provided copy of the library. It is up to each distributor of Python to decide how to manage that. For instance, the current python.org installer 32-bit-only variants for Mac OS X statically includes a version of the SQLite library while the 64-bit variant for 10.6 dynamically links to the system-supplied version of the library. So, to answer your second question, you would need to check each distribution of Python 2.5 and determine what version(s) of SQLite each was statically or dynamically linked with.
It was first included in python version 2.5.
The version of sqlite was 3.0.8, based off of the pysqlite2.1.3 library.
Found here: http://www.python.org/download/releases/2.5/NEWS.txt (search for "sql").
From here, Version 2.5 alpha 1

Problem compiling MySQLdb for Python 2.6 on Win32

I'm using Django and Python 2.6, and I want to grow my application using a MySQL backend. Problem is that there isn't a win32 package for MySQLdb on Python 2.6.
Now I'm no hacker, but I thought I might compile it myself using MSVC++9 Express. But I run into a problem that the compiler quickly can't find config_win.h, which I assume is a header file for MySQL so that the MySQLdb package can know what calls it can make into MySQL.
Am I right? And if so, where do I get the header files for MySQL?
Thanks all! I found that I hadn't installed the developer components in MySQL. Once that was done the problem was solved and I easily compiled the MySQLdb for Python 2.6.
I've made the package available at my site.
I think that the header files are shipped with MySQL, just make sure you check the appropriate options when installing (I think that sources and headers are under "developer components" in the installation dialog).
Have you considered using a pre-built stack with Python, MySQL, Apache, etc.?
For example: http://bitnami.org/stack/djangostack
Also see this post on the mysql-python blog: MySQL-python-1.2.3 beta 2 released - dated March 2009. MySQLdb for Python 2.6 is still a work in progress...

Categories