安装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实现汉诺塔方法汇总
Jul 25 Python
python代码实现ID3决策树算法
Dec 20 Python
python微信跳一跳系列之棋子定位像素遍历
Feb 26 Python
python距离测量的方法
Mar 06 Python
Python selenium抓取微博内容的示例代码
May 17 Python
python创建文件时去掉非法字符的方法
Oct 31 Python
详解PyCharm安装MicroPython插件的教程
Jun 24 Python
Django项目基础配置和基本使用过程解析
Nov 25 Python
Python命令行click参数用法解析
Dec 19 Python
解决python 在for循环并且pop数组的时候会跳过某些元素的问题
Dec 11 Python
Python 机器学习工具包SKlearn的安装与使用
May 14 Python
Python matplotlib绘制雷达图
Apr 13 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 各种排序算法实现代码
2009/08/20 PHP
php中理解print EOT分界符和echo EOT的用法区别小结
2010/02/21 PHP
php radio 单选框获取与保持值的实现代码
2010/05/15 PHP
PHP入门学习笔记之一
2010/10/12 PHP
JS中encodeURIComponent函数用php解码的代码
2012/03/01 PHP
php+mysqli使用面向对象方式更新数据库实例
2015/01/29 PHP
PHP标准类(stdclass)用法示例
2016/09/28 PHP
php+mysql+jquery实现简易的检索自动补全提示功能
2017/04/15 PHP
PHP结合jquery ajax实现上传多张图片,并限制图片大小操作示例
2019/03/01 PHP
读jQuery之二(两种扩展)
2011/06/11 Javascript
ionic+AngularJs实现获取验证码倒计时按钮
2017/04/22 Javascript
js弹性势能动画之抛物线运动实例详解
2017/07/27 Javascript
使用store来优化React组件的方法
2017/10/23 Javascript
jQuery发请求传输中文参数乱码问题的解决方案
2018/05/22 jQuery
JavaScript使用indexOf()实现数组去重的方法分析
2018/09/04 Javascript
30分钟快速实现小程序语音识别功能
2018/11/27 Javascript
「中高级前端面试」JavaScript手写代码无敌秘籍(推荐)
2019/04/08 Javascript
详解VSCode配置启动Vue项目
2019/05/14 Javascript
Python使用django获取用户IP地址的方法
2015/05/11 Python
Django实现图片文字同时提交的方法
2015/05/26 Python
Python中循环后使用list.append()数据被覆盖问题的解决
2018/07/01 Python
python中的数据结构比较
2019/05/13 Python
python元组和字典的内建函数实例详解
2019/10/22 Python
Python 防止死锁的方法
2020/07/29 Python
使用python-cv2实现视频的分解与合成的示例代码
2020/10/26 Python
利用CSS3把图片变成灰色模式的实例代码
2016/09/06 HTML / CSS
css3实现圆锥渐变conic-gradient效果
2020/02/12 HTML / CSS
美国打印机墨水和碳粉购物网站:QuikShip Toner
2018/08/29 全球购物
Linux管理员面试经常问道的相关命令
2014/12/12 面试题
工商管理应届生求职信
2013/10/07 职场文书
广告设计专业自荐信范文
2013/11/14 职场文书
高中生评语大全
2014/04/25 职场文书
2014年党员承诺书范文
2014/05/20 职场文书
2014党员干部四风问题对照检查材料思想汇报
2014/09/24 职场文书
新郎父母婚礼致辞
2015/07/27 职场文书
Python中的datetime包与time包包和模块详情
2022/02/28 Python