Python and MySQL - python

I can get Python to work with Postgresql but I cannot get it to work with MySQL. The main problem is that on the shared hosting account I have I do not have the ability to install things such as Django or PySQL, I generally fail when installing them on my computer so maybe it's good I can't install on the host.
I found bpgsql really good because it does not require an install, it's a single file that I can look at, read and then call the functions of. Does anybody know of something like this for MySQL?

MySQLdb is what I have used before.
If you host is using Python version 2.5 or higher, support for sqlite3 databases is built in (sqlite allows you to have a relational database that is simply a file in your filesystem). But buyer beware, sqlite is not suited for production, so it may depend what you are trying to do with it.
Another option may be to call your host and complain, or change hosts. Honestly these days, any self respecting web host that supports python and mysql ought to have MySQLdb pre installed.

I don't have any experience with http://www.SiteGround.com as a web host personally.
This is just a guess, but it's common for a shared host to support Python and MySQL with the MySQLdb module (e.g., GoDaddy does this). Try the following CGI script to see if MySQLdb is installed.
#!/usr/bin/python
module_name = 'MySQLdb'
head = '''Content-Type: text/html
%s is ''' % module_name
try:
__import__(module_name)
print head + 'installed'
except ImportError:
print head + 'not installed'

I uploaded it and got an internal error
Premature end of script headers
After much playing around, I found that if I had
import cgi
import cgitb; cgitb.enable()
import MySQLdb
It would give me a much more useful answer and say that it was not installed, you can see it yourself -> http://woarl.com/db.py
Oddly enough, this would produce an error
import MySQLdb
import cgi
import cgitb; cgitb.enable()
I looked at some of the other files I had up there and it seems that library was one of the ones I had already tried.

You could try setting up your own python installation using Virtual Python. Check out how to setup Django using it here. That was written a long time ago, but it shows how I got MySQLdb setup without having root access or anything like it. Once you've got the basics going, you can install any python library you want.

You really want MySQLdb for any MySQL + Python code. However, you shouldn't need root access or anything to use it. You can build/install it in a user directory (~/lib/python2.x/site-packages), and just add that to your PYTHON_PATH env variable. This should work for just about any python library.
Give it a shot, there really isn't a good alternative.

Take a pick at
https://docs.djangoproject.com/en/1.8/ref/databases/
MySQLdb is mostly used driver, but if you are using python3 and django 1.8.x that will not work, then you should use mysqlclient that is a folk of MySQLdb on the following link
https://pypi.python.org/pypi/mysqlclient

Related

Python - pysqlite or sqlite3 must be installed

I have python 2.7.12 installed on my server. I'm using PuTTY to connect to my server. When running my python script I get the following.
File "home/myuser/python/lib/python2.7/site-packages/peewee.py", line 3657, in _connect
raise ImproperlyConfigured('pysqlite or sqlite3 must be installed.')
peewee.ImproperlyConfigured: pysqlite or sqlite3 must be installed.
I thought sqlite was installed with python 2.7.12, so I'm assuming the issue is something else. Haven't managed to find any posts on here yet that have been helpful.
I am missing something?
Thanks in advance
Peewee will use either the standard library sqlite3 module or, if you did not compile Python with SQLite, Peewee will look for pysqlite2.
The problem is most definitely not with Peewee on this one, as Peewee requires a SQLite driver to use the SqliteDatabase class... If that driver does not exist, then you need to install it.

MySQLdb import error on windows 7 + Django

I installed python 2.6 and MySQL-python-1.2.2.win32-py2.6 and created a new project using the command django-admin.py startproject mysite and it successfully created the project and I could run it.But when I edit mysite/settings.py file to use MySQLdb and run it again,it says
Validating models...
Unhandled exception in thread started by bound method Command.inner_run of dja
ngo.contrib.staticfiles.management.commands.runserver.Command object at 0x02BFCB
raise ImproperlyConfigured(Error loading MySQLdb module:)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: DLL l
oad failed: The specified module could not be found.
I tried searching all net and couldn't identify the problem.Is it to do with setting some environment variables? Can anyone please help me about this?
If you fire up regular python and try
>>> import MySQLdb
and then fire up the django shell
> python manage.py shell
>>> import MySQLdb
and get the same error, I would presume that you have the wrong SQL package for your system - this is often a 32bit vs 64bit issue.
Dig out the actual SQL dll (called _mysql.pyd and see if you can import that directly into pyton (go to the directory containing the dll, fire up python and import _mysql). This should help speed up the diagnosis.
Alternatively, I've always found Bitnami's django stack to be the least painful way to install and manage my Windows django stack. It will set up and manage Apache HTTP Server, MySQL, Python, SQLite, PostgreSQL, Django all in one go.

Why can I import a library from the Django shell, but not inside my site?

I am trying to use the oauth2app library inside my Django app. I've tried installing the library several ways (easy_install, pip, pip via a requirements file, etc.), and every time it installs just fine. Also every time I can import the library from the Django shell (manage.py shell).
However, when I try to use a view from the library:
(r'^oauth2/token/?$', 'oauth2app.token.handler'),
I get a "No module named oauth2app" import error. I've tried comparing the Python path from the Django debug page to the one from "print sys.path" inside the shell, and the seem to be the same, so I can't for the life of me figure out why one works and the other doesn't.
Can anyone help explain what's going on? I thought the Django shell was an equivalent environment to the Django instance ...
Nevermind, it looks like this was just a case of bad documentation on the library's site; when I changed the urls.py line to:
from oauth2app import token
...
(r'^oauth2/token/?$', token.handler),
it worked, sigh.

python validate Oracle username/passwords without using cx_Oracle

I need to validate that user provided data for oracle account information is correct. I tried to use cx_Oracle but the version of OCI.DLL that is on my servers (which I can't upgrade) seems to not be the correct version for cx_Oracle.
How can I validate username/passwords without using cx_Oracle?
If you can't compile cx_Oracle, you have two options:
use ODBC (for example PyODBC);
use subprocess to interact with sqlplus.
If you have access to the username & password hashes through some alternate means that doesn't require cx_Oracle, and simply need to verify the password hashes, the Passlib python library may be able to help. It supports both oracle10g and oracle11g hash formats.
The problem is most likely the version of your cx_Oracle module.
You have to be extremely careful when you install the module, to choose the appropriate version for your oracle installation.
http://cx-oracle.sourceforge.net/
If you can't connect to the database using cx_Oracle, you might have a hard time checking the accuracy of the information
If you have sql*plus installed, you might try to start a process using the subprocess module and check if you can connect, but in my opinion, you might be better of with fixing cx_Oracle
Edit: Meant the subprocess module.
Here is how can you do it with subprocess:
import subprocess
def is_login_valid(user, password, instance):
output = subprocess.check_output("sqlplus %s/%s#%s" %(user, password, instance))
return (output.find("ORA-01017") == -1 and output.find("ORA-12154") == -1)

Accessing a MySQL database from python

I have been trying for the past several hours to find a working method of accessing a mysql database in python. The only thing that I've managed to get to compile and install is pyodbc but the necessary driver is not available for ppc leopard.
I already know about this.
UPDATE:
I've gotten setuptools to install, but now MySQL-python won't build.
UPDATE:
Now I've gotten sqlalchemy to install but while it will show up when called by the command line it won't import when used in my cgi script.
Try SQL Alchemy.
It is awesome.
Install fink. It includes the MySQLdb package.
UPDATE: Now I've gotten sqlalchemy to
install but while it will show up when
called by the command line it won't
import when used in my cgi script.
Can you verify that the Python being invoked from your CGI script is the same as the one you get when you run Python interactively? Check which python and compare it to your webserver CGI settings. That's the only thing I can think of that would cause this - getting it installed in one Python but not the other.
What OS are you on? If you're on something like Ubuntu, sudo apt-get install python-mysqldb is much more reliable than trying to build it yourself.
Also, unless I'm mistaken, SQLAlchemy won't actually help you make the connection itself if you don't have a DB-API2 module (like python-mysqldb) installed - SQLAlchemy sits at the next level up, using the DB-API2 connection and making access to it more Pythonic.

Categories

Resources