django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

前端 未结 23 1033
夕颜
夕颜 2020-12-02 10:51

The problem Im facing while trying to connect to database for mysql. I have also given the database settings that i have used.

 Traceback (most recent call l         


        
相关标签:
23条回答
  • 2020-12-02 11:24

    If you get errors trying to install mysqlclient with pip, you may lack the mysql dev library. Install it by running:

    apt-get install libmysqlclient-dev
    

    and try again to install mysqlclient:

    pip install mysqlclient
    
    0 讨论(0)
  • 2020-12-02 11:28

    When I set up Django development environment for PyCharm in Mac OS X Mountain Lion with python, mysql, sequel pro application I got error same as owner of this thread. However, my answer for them who is running python-mysqldb under Mac OS Mountain Lion x86_x64 (MySql and Python also should be same architecture) and already tried everything like pip and etc. In order fix this problem do following steps:

    1. Download MySql for Python from here
    2. Untar downloaded file. In terminal window do following: tar xvfz downloade.tar.
    3. cd /to untared directory
    4. Run sudo python setup.py install
    5. If you get error something like this: "Environment Error: /usr/local/bin/mysql_config not found" then try to add path ass follows: "export PATH=$PATH:/usr/local/mysql/bin". But id did not helped to me and I found another solution. In the end of command execution error output which looks like this:

      File "/path_to_file/MySQL-python-1.2.4b4/setup_posix.py", line 25, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,))

    6. Open setup_posix.py with vim and go to line 25 (In your case it can be different unless if it is same version).

    7. Line 25 should look like this after your editing unless your mysql have symbolic link like follows '/usr/local/mysql/bin/':

      f = popen("%s --%s" % ('/usr/local/mysql/bin/mysql_config', what))

    8. After this I got another error as following:

      django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so Reason: image not found

    9. Finally I did following in console:

      sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

    Currently everything works fine. So I hope it will be helpful for somebody who uses Mac. :)

    0 讨论(0)
  • 2020-12-02 11:29

    For Ubuntu 16.04 and 18.04 or python 3 versions

    sudo apt-get install python3-mysqldb
    
    0 讨论(0)
  • 2020-12-02 11:34

    My answer is similar to @Ron-E, but I got a few more errors/corrections so I'm putting my steps below for Mac OSX on Mavericks and Python 2.7.6.

    1. Install Python mysql package (if you get a success message, then ignore the below steps)

      pip install mysql-python
      
    2. When I did the above, I got the error "EnvironmentError: mysql_config not found" enter image description here To fix this, I did the below in terminal:

      export PATH=$PATH:/usr/local/mysql/bin
      
    3. When I reran step 1, I get a new error "error: command 'cc' failed with exit status 1" enter image description here To fix this, I did the below in terminal:

       export CFLAGS=-Qunused-arguments
       export CPPFLAGS=-Qunused-arguments
      
    4. I reran step 1 and got the success message 'Successfully installed mysql-python'!

    0 讨论(0)
  • 2020-12-02 11:34

    It is because it did not find sql connector. try:

    pip install mysqlclient
    
    0 讨论(0)
  • 2020-12-02 11:34

    I've solved this issue in this environment:

    $ pyenv --version
    pyenv 1.2.9
    
    $ python --version
    Python 3.7.2
    
    $ python -m django --version
    2.1.7
    

    ./settings.py

    DATABASES = {
        'default': {
            'NAME': 'my_db_name',
            'ENGINE': 'mysql.connector.django',   # 'django.db.backends.mysql'
            'USER': '<user>',
            'PASSWORD': '<pass>',
            'HOST': 'localhost',
            'PORT': 3306,
            'OPTIONS': {
                'autocommit': True,
            },
        }
    }
    

    If you use 'ENGINE': 'mysql.connector.django' , install driver executing:

    $ pip install mysql-connector-python
    Successfully installed mysql-connector-python-8.0.15 protobuf-3.7.0 six-1.12.0
    

    Note that $ pip install mysql-pythondidn't work for me.

    Note that if you use 'ENGINE': 'django.db.backends.mysql' , you should install driver executing:
    $ pip install mysqlclient

    Finally execute:
    $ python manage.py migrate

    If it's all right, python creates all these tables id database:

    auth_group
    auth_group_permissions
    auth_permission
    auth_user
    auth_user_groups
    auth_user_user_permissions
    django_admin_log
    django_content_type
    django_migrations
    django_session
    
    0 讨论(0)
提交回复
热议问题