在Ubuntu 22.04上安装Python3.11及库(numpy、matplotlib、scipy)
我想在我的Ubuntu 22.04桌面上升级python、numpy、matplotlib和scipy的版本,让它们和我笔记本上的版本一致。
运行 apt list --installed
可以看到很多已经安装的包,其中一些是通过 apt-get
安装的,这些是在我设置桌面时安装的:
python3-matplotlib-inline/jammy,jammy,now 0.1.3-1 all [installed]
python3-matplotlib/jammy,now 3.5.1-2build1 amd64 [installed]
python3-numpy/jammy-updates,jammy-security,now 1:1.21.5-1ubuntu22.04.1 amd64 [installed,automatic]
python3-scipy/jammy,now 1.8.0-1exp2ubuntu1 amd64 [installed,automatic]
python3.10-dev/jammy-updates,jammy-security,now 3.10.12-1~22.04.3 amd64 [installed,automatic]
python3.10-minimal/jammy-updates,jammy-security,now 3.10.12-1~22.04.3 amd64 [installed,automatic]
特别是最后两个显示我正在使用python 3.10.12。
为了升级到python 3.11,我运行了
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt install python3.11-full
这让我得到了一个干净的python版本,但现在我在升级我的库时遇到了麻烦,我希望通过pip来管理这些库。特别是当我尝试升级matplotlib时,我得到了
$ pip install numpy --upgrade
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: numpy in ./.local/lib/python3.10/site-packages (1.26.4)
类似的错误,因为这些包已经安装了,pip不想去更改它们。
有没有办法让我在系统上安装新包,并让我的python 3.11指向它们?理想情况下,我希望它是全局可用的,而不是使用虚拟环境(主要是因为我不太理解虚拟环境,我只想能从任何地方调用我的脚本)。
至少我想知道如何升级到matplotlib 3.8.2,并在运行python 3.10内核时导入它,而不是当前的mpl 3.5.1,但理想情况下是升级所有的库。
2 个回答
检查一下你用的是哪个Python,可能是系统自带的那个。然后为安装的Python 3.11创建一个虚拟环境(用命令python -m venv 目录名),激活这个环境后就可以使用这个环境里的pip了。根据我的经验,这是在Ubuntu上使用想要的Python版本最简单的方法。
更好的做法是使用虚拟环境,这样你可以创建符合特定条件和依赖的环境。
你可以创建多个环境,每个环境可以使用不同版本的Python和指定版本的包。
有很多方法可以做到这一点,我特别喜欢Miniforge,这是一个很棒的免费工具,用于管理Python版本和环境。
它支持Linux、Windows和OSX。
安装和使用都很简单。
以下是安装说明:
curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh
举个例子,创建一个名为“myenv”的环境,使用Python 3.11,并安装numpy、matplotlib和scipy:
conda create -n myenv python=3.11 numpy matplotlib scipy
要使用这个环境,可以输入:
conda activate myenv
python main.py
它也支持像Visual Studio Code这样的集成开发环境(IDE),而且Visual Studio Code也可以在Ubuntu上使用。