03_mysql-python模块, linux环境下python2,python3的

---恢复内容开始---

1、Python2 正常

[root@IP ~]# pip install mysql-python 
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Requirement already satisfied: mysql-python in /usr/lib64/python2.7/site-packages (1.2.5)

# 测试
[root@IP ~]# python2
Python 2.7.5 (default, Aug  7 2019, 00:51:29) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> 

有时候,上面的包安装好了,导入失败

另外的

[root@IP ~]# pip install mysqlclient

[root@IP ~]# pip list
mysqlclient                      1.4.6


# 成功
[root@IP ~]# python
Python 2.7.5 (default, Aug  7 2019, 00:51:29) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> 

2、说明

python2中支持 MySQLdb

python3为PyMySQL或

 

python3中mysqldb不再支持,包括mysql-python是安装不上的。

转为Pymysql

命令:

pip3 install PyMySQL


使用及安装参考链接:

https://www.runoob.com/python3/python3-mysql.html

 

3、python3安装PyMySQL

[root@IP mysqlclient-1.4.6]# pip3 install PyMySQL

[root@IP mysqlclient-1.4.6]# python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymysql

 

4、python3 安装 mysqlclient

失败1

[root@localhost ~]# pip3 install mysqlclient
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting mysqlclient
  Using cached https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz
    Complete output from command python setup.py egg_info:
    /bin/sh: mysql_config: command not found
    /bin/sh: mariadb_config: command not found
    /bin/sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-bpt1pa2k/mysqlclient/setup.py", line 16, in <module>
        metadata, options = get_config()
      File "/tmp/pip-build-bpt1pa2k/mysqlclient/setup_posix.py", line 61, in get_config
        libs = mysql_config("libs")
      File "/tmp/pip-build-bpt1pa2k/mysqlclient/setup_posix.py", line 29, in mysql_config
        raise EnvironmentError("%s not found" % (_mysql_config_path,))
    OSError: mysql_config not found
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-bpt1pa2k/mysqlclient/

 

按照网上

更新pip3 和 setuptools

[root@localhost ~]# pip3 install --upgrade setuptools
[root@localhost ~]# python3 -m pip install --upgrade pip

 

继续失败

[root@localhost ~]# pip3 install mysqlclient
Traceback (most recent call last):
  File "/usr/bin/pip3", line 16, in <module>
    sys.exit(main())
TypeError: 'module' object is not callable

安装mysql依赖

 https://www.jianshu.com/p/5b6deb15bd21

yum install mysql-devel gcc gcc-devel python-devel

 

继续安装

  [root@localhost ~/mysqlclient-1.4.6]# python3 setup.py install
  [Errno 2] No such file or directory: 
'/usr/local/lib64/python3.6/site-packages/test-easy-install-35198.write-test'

The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    /usr/local/lib64/python3.6/site-packages/

This directory does not currently exist.  Please create it and try again, or
choose a different installation directory (using the -d or --install-dir
option).

 

报错

创建该目录

 

失败1.1

deepin失败

/bin/sh: 1: mysql_config: not found
Traceback (most recent call last):
  File "setup.py", line 16, in <module>
    metadata, options = get_config()
  File "/home/redhat/Desktop/pycharmProject/base-api/mysqlclient-1.4.6/setup_posix.py", line 61, in get_config
    libs = mysql_config("libs")
  File "/home/redhat/Desktop/pycharmProject/base-api/mysqlclient-1.4.6/setup_posix.py", line 29, in mysql_config
    raise EnvironmentError("%s not found" % (_mysql_config_path,))
OSError: mysql_config not found

 

 安装这个,找不到,

sudo apt-get install libmysqlclient-dev

https://blog.csdn.net/sduwgl/article/details/47814601

更新apt源试试

https://juejin.im/post/5d5f93156fb9a06b093622a9

 

然后可以安装成功

 

 

 

 

失败2

 使用pip install mysqlclient命令安装mysqlclient失败?

安装mysqlclient报错如下

 

1、下载本地whl,安装

blog:https://blog.csdn.net/cn_1937/article/details/81533544

https://blog.csdn.net/liuweiyuxiang/article/details/80666773

# 下载包
[root@IP mysqlclient-1.4.6]# wget 
https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz

# 解压
[root@IP ~]# tar xvf mysqlclient-1.4.6.tar.gz


# 安装保存  缺少gcc
[root@IP mysqlclient-1.4.6]# python3 setup.py install

MySQLdb/_mysql.c:38:20: fatal error: Python.h: No such file or directory
 #include "Python.h"
                    ^
compilation terminated.
error: command 'gcc' failed with exit status 1

 

 

2、安装python3-devel

参考 https://stackoverflow.com/questions/11094718/error-command-gcc-failed-with-exit-status-1-while-installing-eventlet

https://my.oschina.net/u/1433006/blog/1612797

# 安装成功依赖
[root@IP mysqlclient-1.4.6]# yum install python3-devel

 

重装成功

[root@IP mysqlclient-1.4.6]# python3 setup.py install
或者
[root@IP mysqlclient-1.4.6]# pip3 install mysqlclient

 

 

导入有问题

# 导入有问题
[root@IP mysqlclient-1.4.6]# python3
Python 3.6.8 (default, Aug  7 2019, 17:28:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/mysqlclient-1.4.6/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: cannot import name '_mysql'

 

 

3、MySQLdb导入问题

 

 在包安装的时候,会默认在包下的路径,error

 

 

 

---恢复内容结束---

posted @ 2019-11-28 15:50  venicid  阅读(919)  评论(0编辑  收藏  举报