在 CentOS 上为 Django 项目安装 python 3.6 mysqlclient

Posted

技术标签:

【中文标题】在 CentOS 上为 Django 项目安装 python 3.6 mysqlclient【英文标题】:Install python 3.6 mysqlclient for Django project on CentOS 【发布时间】:2018-01-15 09:50:05 【问题描述】:

我已经在我的 CentOS 6.9 上成功安装了一个 Django 项目。我已经在使用 python3.6 python3.6 -m venv env 创建的虚拟环境中安装了项目我已经激活了虚拟环境,现在我正在尝试使用运行应用程序;

python manage.py runserver 0.0.0.0:8000

当我运行此命令时,我收到以下错误;

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f7f7c5d67b8>
Traceback (most recent call last):
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 26, in <module>
    import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/REMOVED/REMOVED/python3.6/softwareix/storefront/models.py", line 10, in <module>
    class Brand(models.Model):
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/db/models/base.py", line 124, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/db/models/base.py", line 331, in add_to_class
    value.contribute_to_class(cls, name)
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/db/models/options.py", line 214, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/db/utils.py", line 211, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/db/utils.py", line 115, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/REMOVED/REMOVED/python3.6/env/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 30, in <module>
    'Did you install mysqlclient or MySQL-python?' % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'.
Did you install mysqlclient or MySQL-python?

我发现我缺少 Python mysql 扩展或驱动程序。我已尝试按照建议安装它。当我跑步时; pip install mysqlclient我明白了;

Collecting mysqlclient
  Using cached mysqlclient-1.3.10.tar.gz
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... error
    Complete output from command /REMOVED/REMOVED/python3.6/env/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-7g3likz_/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 /tmp/pip-y1mak800-record/install-record.txt --single-version-externally-managed --compile --install-headers /REMOVED/REMOVED/python3.6/env/include/site/python3.6/mysqlclient:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.6
    copying _mysql_exceptions.py -> build/lib.linux-x86_64-3.6
    creating build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-3.6/MySQLdb
    creating build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    running build_ext
    building '_mysql' extension
    creating build/temp.linux-x86_64-3.6
    gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,3,10,'final',0) -D__version__=1.3.10 -I/usr/include/mysql -I/REMOVED/REMOVED/python3.6/env/include -I/usr/local/include/python3.6m -c _mysql.c -o build/temp.linux-x86_64-3.6/_mysql.o
    gcc: error trying to exec 'as': execvp: Permission denied
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/REMOVED/REMOVED/python3.6/env/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-7g3likz_/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 /tmp/pip-y1mak800-record/install-record.txt --single-version-externally-managed --compile --install-headers /REMOVED/REMOVED/python3.6/env/include/site/python3.6/mysqlclient" failed with error code 1 in /tmp/pip-build-7g3likz_/mysqlclient/

这似乎不起作用。搜索互联网后,我尝试使用 yum 安装多个软件包。我尝试安装yum install python3-devel,但这不可用(No package python3-devel available.),我看到它应该在 epel 存储库中。所以我做了yum search python dev,我得到了这个;

dbus-python-devel.x86_64 : Libraries and headers for dbus-python
gstreamer-python-devel.x86_64 : Headers for developing programs that will use gstreamer-python
libimobiledevice-python.x86_64 : Python bindings for libimobiledevice
pyldb-devel.x86_64 : Development files for the Python bindings for the LDB library
python-behave.noarch : Tools for the behavior-driven development, Python style
python-cherrypy.noarch : Pythonic, object-oriented web development framework
python-cherrypy2.noarch : Pythonic, object-oriented web development framework
python-devel.x86_64 : The libraries and header files needed for Python development
python-devel.i686 : The libraries and header files needed for Python development
python-igraph-devel.x86_64 : Development files for igraph
python-imaging-devel.x86_64 : Development files for python-imaging
python-mozdevice.noarch : Mozilla-authored device management
python-pyblock.x86_64 : Python modules for dealing with block devices
python-pyldns.x86_64 : Header files, libraries and development documentation for ldns.
python-pyside-devel.i686 : Development files for python-pyside
python-pyside-devel.x86_64 : Development files for python-pyside
python-tools.x86_64 : A collection of development tools included with Python
python34-devel.i686 : Libraries and header files needed for Python 3 development
python34-devel.x86_64 : Libraries and header files needed for Python 3 development
wxPython-devel.i686 : Development files for wxPython add-on modules
wxPython-devel.x86_64 : Development files for wxPython add-on modules
python-pycxx-devel.noarch : PyCXX header and source files

这是我迄今为止尝试过的。我正在尝试让我的 Django 应用在 MySQL 上运行,但似乎没有安装扩展或模块。

【问题讨论】:

【参考方案1】:

gcc: error trying to exec 'as': execvp: Permission denied 诊断非常清楚。使用which as 验证您没有在$PATH 上放置另一个名为as 的文件。您希望能够成功编译一个简单的hello.c Hello World 程序,其中涉及gcc 生成汇编代码,以及as 将其转换为intel x86 目标代码。试试这个:

sudo yum install gcc binutils-devel

通过运行which as; as --version 并看到类似“GNU assembler (GNU Binutils for Ubuntu) 2.27”这样的横幅来验证它是否成功。

【讨论】:

运行sudo yum install gcc binutils-devel成功后; which as 显示; /usr/bin/as 对于as --version 我得到类似的东西; GNU assembler version 2.20.51.0.2-5.47.el6_9.1 20100205 .. This assembler was configured for a target of x86_64-redhat-linux'.` 呃! - 根据这个 lynis 建议,我确实对 as 的权限有限:cisofy.com/lynis/controls/HRDN-7222【参考方案2】:

您目前没有pip install mysqlclient 的权限。由于这是 centos,您可能不想使用pip(除非您设置了virtualenv)。你想跑yum install mysql-python

【讨论】:

运行yum install mysql-python 返回; No package mysql-python available. 我想使用我使用 python 3.6 设置的 virtualenv 以root身份登录并激活virtualenv,然后运行pip install mysqlclient 我设法安装了mysqlclient。所以我只是没有权限。如何授予我的 Cpanel 用户运行 pip install mysqlclient 的权限 啊。以 root 身份安装 virtualenv 可能是您的问题。您需要查看 cpanel 以哪个用户身份运行(我认为它只是 cpanel),并授予他们对 virtualenv 的权限。我可能会做的是:执行 pip freeze 以查看安装了哪些软件包,然后使用 cpanel 用户(sudo su cpanel 或 sudo -u cpanel bash 以该用户身份打开 shell)创建一个新的 virtualenv。

以上是关于在 CentOS 上为 Django 项目安装 python 3.6 mysqlclient的主要内容,如果未能解决你的问题,请参考以下文章

nginx + uwsgi 发布django项目!(linux为centos7)

在 Mac OS X Sierra 上为 Django Python 安装 mysqlclient

如何在未配置的 Docker 上为 centos 7 删除/安装 docker 映像

django项目之集成FastDFS 分布式存储

在 Linux Redhat/CentOS 上为 msodbcsql17 安装 unixODBC >= 2.3.1

在 Centos 7.6 上为 PostgreSQL 安装 PHP 7.4 PDO 驱动程序