Getting “Error loading MySQLdb module: No module named MySQLdb” - have tried previously posted solutions

匿名 (未验证) 提交于 2019-12-03 01:31:01

问题:

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    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  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  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    from django.db import connection File "/Users/joerobinson/.virtualenvs/dj_tut/lib/python2.6/site-packages/django/db/__init__.py", line 75, in    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    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?

回答1:

I had the same error and pip install MySQL-python solved it for me.

Alternate installs:

  • If you don't have pip, easy_install MySQL-python should work.
  • If your python is managed by a packaging system, you might have to use that system (e.g. sudo apt-get install ...)

Below, Soli notes that if you receive the following error:

EnvironmentError: mysql_config not found

... 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



回答2:

Running Ubuntu, I had to do:

sudo apt-get install python-mysqldb 


回答3:

Adding to other answers, the following helped me finish the installation mysql-python:

virtualenv, mysql-python, pip: anyone know how?

On Ubuntu...

apt-get install libmysqlclient-dev apt-get install python-dev pip install mysql-python 

Don't forget to add 'sudo' to the beginning of commands if you don't have the proper permissions.



回答4:

Try this the commands below. They work for me:

brew install mysql-connector-c  pip install MySQL-python 


回答5:

mysql_config must be on the path. On Mac, do

export PATH=$PATH:/usr/local/mysql/bin/ pip install MySQL-python 


回答6:

pip install mysql-python 

raised an error:

EnvironmentError: mysql_config not found

sudo apt-get install python-mysqldb 

fixed the problem.



回答7:

Try this: This solved the issue for me .

pip install MySQL-python



回答8:

How I got it working:

virtualenv -p python3.5 env/test 

After sourcing my env:

pip install pymysql pip install django 

Then, I ran the startproject and inside the manage.py, I added this:

+ try: +     import pymysql +     pymysql.install_as_MySQLdb() + except: +     pass 

Also, updated this inside settings:

DATABASES = {     'default': {         'ENGINE': 'django.db.backends.mysql',         'NAME': 'foobar_db',         'USER': 'foobaruser',         'PASSWORD': 'foobarpwd',     } } 

I also have configparser==3.5.0 installed in my virtualenv, not sure if that was required or not...

Hope it helps,



回答9:

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/



回答10:

The following worked perfectly for me, running Ubuntu 13.10 64-bit:

sudo apt-get install libmysqlclient-dev sudo apt-get install python-dev 

Now, navigate to your virtualenv (such as env folder) and execute the following:

sudo ./bin/pip install mysql-python 

I actually found the solution in a separate question and I am quoting it below:

If you have created the virtualenv with the --no-site-packages switch (the default), then system-wide installed additions such as MySQLdb are not included in the virtual environment packages.

You need to install MySQLdb with the pip command installed with the virtualenv. Either activate the virtualenv with the bin/activate script, or use bin/pip from within the virtualenv to install the MySQLdb library locally as well.

Alternatively, create a new virtualenv with system site-packages included by using the --system-site-package switch.

I think this should also work with OSX. The only problem would be getting an equivalent command for installing libmysqlclient-dev and python-dev as they are needed to compile mysql-python I guess.

Hope this helps.



回答11:

This worked for Red Hat Enterprise Linux Server release 6.4

sudo yum install mysql-devel sudo yum install python-devel pip install mysql-python 


回答12:

sudo apt-get install python-mysqldb works perfectly in ubuntu

pip install mysql-python raises an Environment Error



回答13:

You can install as pip install mysqlclient



回答14:

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:

command'/usr/bin/clang' failed with exit status 1.

So i need to update to Xcode 5 and tried again to install using pip.

pip install mysql-python 

That fix all the problems.



回答15:

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.



回答16:

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

sudo bash export ARCHFLAGS='-arch i386 -arch x86_64' rm -r build python setup.py build python setup.py install 

Many thanks to Ned Deily for this solution. Check here



回答17:

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:

export PYTHONPATH="$PYTHONPATH:/usr/local/lib/pythonx.x/site-packages/" 

*replace x.x with the python version you are using



回答18:

If you are using python3, then try this(My OS is Ubuntu 16.04):

sudo apt-get install python3-mysqldb 


回答19:

For me the problem got solved by simply reinstalling mysql-python

pip uninstall mysql-python pip install mysql-python 


回答20:

Install Command Line Tools Works for me:

xcode-select --install 


回答21:

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).

  (vir_env) admins-MacBook-Pro-3:src admin$ python manage.py makemigrations Traceback (most recent call last):   File "manage.py", line 10, in      execute_from_command_line(sys.argv)   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line     utility.execute()   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute     django.setup()   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/__init__.py", line 18, in setup     apps.populate(settings.INSTALLED_APPS)   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate     app_config.import_models(all_models)   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models     self.models_module = import_module(models_module_name)   File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module     __import__(name)   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in      class Permission(models.Model):   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/base.py", line 139, in __new__     new_class.add_to_class('_meta', Options(meta, **kwargs))   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class     value.contribute_to_class(cls, name)   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/models/options.py", line 250, in contribute_to_class     self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__     return getattr(connections[DEFAULT_DB_ALIAS], item)   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/utils.py", line 240, in __getitem__     backend = load_backend(db['ENGINE'])   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/utils.py", line 111, in load_backend     return import_module('%s.base' % backend_name)   File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module     __import__(name)   File "/Users/admin/Desktop/SetUp1/vir_env/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 27, in      raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb 

Finally I overcame this problem as follows:



回答22:

Run this command

sudo pip install mysql-python; 

now you can run your command.

python manage.py startapp filename; 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!