安裝python依賴(lài)包psycopg2來(lái)調(diào)用postgresql的操作
1、先安裝psycopg2的依賴(lài)組件
本案例的操作系統(tǒng)為linux red hat
在安裝python依賴(lài)包psycopg之前,你必須需要先安裝postgresql數(shù)據(jù)庫(kù)的相關(guān)組件:
postgresql-devel,postgresql,postgresql-libs這三個(gè)組件比較重要。另外,可選組件:postgresql-server
安裝上述組件之前,可以用命令來(lái)查看你的系統(tǒng)現(xiàn)在是否已經(jīng)安裝了相關(guān)組件:
[root@dthost27 ~]# rpm -qa | grep PostgreSQL
如果都沒(méi)有安裝,則可執(zhí)行命令如下:
[root@dthost27 ~]# yum install postgresql-devel
(安裝過(guò)程中會(huì)順帶安裝上postgresql和postgresql-libs組件)
2、安裝psycopg2依賴(lài)包
保證依賴(lài)組件存在后,就可以使用pip命令安裝了:
[root@dthost27 ~]# pip install psycopg2-binary
注:這里安裝的是binary格式的psycopg2依賴(lài)包,其實(shí)安裝psycopg2也可以,但是有時(shí)候執(zhí)行pip install psycopg2會(huì)報(bào)錯(cuò),而安裝psycopg2-binary(編譯后)則不會(huì)
啟動(dòng)python測(cè)試
import psycopg2
補(bǔ)充:安裝psycopg2報(bào)錯(cuò)_解決方案
報(bào)錯(cuò)信息
(python3-virtualenv) [root@vl-bg-anaylsis02 extract_log]# pip3 install psycopg2 Collecting psycopg2 Using cached psycopg2-2.8.4.tar.gz (377 kB) ERROR: Command errored out with exit status 1: command: /disk2/extract_log/python3-virtualenv/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-2d9wyu3k/psycopg2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-2d9wyu3k/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-2d9wyu3k/psycopg2/pip-egg-info cwd: /tmp/pip-install-2d9wyu3k/psycopg2/ Complete output (23 lines): running egg_info creating /tmp/pip-install-2d9wyu3k/psycopg2/pip-egg-info/psycopg2.egg-info writing /tmp/pip-install-2d9wyu3k/psycopg2/pip-egg-info/psycopg2.egg-info/PKG-INFO writing dependency_links to /tmp/pip-install-2d9wyu3k/psycopg2/pip-egg-info/psycopg2.egg-info/dependency_links.txt writing top-level names to /tmp/pip-install-2d9wyu3k/psycopg2/pip-egg-info/psycopg2.egg-info/top_level.txt writing manifest file '/tmp/pip-install-2d9wyu3k/psycopg2/pip-egg-info/psycopg2.egg-info/SOURCES.txt' Error: pg_config executable not found. pg_config is required to build psycopg2 from source. Please add the directory containing pg_config to the $PATH or specify the full executable path with the option: python setup.py build_ext --pg-config /path/to/pg_config build ... or with the pg_config option in 'setup.cfg'. If you prefer to avoid building psycopg2 from source, please install the PyPI 'psycopg2-binary' package instead. For further information please check the 'doc/src/install.rst' file (also at <http://initd.org/psycopg/docs/install.html>). ---------------------------------------- ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
解決方案 For ubuntu
sudo apt-get install libpq-dev python3-dev
解決方案 For Fedora/Centos
yum install -y postgresql10 yum install postgresql-libs python3-devel postgresql-devel yum install gcc
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
python 讀入多行數(shù)據(jù)的實(shí)例
下面小編就為大家分享一篇python 讀入多行數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Python實(shí)現(xiàn)高效求解素?cái)?shù)代碼實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)高效求解素?cái)?shù)代碼實(shí)例,本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-06-06Python 實(shí)現(xiàn)二叉查找樹(shù)的示例代碼
這篇文章主要介紹了Python 實(shí)現(xiàn)二叉查找樹(shù)的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12Windows中使用wxPython和py2exe開(kāi)發(fā)Python的GUI程序的實(shí)例教程
wxPython是一款集成了Python的圖形化類(lèi)庫(kù)的工具,而py2exe是一款將Python程序轉(zhuǎn)換為exe可執(zhí)行文件的程序,二者搭配可以輕松地在Windows中創(chuàng)建圖形化程序,這里我們就來(lái)學(xué)習(xí)Windows中使用wxPython和py2exe開(kāi)發(fā)Python的GUI程序的實(shí)例教程:2016-07-07Python Tkinter Entry和Text的添加與使用詳解
這篇文章主要介紹了Python Tkinter Entry和Text的添加與使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Python opencv缺陷檢測(cè)的實(shí)現(xiàn)及問(wèn)題解決
這篇文章主要介紹了Python opencv缺陷檢測(cè)的實(shí)現(xiàn)及問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04python實(shí)現(xiàn)簡(jiǎn)單銀行管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單銀行管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10Pyqt5將多個(gè)類(lèi)組合在一個(gè)界面顯示的完整示例
這篇文章主要給大家介紹了關(guān)于Pyqt5將多個(gè)類(lèi)組合在一個(gè)界面顯示的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09