如何在 OSX 10.6 中将 MySQLdb 与 Python 和 Django 一起使用?
对于 OSX 10.6 用户来说,这是一个经常讨论的问题,但我一直无法找到有效的解决方案。这是我的设置:
Python 2.6.1 64位 姜戈1.2.1 MySQL 5.1.47 osx10.6 64位
我使用 --no-site-packages 创建一个 virtualenvwrapper ,然后安装 Django 。当我激活 virtualenv 并运行 python manage.pysyncdb 时,出现此错误:
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 257, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 67, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/commands/syncdb.py", line 7, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/sql.py", line 5, in <module>
from django.contrib.contenttypes import generic
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/contrib/contenttypes/generic.py", line 6, in <module>
from django.db import connection
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/__init__.py", line 75, in <module>
connection = connections[DEFAULT_DB_ALIAS]
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 91, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 32, in load_backend
return import_module('.base', backend_name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 14, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
我还安装了 MySQL for Python 适配器,但无济于事(也许我安装不当?)。
以前有人处理过这个吗?
This is a much discussed issue for OSX 10.6 users, but I haven't been able to find a solution that works. Here's my setup:
Python 2.6.1 64bit
Django 1.2.1
MySQL 5.1.47 osx10.6 64bit
I create a virtualenvwrapper with --no-site-packages, then installed Django. When I activate the virtualenv and run python manage.py syncdb, I get this error:
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 257, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/__init__.py", line 67, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/commands/syncdb.py", line 7, in <module>
from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/core/management/sql.py", line 5, in <module>
from django.contrib.contenttypes import generic
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/contrib/contenttypes/generic.py", line 6, in <module>
from django.db import connection
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/__init__.py", line 75, in <module>
connection = connections[DEFAULT_DB_ALIAS]
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 91, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/utils.py", line 32, in load_backend
return import_module('.base', backend_name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 14, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
I've also installed the MySQL for Python adapter, but to no avail (maybe I installed it improperly?).
Anyone dealt with this before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
我遇到了同样的错误,
pip install MySQL-python
为我解决了这个问题。备用安装:
easy_install MySQL-python
应该可以使用。该系统(例如
sudo apt-get install ...
)下面,Soli 指出,如果您收到以下错误:
...那么您还有进一步的系统依赖性问题。解决此问题的方法因系统而异,但对于 Debian 派生系统:
sudo apt-get install python-mysqldb
I had the same error and
pip install MySQL-python
solved it for me.Alternate installs:
easy_install MySQL-python
should work.that system (e.g.
sudo apt-get install ...
)Below, Soli notes that if you receive the following error:
... then you have a further system dependency issue. Solving this will vary from system to system, but for Debian-derived systems:
sudo apt-get install python-mysqldb
运行 Ubuntu,我必须这样做:
Running Ubuntu, I had to do:
添加到其他答案,以下内容帮助我完成了 mysql-python 的安装:
virtualenv, mysql-python, pip: 有人知道怎么做吗?
在 Ubuntu 上...
如果您没有适当的权限,请不要忘记在命令开头添加“sudo”。
Adding to other answers, the following helped me finish the installation mysql-python:
virtualenv, mysql-python, pip: anyone know how?
On Ubuntu...
Don't forget to add 'sudo' to the beginning of commands if you don't have the proper permissions.
尝试下面的命令。他们为我工作:
Try this the commands below. They work for me:
mysql_config
必须位于路径上。在 Mac 上,执行mysql_config
must be on the path. On Mac, do引发了一个错误:
修复了问题。
raised an error:
fixed the problem.
我是如何让它工作的:
在获取我的环境之后:
然后,我运行了startproject并在manage.py中添加了这个:
另外,更新了这个内部设置:
我还有
configparser==3.5.0
安装在我的 virtualenv 中,不确定是否需要...希望它有帮助,
How I got it working:
After sourcing my env:
Then, I ran the startproject and inside the manage.py, I added this:
Also, updated this inside settings:
I also have
configparser==3.5.0
installed in my virtualenv, not sure if that was required or not...Hope it helps,
以下内容对我来说非常有效,运行 Ubuntu 13.10 64 位:
现在,导航到您的 virtualenv (例如 env 文件夹)并执行以下命令:
我实际上找到了 解决方案在一个单独的问题中,我在下面引用它:
我认为这也应该适用于 OSX。唯一的问题是获得用于安装 libmysqlclient-dev 和 python-dev 的等效命令,因为它们需要编译
我猜是mysql-python。
希望这有帮助。
The following worked perfectly for me, running Ubuntu 13.10 64-bit:
Now, navigate to your virtualenv (such as env folder) and execute the following:
I actually found the solution in a separate question and I am quoting it below:
I think this should also work with OSX. The only problem would be getting an equivalent command for installing
libmysqlclient-dev
andpython-dev
as they are needed to compilemysql-python
I guess.Hope this helps.
此问题是由于 MySQL for Python 适配器安装不完整/不正确造成的。具体来说,我必须编辑 mysql_config 文件的路径以指向 /usr/local/mysql/bin/mysql_config - 在本文中进行了更详细的讨论:http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-豹纹/
This issue was the result of an incomplete / incorrect installation of the MySQL for Python adapter. Specifically, I had to edit the path to the mysql_config file to point to /usr/local/mysql/bin/mysql_config - discussed in greater detail in this article: http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-leopard/
这适用于 Red Hat Enterprise Linux Server 版本 6.4
This worked for Red Hat Enterprise Linux Server release 6.4
您可以安装为
pip install mysqlclient
You can install as
pip install mysqlclient
我升级到 OSX Mavericks 和 Pycharm 3 并开始收到此错误,我使用 pip 和 easy install 并收到错误:
所以我需要更新到Xcode 5并再次尝试使用pip安装。
这解决了所有问题。
I made the upgrade to OSX Mavericks and Pycharm 3 and start to get this error, i used pip and easy install and got the error:
So i need to update to Xcode 5 and tried again to install using pip.
That fix all the problems.
这里引发的错误是在导入 python 模块时发生的。这可以通过将 python site-packages 文件夹添加到 OS X 上的环境变量 $PYTHONPATH 来解决。因此我们可以将以下命令添加到 .bash_profile 文件中:
*将 xx 替换为您正在使用的 python 版本
The error raised here is in importing the python module. This can be solved by adding the python site-packages folder to the environment variable $PYTHONPATH on OS X. So we can add the following command to the .bash_profile file:
*replace x.x with the python version you are using
如果你使用的是python3,那么试试这个(我的操作系统是Ubuntu 16.04):
If you are using python3, then try this(My OS is Ubuntu 16.04):
pip
在 Windows 8 64 位系统上对我不起作用。easy_install mysql-python
对我有用。如果 pip 不起作用,您可以使用
easy_install
来避免在 Windows 上构建二进制文件。pip
did not work for me on windows 8 64 bits system.easy_install mysql-python
works for me.You can use
easy_install
to avoid building binaries on windows if pip does not work.我在 OSX 10.6.6 上遇到了同样的问题。但只是在终端上简单安装 mysql-python 并没有解决这个问题,因为另一个问题随之而来:
错误:命令“gcc-4.2”失败,退出状态为 1。
显然,这个问题是从 XCode3(OSX 10.6 自带的)升级到 XCode4 后出现的。这个较新的版本删除了对构建 ppc arch 的支持。如果是同样的情况,请在
easy_install mysql-python
之前尝试执行以下操作非常感谢 Ned Deily 提供的这个解决方案。 检查此处
I had the same problem on OSX 10.6.6. But just a simple
easy_install mysql-python
on terminal did not solve it as another hiccup followed:error: command 'gcc-4.2' failed with exit status 1
.Apparently, this issue arises after upgrading from XCode3 (which is natively shipped with OSX 10.6) to XCode4. This newer ver removes support for building ppc arch. If its the same case, try doing as follows before
easy_install mysql-python
Many thanks to Ned Deily for this solution. Check here
对我来说,只需重新安装 mysql-python 就解决了问题
For me the problem got solved by simply reinstalling mysql-python
安装命令行工具对我有用:
Install Command Line Tools Works for me:
我通过使用 pip 安装
MySQL-python
库克服了同样的问题。当我第一次在 settings.py 中更改数据库设置并执行 makemigrations 命令时,您可以看到控制台上显示的消息(解决方案遵循以下消息,只需查看即可)。最后我解决了这个问题,如下:
I overcame the same problem by installing
MySQL-python
library using pip. You can see the message displayed on my console when I first changed my database settings in settings.py and executed makemigrations command(The solution is following the below message, just see that).Finally I overcame this problem as follows:
运行此命令
现在您可以运行您的命令了。
Run this command
now you can run your command.
我遇到了像你这样的类似情况,我在 mac osx 上的 virtualenv 中使用 python3.7 和 django 2.1 。
尝试运行命令:
并编辑项目文件夹中的 __init__.py 文件并添加以下内容:
然后运行:
python3 manage.py runserver
或 python manage.py runserver
I encountered similar situations like yours that I am using python3.7 and django 2.1 in virtualenv on mac osx.
Try to run command:
And edit
__init__.py
file in your project folder and add following:Then run:
python3 manage.py runserver
or
python manage.py runserver