简体   繁体   English

Django manage.py syncdb throw没有名为MySQLdb的模块

[英]Django manage.py syncdb throwing No module named MySQLdb

I am a newbie learning Python/Django... 我是学习Python / Django的新手......

Am using the following tutorial located here . 我正在使用此处的以下教程。

Created a mysite database in MySQL 5 running on Snow Leopard. 在Snow Leopard上运行的MySQL 5中创建了一个mysite数据库。

Edited the settings.py file to look like this: 编辑settings.py文件如下所示:

DATABASE_ENGINE = 'mysql'        
DATABASE_NAME = 'mysite'             
DATABASE_USER = 'root'             
DATABASE_PASSWORD = ''       
DATABASE_HOST = ''            
DATABASE_PORT = ''             

Now when I run the following command: 现在,当我运行以下命令时:

python manage.py syncdb

I receive the following error: 我收到以下错误:

Traceback (most recent call last):
File "manage.py", line 11, in <module>
  execute_manager(settings)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/__init__.py", line 362,
  in execute_manager
  utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/__init__.py", line 303,
  in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/base.py", line 195, in
  run_from_argv
  self.execute(*args, **options.__dict__)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/base.py", line 221, in
  execute
  self.validate()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/base.py", line 249, in
  validate
  num_errors = get_validation_errors(s, app)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/validation.py", line
  22, in get_validation_errors
  from django.db import models, connection
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/db/__init__.py", line 41, in <module>
  backend = load_backend(settings.DATABASE_ENGINE)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages  
   /django/db/__init__.py", line 17, in load_backend
   return import_module('.base', 'django.db.backends.%s' %backend_name)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
  __import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/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 module named MySQLdb

What am I possibly doing wrong? 我可能做错了什么?

Happy programming... 编程愉快......

sudo easy_install mysql-python

will install the MySQLdb module to allow you to work with MySQL from Python, or, if you want to work with virtualenv (which you should), 将安装MySQLdb模块以允许您从Python使用MySQL,或者,如果您想使用virtualenv(您应该),

sudo easy_install virtualenv virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export PIP_VIRTUALENV_BASE=$WORKON_HOME
source /usr/local/bin/virtualenvwrapper_bashrc
mkvirtualenv mysite
pip install mysql-python django

Will put you inside a virtualenv with a current install of django (you can specify which version, eg django==1.1.1) and the MySQLdb module installed. 将你带入当前安装的django(你可以指定哪个版本,例如django == 1.1.1)和安装了MySQLdb模块的virtualenv。 Using virtualenv will allow you to have separate environments for different projects so you can install different modules and even use different versions of those modules (or Python) for different projects. 使用virtualenv将允许您为不同的项目提供单独的环境,因此您可以安装不同的模块,甚至可以为不同的项目使用这些模块(或Python)的不同版本。 To leave you virtualenv just type the command 离开你virtualenv只需输入命令

deactivate

or, to switch to the environment 'foo' type 或者,切换到环境'foo'类型

workon foo

You should also, if you're going to be using virtualenv add these three lines to your ~/.bash_profile (on OS X, ~/.bashrc generally on Linux): 您还应该,如果您要使用virtualenv将这三行添加到〜/ .bash_profile(在OS X上,〜/ .bashrc一般在Linux上):

export WORKON_HOME=$HOME/.virtualenvs # where virtualenvs should be created
export PIP_VIRTUALENV_BASE=$WORKON_HOME # tells pip where to look for virtualenvs
source /usr/local/bin/virtualenvwrapper_bashrc # bash completion and wrapper functions for virtualenv

The problem is that you are missing the Python module that interfaces with MySQL. 问题是你缺少与MySQL接口的Python模块。

See the Django docs for get your database running which further points to MySQL notes . 请参阅Django文档以获取正在运行的数据库,这些文档进一步指向MySQL注释

However, if you are just going through the tutorial it is much easier to use SQLite backend which is built-in to Python. 但是,如果您只是阅读本教程,那么使用内置于Python的SQLite后端会更容易。 No drivers, server, etc. needed. 不需要驱动程序,服务器等。

It's worth pointing out (even at this late date) that the "ImproperlyConfigured" error below 值得指出(即使在这个较晚的日期)下面的“不正确配置”错误

File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages /django/db/backends/mysql/base.py", line 13, in 文件“/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages /django/db/backends/mysql/base.py”,第13行,in
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb raise ImproperlyConfigured(“加载MySQLdb模块时出错:%s”%e)django.core.exceptions.ImproperlyConfigured:加载MySQLdb模块时出错:没有名为MySQLdb的模块

can be a red herring in cases where mysql isn't running - I was just googling this error myself, wondering why a misconfiguration had 'suddenly' appeared and while searching and prodding I ran some other manage.py command and got this similar, but much more helpful, error: 在mysql没有运行的情况下可以是一个红色的鲱鱼 - 我只是自己搜索这个错误,想知道为什么错误配置'突然'出现了,而在搜索和刺激时我运行了一些其他manage.py命令并得到了类似的但是更有帮助,错误:

File "~/.virtualenvs/mdid3/lib/python2.7/site-packages/MySQLdb/connections.py", line 187, in init init中的文件“〜/ .virtualenvs / mdid3 / lib / python2.7 / site-packages / MySQLdb / connections.py”,第187行

super(Connection, self). 超级(连接,自我)。 init (*args, **kwargs2) init (* args,** kwargs2)

_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (61)") _mysql_exceptions.OperationalError:(2003,“无法连接到'127.0.0.1上的MySQL服务器'(61)”)

After checking mysqld, finding it not running and starting it -- my configuration error was miraculously fixed! 检查mysqld后,发现它没有运行并启动它 - 奇迹般地修复了我的配置错误!

Try this if you are using 如果您使用,请尝试此操作

linux:- sudo apt-get install python-mysqldb linux: - sudo apt-get install python-mysqldb

windows:- pip install python-mysqldb or easy_install python-mysqldb windows: - pip install python-mysqldbeasy_install python-mysqldb

Hope this should work 希望这应该有效

Make sure to create your database and with your MySQL server running try to connect to the database to check that database name, username and password are correct. 确保创建数据库并运行MySQL服务器尝试连接数据库以检查数据库名称,用户名和密码是否正确。 then run: 然后运行:

1) pip install mysqlclient 1) pip install mysqlclient

2) python manage.py migrate 2) python manage.py migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM