Python2.7 CentOS7 处理报错 ImportError: No module named psycopg2

运行出错:ImportError: No module named psycopg2
import psycopg2
Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named psycopg2

安装出错:

[root@localhost pyscript]# pip install psycopg2
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.

Looking in indexes: http://mirrors.cloud.aliyuncs.com/pypi/simple/
Collecting psycopg2
Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/fd/ae/98cb7a0cbb1d748ee547b058b14604bd0e9bf285a8e0cc5d148f8a8a952e/psycopg2-2.8.6.tar.gz (383 kB)
|████████████████████████████████| 383 kB 15.5 MB/s
ERROR: Command errored out with exit status 1:
command: /usr/bin/python -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘”‘”‘/tmp/pip-install-zXoKIr/psycopg2/setup.py'”‘”‘; file='”‘”‘/tmp/pip-install-zXoKIr/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-pip-egg-info-1DZX3c
cwd: /tmp/pip-install-zXoKIr/psycopg2/
Complete output (25 lines):
/usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: ‘project_urls’
warnings.warn(msg)
running egg_info
creating /tmp/pip-pip-egg-info-1DZX3c/psycopg2.egg-info
writing /tmp/pip-pip-egg-info-1DZX3c/psycopg2.egg-info/PKG-INFO
writing top-level names to /tmp/pip-pip-egg-info-1DZX3c/psycopg2.egg-info/top_level.txt
writing dependency_links to /tmp/pip-pip-egg-info-1DZX3c/psycopg2.egg-info/dependency_links.txt
writing manifest file ‘/tmp/pip-pip-egg-info-1DZX3c/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.

……………………………………

ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output


$ wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
$ tar xf Python-2.7.6.tar.xz
$ cd Python-2.7.6
$ ./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
$ make && make altinstall
$ yum install postgresql-libs

$ wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py

$ python2.7 ez_setup.py

$ easy_install-2.7 psycopg2

安装 ez_setup.py setuptools


可以先打开setuptools的python官网看看setuptools软件包如何安装:

> wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py > python ez_setup.py --user

也可以下载setuptools软件包后安装:
(1)下载setuptools包

(2)解压setuptools包

(3)切换到当前文件夹

(4)编译setuptools

(5)开始执行setuptools安装


[root@localhost pyscript]
Python 2.7.5 (default, Jun 20 2019, 20:27:34)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> import psycopg2
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named psycopg2

解决步骤 1、

sudo  pip install psycopg2

解决步骤 2、

sudo  pip install psycopg2-binary

完成。

Original: https://blog.csdn.net/sunny_day_day/article/details/122194845
Author: ac.char
Title: Python2.7 CentOS7 处理报错 ImportError: No module named psycopg2

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/509496/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球