安装python依赖包psycopg2来调用postgresql的操作


Posted in Python onJanuary 01, 2021

1、先安装psycopg2的依赖组件

本案例的操作系统为linux red hat

在安装python依赖包psycopg之前,你必须需要先安装postgresql数据库的相关组件:

postgresql-devel,postgresql,postgresql-libs这三个组件比较重要。另外,可选组件:postgresql-server

安装上述组件之前,可以用命令来查看你的系统现在是否已经安装了相关组件:

[root@dthost27 ~]# rpm -qa | grep PostgreSQL

如果都没有安装,则可执行命令如下:

[root@dthost27 ~]# yum install postgresql-devel

(安装过程中会顺带安装上postgresql和postgresql-libs组件)

2、安装psycopg2依赖包

保证依赖组件存在后,就可以使用pip命令安装了:

[root@dthost27 ~]# pip install psycopg2-binary

注:这里安装的是binary格式的psycopg2依赖包,其实安装psycopg2也可以,但是有时候执行pip install psycopg2会报错,而安装psycopg2-binary(编译后)则不会

启动python测试

import psycopg2

补充:安装psycopg2报错_解决方案

报错信息

(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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。如有错误或未考虑完全的地方,望不吝赐教。

Python 相关文章推荐
Python简单操作sqlite3的方法示例
Mar 22 Python
Python实现在线暴力破解邮箱账号密码功能示例【测试可用】
Sep 06 Python
1 行 Python 代码快速实现 FTP 服务器
Jan 25 Python
python调用Matplotlib绘制分布点并且添加标签
May 31 Python
Python文件常见操作实例分析【读写、遍历】
Dec 10 Python
详解Django项目中模板标签及模板的继承与引用(网站中快速布置广告)
Mar 27 Python
下载与当前Chrome对应的chromedriver.exe(用于python+selenium)
Jan 14 Python
Tensorflow: 从checkpoint文件中读取tensor方式
Feb 10 Python
pycharm实现在子类中添加一个父类没有的属性
Mar 12 Python
浅析Python 序列化与反序列化
Aug 05 Python
python中_del_还原数据的方法
Dec 09 Python
python利用while求100内的整数和方式
Nov 07 Python
python matlab库简单用法讲解
Dec 31 #Python
Pytorch1.5.1版本安装的方法步骤
Dec 31 #Python
python爬虫基础之urllib的使用
Dec 31 #Python
python opencv实现直线检测并测出倾斜角度(附源码+注释)
Dec 31 #Python
Python实现Appium端口检测与释放的实现
Dec 31 #Python
python 基于selectors库实现文件上传与下载
Dec 31 #Python
python中Mako库实例用法
Dec 31 #Python
You might like
php数据入库前清理 注意php intval与mysql的int取值范围不同
2010/12/12 PHP
php上传图片存入数据库示例分享
2014/03/11 PHP
php加密之discuz内容经典加密方式实例详解
2017/02/04 PHP
破除一些网站复制、右键限制
2006/11/04 Javascript
Javascript typeof 用法
2008/12/28 Javascript
jquery JSON的解析方式
2009/07/25 Javascript
jQuery学习5 jQuery事件模型
2010/02/07 Javascript
jquery下动态显示jqGrid以及jqGrid的属性设置容易出现问题的解决方法
2010/10/22 Javascript
javascript的数据类型、字面量、变量介绍
2012/05/23 Javascript
js实现头像图片切割缩放及无刷新上传图片的方法
2015/07/17 Javascript
jquery实现的3D旋转木马特效代码分享
2015/08/25 Javascript
浅谈js里面的InttoStr和StrtoInt
2016/06/14 Javascript
利用JS判断鼠标移入元素的方向
2016/12/11 Javascript
老生常谈js中0到底是 true 还是 false
2017/03/08 Javascript
Angular X中使用ngrx的方法详解(附源码)
2017/07/10 Javascript
vue中将html字符串转换成html后遇到的问题小结
2018/12/10 Javascript
jQuery实现判断滚动条滚动到document底部的方法分析
2019/08/27 jQuery
vue npm install 安装某个指定的版本操作
2020/08/11 Javascript
vue自定义树状结构图的实现方法
2020/10/18 Javascript
python使用sorted函数对列表进行排序的方法
2015/04/04 Python
Python图像灰度变换及图像数组操作
2016/01/27 Python
Python入门_浅谈逻辑判断与运算符
2017/05/16 Python
Python实现图片尺寸缩放脚本
2018/03/10 Python
租租车:国际租车、美国租车、欧洲租车、特价预订国外租车(中文服务)
2018/03/28 全球购物
中专自我鉴定范文
2013/10/16 职场文书
售后求职信范文
2014/03/15 职场文书
公共场所禁烟标语
2014/06/25 职场文书
教师三严三实对照检查材料
2014/09/25 职场文书
小学教师师德整改措施
2014/09/29 职场文书
四风查摆剖析材料
2014/10/10 职场文书
2014年调度员工作总结
2014/11/19 职场文书
广告公司文案策划岗位职责
2015/04/14 职场文书
2015年技术工作总结范文
2015/04/20 职场文书
教师岗位说明书
2015/09/30 职场文书
靠谱准确的求职信
2019/04/02 职场文书
CSS实现鼠标悬浮动画特效
2023/05/07 HTML / CSS