(python部落格開發)window7上Error loading MySQLdb module: No module named MySQLdb解決辦法
阿新 • • 發佈:2019-01-25
在windows環境中(win7),建立虛擬環境,django建好模型準備遷移資料,執行命令:,出現如下錯誤:
(virtualenv) D:\workspace\blog_project>python manage.py makemigrationsTraceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "D:\workspace\maiziblog\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_com
mand_line
utility.execute()
File "D:\workspace\maiziblog\lib\site-packages\django\core\management\__init__.py", line 312, in execute
django.setup()
File "D:\workspace\maiziblog\lib\site-packages\django\__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\workspace\maiziblog\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models(all_models)
File "D:\workspace\maiziblog\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "D:\workspace\maiziblog\lib\site-packages\django\contrib\auth\models.py", line 41, in <module>
class Permission(models.Model):
File "D:\workspace\maiziblog\lib\site-packages\django\db\models\base.py", line 139, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "D:\workspace\maiziblog\lib\site-packages\django\db\models\base.py", line 324, in add_to_class
value.contribute_to_class(cls, name)
File "D:\workspace\maiziblog\lib\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 "D:\workspace\maiziblog\lib\site-packages\django\db\__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "D:\workspace\maiziblog\lib\site-packages\django\db\utils.py", line 240, in __getitem__
backend = load_backend(db['ENGINE'])
File "D:\workspace\maiziblog\lib\site-packages\django\db\utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "D:\workspace\maiziblog\lib\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
提示 No module named MySQLdb,百度了一下說需要:pip install mysqlclient,然後執行該命令
(maiziblog) D:\workspace\blog_project>pip install mysqlclientCollecting mysqlclient
Using cached mysqlclient-1.3.12.tar.gz
Building wheels for collected packages: mysqlclient
Running setup.py bdist_wheel for mysqlclient ... error
Complete output from command d:\workspace\maiziblog\scripts\python.exe -u -c "import setuptools, tokenize;__file_
_='c:\\users\\admini~1\\appdata\\local\\temp\\pip-build-sufwv_\\mysqlclient\\setup.py';f=getattr(tokenize, 'open',
open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d
c:\users\admini~1\appdata\local\temp\tmpyakxrkpip-wheel- --python-tag cp27:
running bdist_wheel
running build
running build_py
creating build
creating build\lib.win-amd64-2.7
copying _mysql_exceptions.py -> build\lib.win-amd64-2.7
creating build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\__init__.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\compat.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\connections.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\converters.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\cursors.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\release.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\times.py -> build\lib.win-amd64-2.7\MySQLdb
creating build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\CR.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\ER.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-2.7\MySQLdb\constants
running build_ext
building '_mysql' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
C:\Users\Administrator\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /
nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 "-IC:\Program Files (x86)\M
ySQL\MySQL Connector C 6.1\include" -Ic:\python27\include -Id:\workspace\maiziblog\PC /Tc_mysql.c /Fobuild\temp.win
-amd64-2.7\Release\_mysql.obj /Zl
_mysql.c
_mysql.c(29) : fatal error C1083: Cannot open include file: 'mysql.h': No such file or directory
error: command 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0
\\VC\\Bin\\amd64\\cl.exe' failed with exit status 2
----------------------------------------
Failed building wheel for mysqlclient
Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
Running setup.py install for mysqlclient ... error
Complete output from command d:\workspace\maiziblog\scripts\python.exe -u -c "import setuptools, tokenize;__fil
e__='c:\\users\\admini~1\\appdata\\local\\temp\\pip-build-sufwv_\\mysqlclient\\setup.py';f=getattr(tokenize, 'open'
, open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --re
cord c:\users\admini~1\appdata\local\temp\pip-djjlqo-record\install-record.txt --single-version-externally-managed
--compile --install-headers d:\workspace\maiziblog\include\site\python2.7\mysqlclient:
running install
running build
running build_py
creating build
creating build\lib.win-amd64-2.7
copying _mysql_exceptions.py -> build\lib.win-amd64-2.7
creating build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\__init__.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\compat.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\connections.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\converters.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\cursors.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\release.py -> build\lib.win-amd64-2.7\MySQLdb
copying MySQLdb\times.py -> build\lib.win-amd64-2.7\MySQLdb
creating build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\CR.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\ER.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-2.7\MySQLdb\constants
copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-2.7\MySQLdb\constants
running build_ext
building '_mysql' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
C:\Users\Administrator\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c
/nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 "-IC:\Program Files (x86)
\MySQL\MySQL Connector C 6.1\include" -Ic:\python27\include -Id:\workspace\maiziblog\PC /Tc_mysql.c /Fobuild\temp.w
in-amd64-2.7\Release\_mysql.obj /Zl
_mysql.c
_mysql.c(29) : fatal error C1083: Cannot open include file: 'mysql.h': No such file or directory
error: command 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9
.0\\VC\\Bin\\amd64\\cl.exe' failed with exit status 2
----------------------------------------
Command "d:\workspace\maiziblog\scripts\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\admini~1
\\appdata\\local\\temp\\pip-build-sufwv_\\mysqlclient\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f
.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record c:\users\admini~1\a
ppdata\local\temp\pip-djjlqo-record\install-record.txt --single-version-externally-managed --compile --install-head
ers d:\workspace\maiziblog\include\site\python2.7\mysqlclient" failed with error code 1 in c:\users\admini~1\appdat
a\local\temp\pip-build-sufwv_\mysqlclient\
(maiziblog) D:\workspace\blog_project>pip install --upgrade wheel
Requirement already up-to-date: wheel in d:\workspace\maiziblog\lib\site-packages
(maiziblog) D:\workspace\blog_project>mysql version
ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)
安裝不上,後來我通過如下途徑的到解決,在http://www.codegood.com/archives/129 下載安裝 MySQL-python-1.2.3.win-amd64-py2.7.exe
安裝完成之後問題完美解決:
2018-01-08 17:33:50,447 [MainThread:9528] [django.db.backends:89] [utils:execute] [DEBUG]- (0.003) SET SQL_AUTO_IS_
NULL = 0; args=None
Migrations for 'blog':
0001_initial.py:
- Create model User
- Create model Ad
- Create model Article
- Create model Category
- Create model Comment
- Create model Links
- Create model Tag
- Add field category to article
- Add field tag to article
- Add field user to article
(virtualenv) D:\workspace\blog_project>