如何在 ubuntu 21.04 上安装 python3.8/3.7

如何在 ubuntu 21.04 上安装 python3.8/3.7

我在 ubuntu 21.04 上安装 python3.8 一次又一次失败

现在一切正常,但是 pip 出现了这个错误消息(并且 import ctypes 也不起作用):

    Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/setuptools/__init__.py", line 20, in <module>
    from setuptools.dist import Distribution, Feature
  File "/usr/local/lib/python3.8/site-packages/setuptools/dist.py", line 35, in <module>
    from setuptools import windows_support
  File "/usr/local/lib/python3.8/site-packages/setuptools/windows_support.py", line 2, in <module>
    import ctypes
  File "/usr/local/lib/python3.8/ctypes/__init__.py", line 7, in <module>
    from _ctypes import Union, Structure, Array
  File "/usr/local/lib/python3.8/_ctypes/__init__.py", line 8, in <module>
    from _ctypes import Union, Structure, Array
ImportError: cannot import name 'Union' from partially initialized module '_ctypes' (most likely due to a circular import) (/usr/local/lib/python3.8/_ctypes/__init__.py)

答案1

您可以使用项目 Conda。Conda 适用于 Python 环境。幸运的是,您可以选择要使用的 Python 版本。查看 conda.io!进入 Python 环境后,通过 安装任何包conda install PACKAGE。官方文档:https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/packages.html

要创建环境,请使用conda create 要创建 Python 3.8 环境,请使用 python 变量,例如: conda create -n "name" python=3.8 #quotes not required 然后:conda use "name" #quotes not required

答案2

以下是安装方法,安装步骤如下:

==================1==================

首先更新所有内容并安装以下软件包

$ sudo apt update
$ sudo apt upgrade
$ sudo apt dist-upgrade
$ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

下一步是创建一个临时目录并下载 python 源代码

$ mkdir ~/tmp
$ cd ~/tmp
$ wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz

接下来是提取并配置它

$ tar -xvzf Python-3.8.1.tgz
$ cd Python-3.8.1
$ ./configure

现在安装它

sudo make altinstall

确保在安装所有内容时具有 sudo 权限

==================2===================

使用第三方 PPA 仓库安装

$ sudo apt update
$ sudo apt install software-properties-common

$ sudo add-apt-repository ppa:deadsnakes/ppa

$ sudo apt update
$ sudo apt install python3.8

最后检查python的版本,执行此命令。

希望有帮助

相关内容