![](/img/trans.png)
[英]How to install Pip 3 and then Python Lib MySQLdb on Synology
[英]How to install Python MySQLdb module using pip?
這很容易做到,但很難記住正確的拼寫:
pip install mysqlclient
如果您需要 1.2.x 版本(僅限舊版 Python),請使用pip install MySQL-python
注意:運行上述命令時,可能必須存在一些依賴項。 關於如何在各種平台上安裝這些的一些提示:
sudo apt-get install python-pip python-dev libmysqlclient-dev
sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc
brew install mysql-connector-c
如果失敗,請嘗試
brew install mysql
從全新的 Ubuntu 14.04.2 系統開始,需要這兩個命令:
apt-get install python-dev libmysqlclient-dev
pip install MySQL-python
僅僅做“pip install”本身是行不通的。
來自http://codeinthehole.com/writing/how-to-set-up-mysql-for-python-on-ubuntu/
第一的
pip install pymysql
然后將下面的代碼放入__init__.py
( projectname/__init__.py
)
import pymysql
pymysql.install_as_MySQLdb()
我的環境是(python3.5,django1.10),這個解決方案對我有用!
希望這可以幫助!!
我通過 Pip 在 Windows 上安裝 64 位版本的 MySQLdb 時遇到問題(編譯源問題)[32 位版本安裝正常]。 設法從http://www.lfd.uci.edu/~gohlke/pythonlibs/提供的 .whl 文件中安裝已編譯的 MySQLdb
然后可以通過 pip 將 .whl 文件安裝為https://pip.pypa.io/en/latest/user_guide/#installing-from-wheels中的文檔
例如,如果您保存在C:/
中,您可以通過以下方式安裝
pip install c:/MySQL_python-1.2.5-cp27-none-win_amd64.whl
后續:如果你安裝了64位版本的Python,那么你想從上面的鏈接中安裝64位AMD版本的MySQLdb[即即使你有英特爾處理器]。 如果您嘗試安裝 32 位版本,我認為您會在下面的評論中看到不支持的輪子錯誤。
這對我有用:
pip install mysqlclient
這適用於 python 3.x
轉到pycharm,然后轉到默認設置--> pip(雙擊)--pymsqldb..--> 安裝--在這樣的程序中安裝使用后
import pymysql as MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","root","root","test" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("show tables")
# Fetch a single row using fetchone() method.
data = cursor.fetchall()
print (data)
# disconnect from server
db.close()
我嘗試了所有選項,但無法讓它在 Redhat 平台上運行。 我做了以下工作以使其工作:-
yum install MySQL-python -y
安裝軟件包后,就可以在解釋器中按以下方式導入模塊:-
>>> import MySQLdb
>>>
這里給出的許多答案都非常令人困惑,所以我會盡量簡單地說。 它幫助我安裝了這個
pip install pymysql
然后在python文件中使用以下命令
import pymysql as MySQLdb
這樣您就可以毫無問題地使用 MySQLdb。
如果你無法安裝mysqlclient你也可以安裝pymysql :
pip install pymysql
這與MySqldb相同。 之后全部使用 pymysql 而不是 MySQLdb
如果您使用的是 Raspberry Pi [Raspbian OS]
首先需要安裝 pip 命令
apt-get install python-pip
所以只需依次安裝
apt-get install python-dev libmysqlclient-dev
apt-get install python-pip
pip install MySQL-python
你可以去這個網站下載這個包。
我也有同樣的問題。如果你在 Windows 上,請按照這些步驟操作。 轉到:1.我的電腦 2.系統屬性 3.高級系統設置 4.在“高級”選項卡下單擊“環境變量”按鈕 5.然后在系統變量下,您必須添加/更改以下變量:PYTHONPATH和路徑。 這是我的變量的樣子:python 路徑:
C:\Python27;C:\Python27\Lib\site-packages;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\Python27\Scripts
小路:
C:\Program Files\MySQL\MySQL Utilities 1.3.5\;C:\Python27;C:\Python27\Lib\site-packages;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\Python27\Scripts
請參閱此鏈接以供參考
如果您的系統上安裝了 Windows,請在 cmd 上鍵入以下命令:
pip install mysql-connector
如果上述命令不起作用,請嘗試使用:
pip install mysql-connector-python
現在,如果上述命令沒有完成工作,請嘗試使用:
pip install mysql-connector-python-rf
就是這樣,你現在可以走了。
上面的答案很好,但是我們在Windows中使用pip安裝MySQL-python時可能會出現一些問題
例如,它需要一些與Visual Stdio相關的文件。一個解決方案是安裝VS2008或2010……顯然,它的成本太高了。
另一種方法是@bob90937 的答案。 我是來做點補充的。
通過http://www.lfd.uci.edu/~gohlke/pythonlibs ,您可以下載許多科學開源擴展包的 Windows 二進制文件,用於 Python 編程語言的官方 CPython 發行版。
回到主題,我們可以選擇MySQL-python(py2)或Mysqlclient(py3)並使用pip install進行安裝。 它給我們帶來了極大的便利!
對於 Python3,我需要這樣做:
python3 -m pip install MySQL
如果 pip3 不起作用,您可以嘗試:
sudo apt install python3-mysqldb
pip install mysql-connector-python
如文檔中所述:
https://dev.mysql.com/doc/connector-python/en/connector-python-installation-binary.html
在 RHEL 7 上:
sudo yum install yum-utils mariadb-devel python-pip python-devel gcc
sudo /bin/pip2 install MySQL-python
實際上,遵循@Nick T 的答案對我不起作用,我嘗試apt-get install python-mysqldb
為我工作
root@2fb0da64a933:/home/test_scrapy# apt-get install python-mysqldb
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libmariadbclient18 mysql-common
Suggested packages:
default-mysql-server | virtual-mysql-server python-egenix-mxdatetime python-mysqldb-dbg
The following NEW packages will be installed:
libmariadbclient18 mysql-common python-mysqldb
0 upgraded, 3 newly installed, 0 to remove and 29 not upgraded.
Need to get 843 kB of archives.
After this operation, 4611 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian stretch/main amd64 mysql-common all 5.8+1.0.2 [5608 B]
Get:2 http://deb.debian.org/debian stretch/main amd64 libmariadbclient18 amd64 10.1.38-0+deb9u1 [785 kB]
Get:3 http://deb.debian.org/debian stretch/main amd64 python-mysqldb amd64 1.3.7-1.1 [52.1 kB]
Fetched 843 kB in 23s (35.8 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package mysql-common.
(Reading database ... 13223 files and directories currently installed.)
Preparing to unpack .../mysql-common_5.8+1.0.2_all.deb ...
Unpacking mysql-common (5.8+1.0.2) ...
Selecting previously unselected package libmariadbclient18:amd64.
Preparing to unpack .../libmariadbclient18_10.1.38-0+deb9u1_amd64.deb ...
Unpacking libmariadbclient18:amd64 (10.1.38-0+deb9u1) ...
Selecting previously unselected package python-mysqldb.
Preparing to unpack .../python-mysqldb_1.3.7-1.1_amd64.deb ...
Unpacking python-mysqldb (1.3.7-1.1) ...
Setting up mysql-common (5.8+1.0.2) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Setting up libmariadbclient18:amd64 (10.1.38-0+deb9u1) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Setting up python-mysqldb (1.3.7-1.1) ...
root@2fb0da64a933:/home/test_scrapy# python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
我的環境是:
pip install mysqlclient-1.3.13-cp37-cp37m-win_amd64.whl
為我工作。
import MySQLdb, sys
# --------------------------------------------------
# Connect to MySQL
# --------------------------------------------------
try:
db = MySQLdb.connect(host="localhost", user="user", passwd="pass", db="database", charset='cp1251')
except MySQLdb.Error as e:
print ("Error %d: %s" % (e.args[0], e.args[1]))
sys.exit()
# Creating cursor
cursor = db.cursor()
請轉到該站點以根據python版本下載軟件包。 並使用pip install文件名,如我擁有python 3.7.1並下載紅色圓圈中的文件,並打開下載文件並安裝該文件的終端,例如pip install mysqlclient-1.4.4-cp37-cp37m-win32.whl
並在設置文件中
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'shoping_cart',
'HOST': 'localhost',
'USER': 'root',
'PASSWORD':'',
'PORT': '3306',
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.