web-dev-qa-db-fra.com

Erreur lors du chargement du module MySQLdb et du "programme d'installation de MySQLdb"

J'essaie de démarrer un projet Django. 

Je reçois cette erreur en essayant de lancer le fichier manage.py:

(venv)dyn-160-39-161-214:proj Bren$ python manage.py
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/Django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/Django/core/management/__init__.py", line 328, in execute
    Django.setup()
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/Django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/Django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/Django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/Django/contrib/auth/models.py", line 41, in <module>
    class Permission(models.Model):
  File "/Users/Bren/Desktop/fss/venv/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/Bren/Desktop/fss/venv/lib/python2.7/site-packages/Django/db/models/base.py", line 324, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/Bren/Desktop/fss/venv/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/Bren/Desktop/fss/venv/lib/python2.7/site-packages/Django/db/__init__.py", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/Django/db/utils.py", line 241, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/Django/db/utils.py", line 112, in load_backend
    return import_module('%s.base' % backend_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/Django/db/backends/mysql/base.py", line 27, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
Django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

Je me demandais quel module je devrais essayer d'installer.

Quand j'utilise pip install MySQLdb j'obtiens cette erreur:

    Collecting MySQLdb
  Could not find a version that satisfies the requirement MySQLdb (from versions: )
No matching distribution found for MySQLdb

Quand je regarde cela sur SO et d'autres endroits, il semble y avoir beaucoup de modules mysql différents.

En outre, voici à quoi ressemble ma configuration pour la base de données (je n’ai pas écrit ce projet et suis en grande partie nouveau sur Django et python):

    DATABASES = {
    'default': {
        'ENGINE': 'Django.db.backends.mysql',

...

    }
}

EdBaker a suggéré pip install MySQL-python

J'ai eu ceci comme réponse:

Collecting MySQL-python   Using cached MySQL-python-1.2.5.Zip
    Complete output from command python setup.py Egg_info:
    sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 20, in <module>
      File "/private/var/folders/m3/11zknyw55zxbw6zqh58rwq580000gn/T/pip-build-DC9tSL/MySQL-python/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 25, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found

    ---------------------------------------- Command "python setup.py Egg_info" failed with error code 1 in /private/var/folders/m3/11zknyw55zxbw6zqh58rwq580000gn/T/pip-build-DC9tSL/MySQL-python

Tentative d'exporter MySQLdb dans le shell:

[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb

Tentative de mise à niveau pip:

pip install pip --upgrade

a montré que pip était à jour

Requirement already up-to-date: pip in /Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages
8
Bren

Installer clairement pip install MySQL-python est la voie à suivre. Le problème est avec l'emplacement de mysql_config. Regardez autour de vous ici, cela pourrait vous aider selon votre système d'exploitation: mysql_config introuvable lors de l'installation de l'interface python de mysqldb

16
fodma1

J'ai suivi ma question ici Erreur 404 lors de l’utilisation de brew pour installer mysql

puis, comme suggéré par fodma1, nous sommes allés ici

et maintenant cela fonctionne pour la plupart (avoir une autre erreur mais c'est pour un autre post)! 

Merci EdBaker et fodma1!

0
Bren

brasser installer mysql

Le chemin sera affiché une fois l'installation terminée, utilisez la commande ci-dessous: 

Sudo PATH =/usr/local/Cellar/mysql/bin /: $ PATH pip installer mysql-python

0
user3763076