Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Saturday, January 19, 2013

mysql-python:EnvironmentError: mysql_config not found


1. 首先安装mysql
sudo apt-get install mysql-server
 
2. 安装mysql-python
下载MySQL-python-1.2.3.tar.gz(见附件),解压到指定目录。
到解压后的MySQL-python-1.2.3目录下编译配置MySQL-python:
python setup.py build
此时系统报错:EnvironmentError: mysql_config not found
很明显没有mysql_config这个文件
执行find / -name mysql_config,没有任何数据,表明系统中没有mysql_config这个文件
 
 
网上有人解释说使用apt-get安装的MySQL是没有mysql_config这个文件的
解决办法:sudo apt-get install libmysqld-dev
 
此时执行  find / -name mysql_config 在/usr/bin/下发现了这个文件
然后修改MySQL-python-1.2.3目录下的site.cfg文件
去掉mysql_config=XXX这行的注释,并改成mysql_config=/usr/bin/mysql_config(以mysql_config文件所在机器上的目录为准)
执行下面命令,此时可以成功编译安装了:
python setup.py build
python setup.py install

Python strip lstrip rstrip usage

theString = 'saaaay yes no yaaaass' 
print theString.strip('say') 
print theString.strip('say ') # there is a space after "say"
print theString.lstrip('say') 
print theString.rstrip('say') 

Result: 
yes no 
es no 
yes no yaaaass 
saaaay yes no

Python ImportError:No module named MySQLdb


solution:
Use one of this commands, depends what os and software do u have and use
  1. easy_install mysql-python (mix os)
  2. pip install mysql-python (mix os)
  3. apt-get install python-mysqldb (Linux Ubuntu, ...)
  4. cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD)
  5. yum install mysql-python (Linux Fedora, ...)



Reference :  http://stackoverflow.com/questions/454854/no-module-named-mysqldb


ImportError: No module named _tkinter


I'm using ubuntu11.10 and python2.7.2 .
A weird error:

File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in 
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter


But Tk was already installed in my computer...
$ sudo apt-get install python-tkReading package lists...Done
Building dependency tree       Reading state information...Done
python-tk is already the newest version.
0 upgraded,0 newly installed,0 to remove and0not upgraded.



It may be the problem of having two different versions of python installed in the computer.

Solution:
sudo ln -f /usr/bin/python2.7 /usr/local/bin/python


error when installing nltk ImportError: no module named yaml


1.Download PyYAML
 http://pyyaml.org/download/pyyaml/PyYAML-3.10.tar.gz
 
2.Unpack the archive and install the package by executing
  $python setup.py install
 
3.The source distribution includes a comprehensive test suite. To run the tests, type  
  $python setup.py test
 

Reference: http://pyyaml.org/wiki/PyYAML



ImportError: No module named xxxxx


用python時常遇到的一個錯誤: ImportError: No module named xxxxx   (xxxxx為module名)
例:  ImportError: No module named nltk

根據小弟經驗,發生這種錯誤通常有兩種可能
1. 沒有安裝xxxxx這個module,那就去網路找一下安裝就行了
2. 已安裝這個module,但python找不到這個moudle安裝在哪(有可能是同時安裝了兩個版本的python所導致)。
   這時有多種解法,最簡單的就是先找到這個moudle安裝的路徑,並把該路徑append到程式碼中,例: http://vocaloidmanager.blogspot.tw/2013/01/importerror-no-module-named-nltk.html

ImportError: No module named nltk


Solution:
In your code, simply append a path for nltk before you import nltk.
For example:

sys.path.append("/usr/local/lib/python2.7/site-packages/nltk-2.0b8-py2.7.egg/")
import nltk
 

The path /usr/local/lib/python2.7/site-packages/nltk-2.0b8-py2.7.egg/ is where the folder "nltk" located.


ImportError: cannot import name urandom


I use Ubuntu11.10 and python2.7.2+ .
File "/usr/lib/python2.7/random.py", line 47, in <module>
    from os import urandom as _urandom
ImportError: cannot import name urandom

After I checked os.py , I found that there is no module named "urandom" in it:
$ python
Python 2.7.2+ (default, Aug 16 2011, 07:24:41)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.urandom
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'urandom'

So I reinstall python2.7.2 :
$ ./configure
$ make
$ sudo make install

then it fixed the error!