首先从python网站下载源码自己编译安装,本例子下载的是Python3.6.11。
下载之后,是个名为Python-3.6.11.tgz的压缩包,直接解压。
里头有个README.rst使用说明:
1.首先它给了几个python相关的几个重要网站,
- Website: https://www.python.org
- Source code: https://github.com/python/cpython
- Issue tracker: https://bugs.python.org
- Documentation: https://docs.python.org
- Developer's Guide: https://devguide.python.org/
2.其次,讲了下这个怎么编译, 就3个命令,照着执行即可。
Build Instructions
------------------
On Unix, Linux, BSD, macOS, and Cygwin::
./configure
make
make test
sudo make install
This will install Python as ``python3``.
You can pass many options to the configure script; run ``./configure --help``
to find out more. On macOS and Cygwin, the executable is called ``python.exe``;
elsewhere it's just ``python``.
编译完成后,发下目录下多了个python的可执行文件。
直接在该目录下执行python命令,会发现已经可以使用,如果为了方便,还可以更新/usr/bin/python软链接目标到此,当任何目录执行,使用的都是此版本python。
user@swd:/local/sdb/aosp$ python
Python 3.6.11 (default, Jul 22 2020, 20:53:03)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import ssl
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python3.6/Lib/ssl.py", line 101, in
import _ssl # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
>>>
这里有个问题,当py里边有import ssl时,发现会报错,如上。
这是找很多帖子说,需要安装openssl-devel, 而在Ubuntu则安装openssl和libssl-dev。见网友贴《Ubuntu安装openssl-devel》。
在这之前,先找到的是说要先修改python配置文件Setup.dist和Setup,修改之后再编译。
见贴《centos7中python3.6报错ModuleNotFoundError: No module named '_ssl'》,这篇帖子讲的配置文件不全,还有一个Module/Setup。
都需要将默认注释掉的关于ssl的5行取消,大约在53%处。
# CSV file helper
#_csv _csv.c
# Socket module helper for socket(2)
_socket socketmodule.c
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
编译的时候,发现会报错,即上边提到的没有安装openssl-devel的引起的问题。
报错如下:
user@swd:/local/sdb/tools/python3.6$ make
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -I. -I./Include -DPy_BUILD_CORE \
-DABIFLAGS='"m"' \
-DMULTIARCH=\"x86_64-linux-gnu\" \
-o Python/sysmodule.o ./Python/sysmodule.c
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -I. -I./Include -DPy_BUILD_CORE \
-DSOABI='"cpython-36m-x86_64-linux-gnu"' \
-o Python/dynload_shlib.o ./Python/dynload_shlib.c
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -I. -I./Include -DPy_BUILD_CORE -o Modules/config.o Modules/config.c
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -I. -I./Include -DPy_BUILD_CORE -DPYTHONPATH='":"' \
-DPREFIX='"/usr/local"' \
-DEXEC_PREFIX='"/usr/local"' \
-DVERSION='"3.6"' \
-DVPATH='""' \
-o Modules/getpath.o ./Modules/getpath.c
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -I. -I./Include -DPy_BUILD_CORE -c ./Modules/socketmodule.c -o Modules/socketmodule.o
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -I. -I./Include -DPy_BUILD_CORE -DUSE_SSL -I/usr/local/ssl/include -I/usr/local/ssl/include/openssl -c ./Modules/_ssl.c -o Modules/_ssl.o
./Modules/_ssl.c:72:25: fatal error: openssl/rsa.h: 没有那个文件或目录
#include "openssl/rsa.h"
^
compilation terminated.
make: *** [Modules/_ssl.o] 错误 1
user@swd:/local/sdb/tools/python3.6$
安装上方提到的openssl和libssl-dev后,继续编译问题解决,编译完成后测试import ssl不再报错。
if test "x" != "x" ; then \
rm -f /usr/local/bin/python3-32; \
(cd /usr/local/bin; ln -s python3.6-32 python3-32) \
fi
rm -f /usr/local/share/man/man1/python3.1
(cd /usr/local/share/man/man1; ln -s python3.6.1 python3.1)
if test "xupgrade" != "xno" ; then \
case upgrade in \
upgrade) ensurepip="--upgrade" ;; \
install|*) ensurepip="" ;; \
esac; \
./python -E -m ensurepip \
$ensurepip --root=/ ; \
fi
The directory '/home/user/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/user/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Looking in links: /tmp/tmpm_490mbe
Requirement already up-to-date: setuptools in /usr/local/lib/python3.6/site-packages (40.6.2)
Requirement already up-to-date: pip in /usr/local/lib/python3.6/site-packages (18.1)
user@swd:/local/sdb/tools/python3.6$
user@swd:/local/sdb/tools/python3.6$
user@swd:/local/sdb/tools/python3.6$
user@swd:/local/sdb/tools/python3.6$
user@swd:/local/sdb/tools/python3.6$
user@swd:/local/sdb/tools/python3.6$
user@swd:/local/sdb/tools/python3.6$ python
Python 3.6.11 (default, Jul 22 2020, 21:31:08)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import ssl
>>>
>>>
>>>
user@swd:/local/sdb/tools/python3.6$
把/usr/bin/python软链接到编译的python可执行文件。
user@swd:/usr/bin$ ll | grep -i python
lrwxrwxrwx 1 root root 26 Jan 11 2019 dh_pypy -> ../share/dh-python/dh_pypy*
-rwxr-xr-x 1 root root 1056 Dec 21 2013 dh_python2*
lrwxrwxrwx 1 root root 29 Jan 11 2019 dh_python3 -> ../share/dh-python/dh_python3*
lrwxrwxrwx 1 root root 23 Jan 11 2019 pdb2.7 -> ../lib/python2.7/pdb.py*
lrwxrwxrwx 1 root root 23 Jan 11 2019 pdb3.4 -> ../lib/python3.4/pdb.py*
lrwxrwxrwx 1 root root 31 Jan 11 2019 py3versions -> ../share/python3/py3versions.py*
lrwxrwxrwx 1 root root 26 Jan 11 2019 pybuild -> ../share/dh-python/pybuild*
lrwxrwxrwx 1 root root 25 Jul 22 21:11 python -> /usr/lib/python3.6/python*
lrwxrwxrwx 1 root root 9 Jan 11 2019 python2 -> python2.7*
-rwxr-xr-x 1 root root 3349512 Mar 23 2014 python2.7*
lrwxrwxrwx 1 root root 9 Jan 11 2019 python3 -> python3.4*
-rwxr-xr-x 1 root root 4061272 Apr 11 2014 python3.4*
-rwxr-xr-x 1 root root 4061272 Apr 11 2014 python3.4m*
lrwxrwxrwx 1 root root 10 Jan 11 2019 python3m -> python3.4m*
lrwxrwxrwx 1 root root 9 Jan 11 2019 python_bak -> python2.7* //原来老的python软链接
lrwxrwxrwx 1 root root 29 Jan 11 2019 pyversions -> ../share/python/pyversions.py*
user@swd:/usr/bin$
搞定。