从https://www.python.org/下载源码。
我这里下载最新的 Python3.7.4。
命令如下:
$ tar -xzvf Python-3.7.4.tgz
$ cd Python-3.7.4/
$ mkdir build
$cd build
$../configure --prefix=/usr
$make -j8
$sudo make install
Failed to build these modules:
_ctypes
解决:
sudo apt-get install libffi-dev
Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
解决:
下载libressl最新稳定版本 2.7.4
https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.7.4.tar.gz
解压:
tar -xzvf libressl-2.7.4.tar.gz
编译安装:
$ cd libressl-2.7.4
$ ./configure
$ make -j8
$ sudo make install
新建或修改 /etc/ld.so.conf.d/local.conf 配置文件,添加如下内容:
/usr/local/lib
即将 /usr/local/lib 目录加入到模块加载目录。
重新加载共享模块:
$ sudo ldconfig -v
$ openssl version
LibreSSL 2.7.4
回到 Python-3.7.4 目录,编辑安装文件 Modules/Setup.dist
去掉有关 ssl 编译代码的注释,共 4 行,并修改 SSL 目录为 SSL=/usr/local, 如下所示:
# 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.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
重新配置、编译。
Exception:
Traceback (most recent call last):
File "/tmp/tmpy1hw1kin/pip-19.0.3-py2.py3-none-any.whl/pip/_internal/cli/base_command.py", line 179, in main
status = self.run(options, args)
File "/tmp/tmpy1hw1kin/pip-19.0.3-py2.py3-none-any.whl/pip/_internal/commands/install.py", line 255, in run
with self._build_session(options) as session:
File "/tmp/tmpy1hw1kin/pip-19.0.3-py2.py3-none-any.whl/pip/_internal/cli/base_command.py", line 93, in _build_session
insecure_hosts=options.trusted_hosts,
File "/tmp/tmpy1hw1kin/pip-19.0.3-py2.py3-none-any.whl/pip/_internal/download.py", line 344, in __init__
self.headers["User-Agent"] = user_agent()
File "/tmp/tmpy1hw1kin/pip-19.0.3-py2.py3-none-any.whl/pip/_internal/download.py", line 108, in user_agent
zip(["name", "version", "id"], distro.linux_distribution()),
File "/tmp/tmpy1hw1kin/pip-19.0.3-py2.py3-none-any.whl/pip/_vendor/distro.py", line 120, in linux_distribution
return _distro.linux_distribution(full_distribution_name)
File "/tmp/tmpy1hw1kin/pip-19.0.3-py2.py3-none-any.whl/pip/_vendor/distro.py", line 675, in linux_distribution
self.version(),
File "/tmp/tmpy1hw1kin/pip-19.0.3-py2.py3-none-any.whl/pip/_vendor/distro.py", line 735, in version
self.lsb_release_attr('release'),
File "/tmp/tmpy1hw1kin/pip-19.0.3-py2.py3-none-any.whl/pip/_vendor/distro.py", line 892, in lsb_release_attr
return self._lsb_release_info.get(attribute, '')
File "/tmp/tmpy1hw1kin/pip-19.0.3-py2.py3-none-any.whl/pip/_vendor/distro.py", line 550, in __get__
ret = obj.__dict__[self._fname] = self._f(obj)
File "/tmp/tmpy1hw1kin/pip-19.0.3-py2.py3-none-any.whl/pip/_vendor/distro.py", line 998, in _lsb_release_info
stdout = subprocess.check_output(cmd, stderr=devnull)
File "/media/xxt/install/Python-3.7.4/Lib/subprocess.py", line 395, in check_output
**kwargs).stdout
File "/media/xxt/install/Python-3.7.4/Lib/subprocess.py", line 487, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.
解决办法:
找到lsb_release.py文件和CommandNotFound目录,把它们拷贝到报的错误中subprocess.py所在文件夹
$find /usr/ -name lsb_release.py
/usr/lib/python3.7/lsb_release.py
/usr/lib/python2.7/dist-packages/lsb_release.py
/usr/lib/python3/dist-packages/lsb_release.py
/usr/share/pyshared/lsb_release.py
$ cp /usr/share/pyshared/lsb_release.py /media/xxt/install/Python-3.7.4/Lib
Python3 查看:
python3
Python 3.7.4 (default, Aug 1 2019, 10:26:11)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
安装成功,结束。
基于用户修改 Python 版本:
想要为某个特定用户修改 Python 版本,只需要在其 home 目录下创建一个 alias(别名) 即可。打开该用户的 ~/.bashrc 文件,添加新的别名信息来修改默认使用的 Python 版本。
alias python='/usr/bin/python3.7'
一旦完成以上操作,重新登录或者重新加载 .bashrc 文件,使操作生效。
检查当前的 Python 版本。
$ python --version
Python 3.7.4