Windows上使用Python和MySQL:安装错误

0 投票
3 回答
7492 浏览
提问于 2025-04-15 17:23

我在我的Django项目文件夹里运行了以下命令:

$ python manage.py dbshell

结果出现了这个错误:

$python manage.py dbshell
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "C:\Python25\lib\site-packages\django\core\management\__init__.py", line
362, in execute_manager
    utility.execute()
  File "C:\Python25\lib\site-packages\django\core\management\__init__.py", line
303, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python25\lib\site-packages\django\core\management\base.py", line 195,
 in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Python25\lib\site-packages\django\core\management\base.py", line 222,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python25\lib\site-packages\django\core\management\base.py", line 351,
 in handle
    return self.handle_noargs(**options)
  File "C:\Python25\lib\site-packages\django\core\management\commands\dbshell.py
", line 9, in handle_noargs
    from django.db import connection
  File "C:\Python25\lib\site-packages\django\db\__init__.py", line 41, in <modul
e>
    backend = load_backend(settings.DATABASE_ENGINE)
  File "C:\Python25\lib\site-packages\django\db\__init__.py", line 17, in load_b
ackend
    return import_module('.base', 'django.db.backends.%s' % backend_name)
  File "C:\Python25\Lib\site-packages\django\utils\importlib.py", line 35, in im
port_module
    __import__(name)
  File "C:\Python25\lib\site-packages\django\db\backends\mysql\base.py", line 13
, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No mo
dule named MySQLdb

第一个问题是,为什么Python不直接包含这个MySQLdb模块呢?

好吧,没关系,我决定搜索一下来解决这个问题。

我在stackoverflow.com上查找了安装这个模块的方法,但没有找到好的结果。

可以在这里下载这个模块:http://sourceforge.net/projects/mysql-python/

接下来是在我的Windows Vista系统上安装这个模块。解压缩包后,我打开命令提示符继续安装:

$ python setup.py install

结果又出现了另一个消息:

D:\SOFTWARE\PROGRAMMING\MySQL-python-1.2.3c1>python setup.py install
Traceback (most recent call last):
  File "setup.py", line 5, in <module>
    from setuptools import setup, Extension
ImportError: No module named setuptools

在处理这个错误消息时,我发现包里有一个ez_setup.py文件:

$python ez_setup.py

看起来一切正常:

D:\SOFTWARE\PROGRAMMING\MySQL-python-1.2.3c1>python ez_setup.py
Downloading http://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c9-py
2.5.egg
Processing setuptools-0.6c9-py2.5.egg
Copying setuptools-0.6c9-py2.5.egg to c:\python25\lib\site-packages
Adding setuptools 0.6c9 to easy-install.pth file
Installing easy_install-script.py script to C:\Python25\Scripts
Installing easy_install.exe script to C:\Python25\Scripts
Installing easy_install-2.5-script.py script to C:\Python25\Scripts
Installing easy_install-2.5.exe script to C:\Python25\Scripts

Installed c:\python25\lib\site-packages\setuptools-0.6c9-py2.5.egg
Processing dependencies for setuptools==0.6c9
Finished processing dependencies for setuptools==0.6c9

现在我回到setup.py再试着安装一次:

$python setup.py install

结果又给了我一个新的错误消息:

D:\SOFTWARE\PROGRAMMING\MySQL-python-1.2.3c1>python setup.py install
running install
running bdist_egg
....
copying MySQLdb\constants\CLIENT.py -> build\lib.win32-2.5\MySQLdb\constants
running build_ext
error: Python was built with Visual Studio 2003;
extensions must be built with a compiler than can generate compatible binaries.
Visual Studio 2003 was not found on this system. If you have Cygwin installed,
you can try compiling with MingW32, by passing "-c mingw32" to setup.py.

我该如何解决这个问题呢?

3 个回答

0

呃,这不是Django的问题,你只是下载了一个不明确的Python环境,然后期待它能神奇地按照你的想法完美运行。你需要找一个好的教程,按照上面的步骤去做。

顺便说一下,这里是一个非常有帮助的论坛,但如果你不提供具体信息,然后又以一种态度不好的方式提问,大家就不太会愿意帮你了。

0

我之前也遇到过在同一台电脑上同时运行Python和MySQL的问题。就像上面提到的,Python本身并不支持MySQL,所以你需要下载一些连接工具。

@kibitzer提供的链接在Windows上可能不太好用,所以你可以去这里下载一个适合Windows的连接工具。这个下载包里有安装程序,你不需要手动运行setup.py脚本。

4

你有没有试着在这里找找看:http://sourceforge.net/projects/mysql-python/files/

这是MySQLdb项目的下载区域,跟django没有关系,所以你的问题不太对——django并不会让你切换数据库变得困难,你只需要改一行代码而已。当然,你的Python安装也得支持那个数据库,所以你可以通过我给的链接下载适合Windows的二进制包(记得选择与你的Python版本匹配的正确版本),这样就能避免编译源代码带来的麻烦。

你很可能需要的是 MySQL-python-1.2.2.win32-py2.5.exe 或者 MySQL-python-1.2.2.win32-py2.4.exe

撰写回答