How to install MySQLdb package? (ImportError: No m

2020-02-19 02:57发布

I am trying to install MySQLdb package. I found the source code here.

I did the following:

gunzip MySQL-python-1.2.3c1.tar.gz
tar xvf MySQL-python-1.2.3c1.tar
cd MySQL-python-1.2.3c1
python setup.py build

As the result I got the following:

Traceback (most recent call last):
  File "setup.py", line 5, in ?
    from setuptools import setup, Extension
ImportError: No module named setuptools

Does anybody knows how to solve this problem? By the way, if I am able to do the described step, I will need to do the following:

sudo python setup.py install

And I have no system-administrator-rights. Do I still have a chance to install MySQLdb?

Thank you.

11条回答
够拽才男人
2楼-- · 2020-02-19 03:37

This was sort of tricky for me too, I did the following which worked pretty well.

  • Download the appropriate Python .egg for setuptools (ie, for Python 2.6, you can get it here. Grab the correct one from the PyPI site here.)
  • chmod the egg to be executable: chmod a+x [egg] (ie, for Python 2.6, chmod a+x setuptools-0.6c9-py2.6.egg)
  • Run ./[egg] (ie, for Python 2.6, ./setuptools-0.6c9-py2.6.egg)

Not sure if you'll need to use sudo if you're just installing it for you current user. You'd definitely need it to install it for all users.

查看更多
\"骚年 ilove
3楼-- · 2020-02-19 03:38
#!/usr/bin/env python

import os
import sys
from **distutils.core** import setup, Extension

if sys.version_info < (2, 3):
    raise Error("Python-2.3 or newer is required")

if os.name == "posix":
    from setup_posix import get_config
else: # assume windows
    from setup_windows import get_config

metadata, options = get_config()
metadata['ext_modules'] = [Extension(sources=['_mysql.c'], **options)]
metadata['long_description'] = metadata['long_description'].replace(r'\n', '')
setup(**metadata)
查看更多
爷、活的狠高调
4楼-- · 2020-02-19 03:47

If MySQLdb's now distributed in a way that requires setuptools, your choices are either to download the latter (e.g. from here) or refactor MySQLdb's setup.py to bypass setuptools (maybe just importing setup and Extension from plain distutils instead might work, but you may also need to edit some of the setup_*.py files in the same directory).

Depending on how your site's Python installation is configured, installing extensions for your own individual use without requiring sysadm rights may be hard, but it's never truly impossible if you have shell access. You'll need to tweak your Python's sys.path to start with a directory of your own that's your personal equivalent of the system-wide site pacages directory, e.g. by setting PYTHONPATH persistently in your own environment, and then manually place in said personal directory what normal installs would normally place in site-packages (and/or subdirectories thereof).

查看更多
Juvenile、少年°
5楼-- · 2020-02-19 03:47

@main:

$ su
$ yum install MySQL-python

and it will be installed (MySQLdb).

查看更多
甜甜的少女心
6楼-- · 2020-02-19 03:52

well installing C compiler or GCC didn't work but I found a way to successfully install mysqldb package

kindly follow Mike schrieb's (Thanks to him) instructions here . In my case, I used setuptools-0.6c11-py2.7.egg and setuptools-0.6c11 . Then download the executable file here then install that file. hope it helps :)

查看更多
登录 后发表回答