Fix lỗi “Fatal error: Python.h: No such file or Directory” khi xài pip

Fix lỗi “Fatal error: Python.h: No such file or Directory” khi xài pipCuongquach.com | Bạn gặp tình huống như sau trên hệ thống OS Ubuntu/Debian, bạn cài đặt 1 chương trình hoặc module bên thứ 3 cho python hệ thống bằng chương trình ‘pip‘ thì gặp phải lỗi “Fatal error: Python.h: No such file or Directory” như ví dụ dưới.

# pip install pycurl
..
..
gcc -pthread -fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes -fPIC -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/usr/include/python2.7 -c src/MD2.c -o build/temp.linux-x86_64-2.7/src/MD2.o

src/MD2.c:31:20: fatal error: Python.h: No such file or directory

compilation terminated.

error: command 'gcc' failed with exit status 1

 
Bạn sẽ ngờ ngợ người ra và suy nghĩ làm sao để tìm ra file ‘Python.h‘ và xử lý cái lỗi này bây giờ đây.

Fix lỗi “fatal error: Python.h: No such file or Directory”

Python.h chỉ là 1 file header, được sử dụng bởi chương trình compile ‘gcc‘ để build chương trình/ứng dụng. Bạn sẽ cần phải cài đặt gói chương trình thư viện có tên là ‘python-dev‘. Gói chương trình này sẽ bao gồm các file header, file thư viện tĩnh và các chương trình phát triển cần thiết cho việc xây dựng module Python, mở rộng trình thông dịch Python,… Vậy bạn sẽ cài gói python-dev như sau :

+ Python version 2.x+

# sudo apt-get install python-dev

+ Python version 3.x+

# sudo apt-get install python3-dev

Ví dụ như output khi cài đặt python-dev :

# sudo apt-get install python-dev -y
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following extra packages will be installed:
libexpat1-dev libssl-dev libssl-doc python2.7-dev zlib1g-dev
The following NEW packages will be installed:
libexpat1-dev libssl-dev libssl-doc python-dev python2.7-dev zlib1g-dev
0 upgraded, 6 newly installed, 0 to remove and 2 not upgraded.
Need to get 32.5 MB of archives.
After this operation, 48.2 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://mirrors.service.networklayer.com/ubuntu/ trusty-updates/main libexpat1-dev amd64 2.0.1-7.2ubuntu1.1 [216 kB]
Get:2 http://mirrors.service.networklayer.com/ubuntu/ trusty/main zlib1g-dev amd64 1:1.2.3.4.dfsg-3ubuntu4 [165 kB]
Get:3 http://mirrors.service.networklayer.com/ubuntu/ trusty-updates/main libssl-dev amd64 1.0.1-4ubuntu5.5 [1,525 kB]
Get:4 http://mirrors.service.networklayer.com/ubuntu/ trusty-updates/main libssl-doc all 1.0.1-4ubuntu5.5 [1,034 kB]
Get:5 http://mirrors.service.networklayer.com/ubuntu/ trusty-updates/main python2.7-dev amd64 2.7.3-0ubuntu3.1 [29.5 MB]
Get:6 http://mirrors.service.networklayer.com/ubuntu/ trusty/main python-dev amd64 2.7.3-0ubuntu2 [1,088 B]
Fetched 32.5 MB in 1s (21.4 MB/s) 
Selecting previously unselected package libexpat1-dev.
(Reading database ... 55152 files and directories currently installed.)
Unpacking libexpat1-dev (from .../libexpat1-dev_2.0.1-7.2ubuntu1.1_amd64.deb) ...
Selecting previously unselected package zlib1g-dev.
Unpacking zlib1g-dev (from .../zlib1g-dev_1%3a1.2.3.4.dfsg-3ubuntu4_amd64.deb) ...
Selecting previously unselected package libssl-dev.
Unpacking libssl-dev (from .../libssl-dev_1.0.1-4ubuntu5.5_amd64.deb) ...
Selecting previously unselected package libssl-doc.
Unpacking libssl-doc (from .../libssl-doc_1.0.1-4ubuntu5.5_all.deb) ...
Selecting previously unselected package python2.7-dev.
Unpacking python2.7-dev (from .../python2.7-dev_2.7.3-0ubuntu3.1_amd64.deb) ...
Selecting previously unselected package python-dev.
Unpacking python-dev (from .../python-dev_2.7.3-0ubuntu2_amd64.deb) ...
Processing triggers for man-db ...
Setting up libexpat1-dev (2.0.1-7.2ubuntu1.1) ...
Setting up zlib1g-dev (1:1.2.3.4.dfsg-3ubuntu4) ...
Setting up libssl-dev (1.0.1-4ubuntu5.5) ...
Setting up libssl-doc (1.0.1-4ubuntu5.5) ...
Setting up python2.7-dev (2.7.3-0ubuntu3.1) ...
Setting up python-dev (2.7.3-0ubuntu2) ...

Bây giờ tiến hành cài đặt lại chương trình hay module thông qua pip thử xem nào.

Nguồn: https://cuongquach.com/

1
2
Previous article[Ebook] Thinking in Java 3rd Edition – Download PDF
Next article[Zabbix] Hướng dẫn cài đặt Zabbix Agent trên CentOS/Ubuntu/Debian
Bạn đang theo dõi website "https://cuongquach.com/" nơi lưu trữ những kiến thức tổng hợp và chia sẻ cá nhân về Quản Trị Hệ Thống Dịch Vụ & Mạng, được xây dựng lại dưới nền tảng kinh nghiệm của bản thân mình, Quách Chí Cường. Hy vọng bạn sẽ thích nơi này !