ImportError: No module named MySQLdb - Debian 6.0 - python

I got a problem in Python while try to import mysql. The beginning of my code is (I'm using monkeyrunner to run script) :
#!/usr/bin/env python
import sys
import MySQLdb
.
.
.
etc
Each time I try to run the code, I got the current error:
dani#debian:~/public/Yandex Maps Test$ monkeyrunner test.py
130413 00:49:22.066:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
130413 00:49:22.066:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "/home/dani/public/Yandex Maps Test/test.py", line 4, in <module>
import MySQLdb
ImportError: No module named MySQLdb
130413 00:49:22.066:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] at org.python.core.PyException.fillInStackTrace(PyException.java:70)
.
.
.
etc
I tried to re-install mysql for python:
sudo apt-get reinstall python-mysqldb mysql-server
But nothing helped. Any ideas or tips?
Regards,,,

The problem is that monkeyrunner isn't the same interpreter as your default system Python, and doesn't share the same site-packages, etc.
That #!/usr/bin/env python at the top of your script is misleading. If you actually run your script with /usr/bin/env python test.py, or just ./test.py (assuming chmod +x), it will work. But you're not, you're running it with monkeyrunner.
So, nothing you install for your system Python is going to be usable within monkeyrunner. You have to install it for monkeyrunner instead.
Moreover, as the docs say:
The monkeyrunner tool uses Jython, a implementation of Python that uses the Java programming language.
While Jython is Python, and implements the same Python language and (almost) standard library as CPython, it doesn't implement the CPython C API, and therefore can't use C-API extensions like MySQLdb. (They're actually working on a clever wrapper that will change this in the future, but that doesn't help you today.)
There are other MySQL drivers that work with both CPython and Jython (because they use pure Python), such as PyMySQL. But the simplest solution is to just use Java's JDBC drivers. As the Jython FAQ suggests, the easiest way to do that is:
Use zxJDBC which gives data database connectivity from Jython using the Python DB API 2.0 interface…
Unlike using JDBC directly, using zxJDBC often means you only need to change one line in your MySQLdb code to make it work.

i have the same question with u.
and i tried to use zxJDBC to fix it.
but when i run the script, it cannot find "org.gjt.mm.mysql.Driver" either "com.mysql.jdbc.Driver". Even i put mysql-connector-java-5.1.34-bin.jar and zxJDBC.jar under system CLASSPATH.
i tried run this script under jython. it worked.
so i think this question is related with monkeyrunner.

Related

ImportError: No module named stdio, any way to start fresh with python?

I'm getting the error
Traceback (most recent call last):
File "ghs.py", line 1, in <module>
import stdio
ImportError: No module named stdio
When I try to run my script. I can run my script on other machines just fine. I have installed python using homebrew. And I've tried everything I can think of to get it to recognize my modules! I've uninstalled and reinstalled using brew. I've tried changing the path (though I don't fully understand this). I get no issues using brew doctor.
I've also tried using a python virtual environment but to no avail.
Any ideas on how to fix this issue or else 'start fresh' from a fresh version of python?
When you import a module, Python looks for it at the directory your code is, and the directory in which the built-in libraries are (C:\Users\pc\AppData\Local\Programs\Python\Python35-32\Lib in my case, I'm using Windows 10 and Python 3.5). If it can't find it, it raises ImportError.
I couldn't find a module named stdio in my computer. I also know some C++ and as far as I know, stdio is the library for inputs and outputs(prints). In python, there is no need to import such a library.
You can use try,except statement to test if your code works without importing the module like this.
try:
import stdio
except:
#rest of your code goes here
You will need to indent your whole code however this can be done easily with a text editor in which you can edit more than one line at a time.

Can't get MySQLDb to work in python on Mac is there other easier DB?

I am looking for a production database to use with python/django for web development. I've installed MySQL successfully. I believe the python connector is not working and I don't know how to make it work. Please point me in the right direction. Thanks.
If I try importing MySQLdb:
import MySQLdb
I get the following exception.
Traceback (most recent call last):
File "/Users/vantran/tutorial/scrape_yf/mysql.py", line 3, in <module>
import MySQLdb
ImportError: No module named MySQLdb
I've tried using MySQL but I am struggling with getting the connector package to install or work properly. http://dev.mysql.com/downloads/file.php?id=414340
I've also tried to look at the other SO questions regarding installing MySQL python connectors, but they all seem to be unnecessarily complicated.
I've also tried
http://www.tutorialspoint.com/python/python_database_access.htm
http://zetcode.com/db/mysqlpython/
https://github.com/PyMySQL/PyMySQL
...but nothing seems to work.
If your problem is with the MySQLdb module, not the MySQL server itself, you might want to consider PyMySQL instead. It's much simpler to set up. Of course it's also somewhat different.
The key difference is that it's a pure Python implementation of the MySQL protocol, not a wrapper around libmysql. So it has minimal requirements, but in a few use cases it may not be as performant. Also, since they're completely different libraries, there are a few rare things that one supports but not the other, and various things that they support differently. (For example, MySQLdb handles all MySQL warnings as Python warnings; PyMySQL handles them as information for you to process.)
I would recommend postgres.app : http://postgresapp.com
Tried and never left
My preference for the driver is http://initd.org/psycopg/
You'll find a list of drivers at http://wiki.postgresql.org/wiki/Python

sys.path.append doesn't work with Python 3.x

I have Blender 2.66a which is an application that offers Python 3.3 APIs, On my system I have an installation of Python 3.2 with several modules that I wish to use inside Blender, I tried both
sys.path.append(r"/usr/lib/python3.2/")
sys.path.append("/usr/lib/python3.2/")
and this commands gives no errors, infact even the autocomplete feature works and new modules are indexed, so i tried
import tkinter
but this generates the following error
Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
File "/usr/lib/python3.2/tkinter/__init__.py", line 42, in <module>
raise ImportError(str(msg) + ', please install the python-tk package')
ImportError: No module named '_tkinter', please install the python-tk package
and I don't get the point of this error because it fails to load a module that it's there asking me to install the same module because that module is not installed ( ? ).
What can cause this obscure problem ?
EDIT
the tkinter module works from the gnome-terminal
If I got you right, you're using Python 3.3 from Blender but try to include the 3.2 standard library. This is bound to give you a flurry of issues, you should not do that. Find another way. It's likely that Blender offers a way to use the 3.3 standard library (and that's 99% compatible with 3.2). Pure-Python third party library can, of course, be included by fiddling with sys.path.
The specific issue you're seeing now is likely caused by the version difference. As people have pointed out in the comments, Python 3.3 doesn't find the _tkinter extension module. Although it is present (as it works from Python 3.2), it is most likely in a .so file with an ABI tag that is incompatible with Blender's Python 3.3, hence it won't even look at it (much like a module.txt is not considered for import module). This is a good thing. Extension modules are highly version-specific, slight ABI mismatches (such as between 3.2 and 3.3, or two 3.3 compiled with different options) can cause pretty much any kind of error, from crashes to memory leaks to silent data corruption or even something completely different.
You can verify whether this is the case via import _tkinter; print(_tkinter.__file__) in the 3.2 shell. Alternatively, _tkinter may live in a different directory entirely. Adding that directory won't actually fix the real issue outlined above.

connecting Python 2.6.1 with MySQLdb

I am using Python 2.6.1 and I want to connect to MySQLdb, I installed mySQL in my system, and I am trying to connect MySQL-python-1.2.2.win32-py2.6 from http://www.codegood.com/archives/4 site but its not working
while running my application its saying that No module named MySQLdb
please any one provide me the proper setup for MySQLdb.
thanks in advance
The best setup for Windows that I've found:
http://www.codegood.com/downloads?dl_cat=2
EDIT: Removed original link (it's an ad farm now :( )
The module is not likely in your python search path..
Check to see if that module is in your Python Path... In windows...you may find it in the registry
HKLM\Software\Python\PythonCore\2.6\PythonPath
Be careful editing it...
You may also alter the Python Path programmaticly by the following
import sys
sys.path.append('somepath_to_the_module_you_wanted')
import the_module_you_wanted
Hope that helps
I was having this problem and then I realised I was importing MySQLdb erroneously - it's case sensitive:
Incorrect: >>>import mysqldb
Correct: >>>import MySQLdb
Silly mistake, but cost me a few hours!
generally, (good) python modules provide a 'setup.py' script that takes care of things like proper installation (google for 'distutils python'). MySQLdb is a "good" module in this sense.
since you're using windows, things might be a bit more complex. I assume you already installed MySQLdb following the instructions and it still gives this problem. what I would do is open a cmd.exe window, cd to the directory containing the 'setup.py' script and there type something like
C:\Python26\Python.exe setup.py install
if this does not work, then grab the module somewhere else, maybe at the place where it is actively developed: http://sourceforge.net/projects/mysql-python/
See this post on the mysql-python blog: MySQL-python-1.2.3 beta 2 released - dated March 2009. Looks like MySQLdb for Python 2.6 is still a work in progress...
I went for compiled binary , thats the best way to go on windows. There is a good source maintained by someone.
I wrote about it here before because some months down the lane I will forget how I solved this and be searching Stack again :/
http://vangel.3ezy.com/archives/101-Python-2.4-2.5-2.6-and-2.7-Windows-MySQLdb-python-installation.html

How can I tell python which version of libmysqlclient.so to use?

I'm running a python script on a shared hosting server which until this morning had MySQL version 4. Now it has version 5. My python script can no longer connect to MySQL, as it can't find libmysqlclient_r.so.14:
$ python my_script.py
Traceback (most recent call last):
File "my_script.py", line 6, in ?
import MySQLdb
File "/home/lib/python2.4/site-packages/PIL-1.1.6-py2.4-linux-i686.egg/__init__.py", line 19, in ?
File "build/bdist.linux-i686/egg/_mysql.py", line 7, in ?
File "build/bdist.linux-i686/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient_r.so.14: cannot open shared object file: No such file or directory
There are various other versions of libmysqlclient in /usr/lib:
/usr/lib/libmysqlclient.so.15
/usr/lib/libmysqlclient.so.14
/usr/lib/mysql/libmysqlclient.la
/usr/lib/mysql/libmysqlclient.so
/usr/lib/mysql/libmysqlclient_r.so
/usr/lib/mysql/libmysqlclient_r.a
/usr/lib/mysql/libmysqlclient_r.la
/usr/lib/mysql/libmysqlclient.a
/usr/lib/libmysqlclient.so
/usr/lib/libmysqlclient_r.so
/usr/lib/libmysqlclient_r.so.15
/usr/lib/libmysqlclient_r.so.15.0.0
/usr/lib/libmysqlclient.so.15.0.0
So my question is this: how can I tell python (version 2.4.3) which version of libmysqlclient to use?
You can't tell the dynamic linker which version of a library to use, because the SONAME (full name of the library + interface) is part of the binary.
In your case, you can try to upload libmysqlclient_r.so.14 to the host and set LD_LIBRARY_PATH accordingly, so tell the dynamic linker which directories to search additionally to the system dirs when resolving shared objects.
You can use ldd to see if it LD_LIBRARY_PATH works:
$ ldd $path_to/_mysql.so
...
libmysqlclient_r.so.14 => $path_to_lib/libmysqlclient_r.so.14
...
Otherwise, there will be an error message about unresolved shared objects.
Of course that can only be a temporary fix until you rebuild MySQLdb to use the new libraries.
You will have to recompile python-mysql (aka MySQLdb) to get it to link to the new version of libmysqlclient.
If your host originally set up the environment rather than you compiling it, you'll have to pester them.
/usr/lib/libmysqlclient.so.14
This looks like a remnant of the old libmysqlclient, and should be removed. The _r and .a (static) versions are gone and you don't really want a mixture of libraries still around, it will only risk confusing automake.
Whilst you could make a symbolic link from libmysqlclient_r.so.14 to .15, that'd only work if the new version of the client happened to have the same ABI for the functions you wanted to use as the old - and that's pretty unlikely, as that's the whole point of changing the version number.
One solution is to set your PYTHONPATH environment variable to have some local directory, and copy over (or link, I suppose) the version of the mysql lib you want.

Categories