Python "Traveling the Pit" (1) - Install psycopg2 in Linux system

series of articles


Team Blog: CSDN AI Group


1. Background

The psycopg2 library is a third-party library used by python to operate the PostgreSQL database.

Under the Linux system, if you install psycopg2 directly using pip, the installation will fail. Because pip only installs the Python interface of PostgreSQL, its bottom layer also needs to call the C language library of PostgreSQL, and this C language library needs to be installed with the system package manager on the Linux system.

2. Installation method

(1) Install the C language library that PostgreSQL depends on:

yum install -y postgresql-devel

(2) Install psycopg2

pip install psycopg2

3. Wading the pit

In the case that pip could not be installed, I directly searched for an installation tutorial on the Internet, as shown in the figure below:
insert image description here
This is a very detailed and practical tutorial, directly copy the command and execute it step by step, and finally the installation of psycopg2 is successful, but after a closer look, it is found that the default The environment was inexplicably changed from python3.8 to python2.7...

Looking for the reason, I found that in the third step of the tutorial to install python dependencies, python-devel distinguishes between python2 and python3 versions:

python2 corresponds to python-devel
python3 corresponds to python3-devel

And if you install python-devel directly, the default python environment will be changed to python2. However, you can directly change the default environment to python3 later. The following is a specific example, setting the local python3.8 environment as the default environment:

ln -s -f /usr/local/bin/pip3.8 /usr/bin/pip
ln -s -f /usr/local/bin/pip3.8 /usr/bin/pip3
ln -s -f /usr/local/bin/pip3.8 /usr/bin/pip3.8
ln -s -f /usr/local/bin/python3.8 /usr/bin/python
ln -s -f /usr/local/bin/python3.8 /usr/bin/python3
ln -s -f /usr/local/bin/python3.8 /usr/bin/python3.8

Among them, the function of the ln command is to establish a synchronous link for a certain file in another location. This link can be regarded as an alias of the file, similar to a reference in C++, and will not repeatedly occupy disk space. The syntax is as follows, and detailed explanations can be found in the references at the end of the article.

ln [参数][源文件或目录][目标文件或目录]

An experience can be summed up from the above example. The language used in the computer is an artificial language, and each instruction is completely deterministic. When using instructions, you must understand the meaning, usage environment, and scope of each instruction. Use it directly without fully understanding an instruction, otherwise it may have unexpected consequences.

References

Guess you like

Origin blog.csdn.net/u010280923/article/details/120257671