python mysqldb import error - python

on ubuntu i have installed mysql but still i cannot import mysql database. Below are the steps that i followed.Can any one point me to a solution to this
(Other info is ubuntu is installed on a virtual box hope that should not matter).Python version is 2.6.5
root#rajeev-laptop:/opt/s/site# apt-get install python-mysqldb
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-2.6.32-21 linux-headers-2.6.32-21-generic
Use 'apt-get autoremove' to remove them.
Suggested packages:
python-mysqldb-dbg
The following NEW packages will be installed:
python-mysqldb
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0B/76.6kB of archives.
After this operation, 283kB of additional disk space will be used.
Selecting previously deselected package python-mysqldb.
(Reading database ... 151280 files and directories currently installed.)
Unpacking python-mysqldb (from .../python-mysqldb_1.2.2-10build1_i386.deb) ...
Setting up python-mysqldb (1.2.2-10build1) ...
Processing triggers for python-support ...
root#rajeev-laptop:/opt/s/site# python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
>>>

All,
I got this resolved by the following, in my /etc/bash.bashrc i was importing a pythonpath as /opt/s/
Where django and my site resided.When i removed the following from the pythonpath the import MySQLdb worked..Hope this post will help somebody..

Ahh these problem... invest some time in virtualenv for python. It will pay off =)

I would check the Python PATH and make a manual build of MySQLDB download. Download the installation files from this source forge site.

The package information shows you what's going on (ubuntu 12.04):
apt-cache show python-mysqldb
[...]
Provides: python2.7-mysqldb
Depends: python2.7, [...]
Python-Version: 2.7
[...]
This package is for python 2.7 - so it's not installed for 2.6. execute python2.7 in a shell and try the import again.
(if you have an older ubuntu version, execute the apt-cache command on your machine to check if this is the case in your version as well)

Related

Ubuntu Python Homebrew no module named '_tkinter'

I have the issue with Python 3.9.1 on freshly installed Ubuntu (Ubuntu 20.04.1 LTS) - when running any tkinter-based application, I get import error (example from python console):
user#ubuntu:~$ python3
Python 3.9.1 (default, Dec 29 2020, 13:25:02)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/linuxbrew/.linuxbrew/Cellar/python#3.9/3.9.1_3/lib/python3.9/tkinter/__init__.py", line 37, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
I know this issue was discussed multiple times in SO and other internet resources, for several days I am trying all the options and solutions (like this, this, another one, brew forum neither of them solved the issue).
What works - only uninstalling homebrew does the trick. Still have hope to configure everything to work with homebrew.
What I have tried/performed:
fixed all brew doctor reported issues:
Your system is ready to brew.
tcl-tk is listed under brew list:
black hello libx11 libxdmcp openssl#1.1 pylint sqlite xorgproto
bzip2 libffi libxau libxext patchelf python#3.9 tcl-tk xz
gdbm libpthread-stubs libxcb ncurses pkg-config readline unzip zlib
performed brew uninstall python and brew install python
Initial thought was poetry and/or pyenv broke the dependencies, currently using neither of them.
I am not hardcore ubuntu user, am I missing something? Any idea or help would be valuable.
Homebrew is not the official pcakage mamanger for ubuntu and has some strange behaviours ,Ubuntu's officialy supported pacakage manager that is installed by default is apt(advanced package tool)
First uninstall your brew install of python by typing:
brew uninstall python
Then this is how you use APT to install python3:
sudo apt-get install python3
OR
sudo apt install python3
And to test if python3 installed correctly type:
python3
If a python shell opens python3 has been installed properly
Then to test if tkinter is working type this in the python shell:
import tkinter
Closing the question, as no other activity for over a week.
Used solution I wanted to avoid - removed homebrew, now Tkinter is detected properly.
For anyone stumbling on the same issue - uninstalling homebrew: https://github.com/homebrew/install#uninstall-homebrew

How to use Python Dbus bindings in Anaconda

I am trying to install dbus on Anaconda python environment and I am struggling.
Here is the error message I am getting:
e#gateway:~$ python
Python 3.5.4 |Anaconda custom (64-bit)| (default, Oct 13 2017, 11:22:58)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbus
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/e/anaconda3/lib/python3.5/site-packages/dbus/__init__.py", line 77, in <module>
import dbus.types as types
File "/home/e/anaconda3/lib/python3.5/site-packages/dbus/types.py", line 6, in <module>
from _dbus_bindings import (
ImportError: /home/e/anaconda3/lib/python3.5/site-packages/_dbus_bindings.so: undefined symbol: _Py_ZeroStruct
>>>
Here are some of the outputs I think may be asked:
e#gateway:~$ conda install dbus
Fetching package metadata ...........
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /home/e/anaconda3:
#
dbus 1.10.22 h3b5a359_0
e#gateway:~$ sudo apt-get install libdbus-glib-1-dev libdbus-1-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
libdbus-glib-1-dev is already the newest version (0.106-1).
libdbus-1-dev is already the newest version (1.10.6-1ubuntu3.3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
e#gateway:~$ sudo apt-get install dbus
Reading package lists... Done
Building dependency tree
Reading state information... Done
dbus is already the newest version (1.10.6-1ubuntu3.3).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
e#gateway:~$ which python
/home/e/anaconda3/bin/python
e#gateway:~$ conda --version
conda 4.3.31
e#gateway:~$ sudo /home/e/anaconda3/bin/python -m pip install dbus-python
The directory '/home/e/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/e/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: dbus-python in ./anaconda3/lib/python3.5/site-packages
DBus is working fine on the system python, however not working on Anaconda Python.
Python 2.7:
e#gateway:~$ which python
/usr/bin/python
e#gateway:~$ python
Python 2.7.12 (default, Nov 20 2017, 18:23:56)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbus
>>>
Python 3.5:
e#gateway:~$ which python3
/usr/bin/python3
e#gateway:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbus
>>>
Can anyone help me? Am I missing something blatantly obvious here?
Thanks in advance.
I had similar issues, there are few cases where dbus and python don't work well out-of-the-box. The consensus appears to be that you need a system-level install (i.e. apt-get) to get dbus to work. I believe the /home/e/anaconda3/lib/python3.5/site-packages/_dbus_bindings.so: undefined symbol: _Py_ZeroStruct error you're seeing is directly related to that.
conda install dbus does not add anything to ~/anaconda3/lib/python3.6/site-packages, but instead appears to install some executables in ~/anaconda3/bin/ like dbus-run-session, dbus-daemon, etc. This makes some sense when you analyze the contents of the dbus tarball https://anaconda.org/conda-forge/dbus, as it's all C files and executables. I'm not sure it's supposed to be the dbus python module, but I could be wrong.
EDIT:
I searched the conda repositories and found a few individuals that uploaded a version of dbus-python, presumably that they compiled and installed. I tried this one out in a py3.6 conda environment via:
conda install -c scottwales dbus-python
I was then able to import dbus. This is a hacky approach and should not be used in production, I'd recommend listening to
Carlos Cordoba's post below. But if you need a solution now, search through some user conda packages or try to compile the library yourself.
Can anyone help me? Am I missing something blatantly obvious here?
Yes, you are. There's one thing people still don't understand about conda: conda is not a pip replacement. It is a general package manager, in the same vein as apt-get, yum, brew, emerge, etc, but cross-platform and based on Python.
In this case, that means that conda install dbus does not install the Python Dbus bindings, as you would expect with pip . It installs the Dbus C package itself, which is needed by Qt 5 (again, the C++ library, not the Python bindings to it).
Unfortunately, there are no Conda packages for dbus-python. To make matters worse, it seems there's no easy way to create packages for it, as pointed out here.
Finally, you said
Here is the error message I am getting
The (most probable) cause of that error is because you added your system Python dist-packages path to the PYTHONPATH of Anaconda or because you blindly copied the dbus module from system Python to Anaconda. Please don't do that ever again. System Python and Anaconda packages are compiled with different compilers and under different conditions. So mixing them is the cause of incomprehensible errors, just like the one you reported.

Python 2.7.9: No module named requests

I am writing a code in Python 2.7.9 for which I need the requests module. I installed the module using sudo pip install requests but still in python 2.7.9 I am getting an error as follows:
Python 2.7.9 (default, Jan 5 2016, 18:47:14)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
I checked the installation location /usr/local/lib/python2.7/dist-packages, it contains the requests package directory. The same thing works fine in python3, so I am guessing it's an installation error. How can I fix it ?
EDIT:
On executing pip lists, I could see requests (2.1.9) in the list. After I ran pip uninstall requests it shows requests (2.2.1) in pip list.
Based on the comments, it seems you have installed python 2.7.9 using a method Ubuntu doesn't like. Because of this the dist-packages folder is not added to your sys.path. You could set the PYTHONPATH variable in your .bashrc (or other zshrc, etc) to add that folder to your sys.path by default.
Better method would be to use a library like pyenv (It handles all dependency issues flawlessly for multiple python versions) or a better supported ppa for the latest python where this problem shouldn't arise at all.
Also, you have 2 versions of requests. This seems to be because one is installed using apt-get (sudo apt-get install python-requests) and the other is from pip (sudo pip install requests). It would be good to remove one of them to avoid confusion.

Why I can not use an installed module for python?

I have installed a python library MySQLdb, and it works yesterday. But today when I tried to run it, it goes on as following:
czhao#opx790:~$ python
Python 2.7.7 |Anaconda 2.0.1 (64-bit)| (default, Jun 2 2014, 12:34:02)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
It is really strange, so I try to re-install it, but it seems I do have the library in my computer:
czhao#opx790:~$ sudo apt-get install python-mysqldb
[sudo] password for czhao:
Reading package lists... Done
Building dependency tree
Reading state information... Done
python-mysqldb is already the newest version.
The following packages were automatically installed and are no longer required:
libgooglepinyin0-dev linux-headers-3.2.0-27 linux-headers-3.2.0-41
linux-headers-3.2.0-37 linux-headers-3.2.0-43 linux-headers-3.2.0-61
patchutils linux-headers-3.2.0-37-generic linux-headers-3.2.0-27-generic
linux-headers-3.2.0-61-generic linux-headers-3.2.0-43-generic dpatch
linux-headers-3.2.0-41-generic
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 16 not upgraded.
I do not know why, I have the library but python keep tell me I do not have it.
You are using Anaconda Python, but the package installed with apt-get was installed for the system Python. These two different distributions have different library directories, so packages/modules installed with one are not available for the other.
To get around this, use the pip program that came with Anaconda and run sudo pip install MySQL-python.

WARNING: IPython History requires SQLite, your history will not be saved

Hi I'm using Ubuntu release 12.10 (quantal) 32-bit with Linux Kernel 3.5.0-21-generic. I'm trying to get IPython's History to work. I've set it up using pythonbrew and a virtual environment. In there I use pip to install IPython. Currently, when I start up IPython in a terminal I get:
WARNING: IPython History requires SQLite, your history will not be saved
Python 2.7.3 (default, Nov 8 2012, 18:25:10)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
Searching on the warning in the first line, I found this issue report, so I went back and installed the following:
sudo apt-get install libsqlite0 libsqlite0-dev libsqlite3-0 libsqlite3-dev
and then removed and reinstalled pysqlite using pip
pip uninstall pysqlite
pip install pysqlite
After that I thought I would check the installation by importing the module:
Python 2.7.3 (default, Nov 8 2012, 18:25:10)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3
So now it seems the file _sqlite3.so can't be found. That's when I found this SO question. Either it doesn't exist or it's not in my PYTHONPATH environment variable. Searching for the file, I get:
$ locate _sqlite3.so
/home/me/Desktop/.dropbox-dist/_sqlite3.so
/home/me/epd/lib/python2.7/lib-dynload/_sqlite3.so
/usr/lib/python2.7/lib-dynload/_sqlite3.so
So the file is there, but when I looked in my python path:
import sys
for p in sys.path:
print p
none of the above paths that contain _sqlite3.so were contained in my PYTHONPATH. For giggles, I added the path /usr/lib/python2.7/lib-dynload to my PYTHONPATH in a terminal and then tried to import sqlite3 again:
Python 2.7.3 (default, Nov 8 2012, 18:25:10)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append("/usr/lib/python2.7/lib-dynload")
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/home/me/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: /usr/lib/python2.7/lib-dynload/_sqlite3.so: undefined symbol: PyUnicodeUCS4_DecodeUTF8
Uh oh. Now I'm completely stuck. Can anyone help me out? I've also read in a few places that I may have to rebuild Python. I have no idea how to do this in pythonbrew. Can anyone point me in the right direction?
I've also read in a few places that I may have to rebuild Python.
This is correct. SQLite is part of the standard library,
and is built when you compile Python. There are a few 'optional' parts
of the standard library, which Python will simply skip (with minimal warning, unfortunately)
if the dependencies are missing at build time, and sqlite is one of these.
You should be able to just install libsqlite3-dev,
then rebuild Python and you should be set.
Keep an eye on the build messages,
as they do report which modules they are skipping due to missing dependencies.
Thanks to minrk for pointing me in the right direction. All I had to do was rebuild python. I've outlined the steps below for those that are using pythonbrew. Notice that I already installed the libsqlite3-dev package up in the question section.
First, with the proper version of python and virtual environment loaded up run the command:
$ pip freeze -l > requirements.txt
This gives us a text file list of all of the pip packages that have been installed in the virtual environment for this particular python release in pythonbrew. Then we remove the version of python from pythonbrew and reinstall it (this is the "rebuild python" step):
$ pythonbrew uninstall 2.7.3
$ pythonbrew install 2.7.3
After that, we switch over to the newly installed python version 2.7.3 and create a new virtual environment (which I've called "sci"):
$ pythonbrew switch 2.7.3
$ pythonbrew venv create sci
$ pythonbrew venv use sci
Ideally you should be able to run the command:
$ pip install -r requirements.txt
and according to this pip should reinstall all the modules that you had in the virtual environment before we clobbered that version of python (2.7.3). It didn't work for me for whatever reason so I manually installed all of the modules using pip individuality.
$ ipython --pylab
Python 2.7.3 (default, Jan 5 2013, 18:48:27)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
and IPython history works!
What worked for me (using osx + homebrew + brewed python):
# Reinstall Python 2.7 with sqlite
brew remove python
brew install readline sqlite gdbm --universal
brew install python --universal --framework
# Reinstall iPython with correct bindings
pip uninstall ipython
pip install ipython
And you should be good to go.
You should rebuild your python with sqlite support
sudo apt-get install libsqlite3-dev
wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz
tar -xvf Python-2.7.15.tgz
cd Python-2.7.15
./configure
make
sudo make install
Recreate your virtual environment and you should be good to go
rmvirtualenv venv
mkvirtualenv -p python2 venv
workon venv
pip install -r requirements.txt
# or
pip install ipython
This warning appears on macOS when python is installed with pyenv. By default it installs python without sqlite. These commands reinstall python with sqlite support:
pyenv uninstall 3.7
CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install 3.7

Categories