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

 

 

 

---恢復內容結束---


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM