【Ubuntu】安装常用软件包-java/python

安装java

直接输入java,如果没有安装的话会提醒你输入命令安装,类似

Command 'java' not found, but can be installed with:
sudo apt install jdkxxxxxxxxxxxxxx

然后选一个版本安装就好,我这里选的jdk17,安装完确认一下

ubuntu@VM-4-13-ubuntu:~$ java --version
openjdk 17.0.12 2024-07-16
OpenJDK Runtime Environment (build 17.0.12+7-Ubuntu-1ubuntu224.04)
OpenJDK 64-Bit Server VM (build 17.0.12+7-Ubuntu-1ubuntu224.04, mixed mode, sharing)

安装python3

输入python看他的意思是有python3,输入python3后果然进入shell了,那就ok了。

ubuntu@VM-4-13-ubuntu:~$ python
Command 'python' not found, did you mean:
  command 'python3' from deb python3
  command 'python' from deb python-is-python3
ubuntu@VM-4-13-ubuntu:~$ python3
Python 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

下面那句是你想要输入python时候就运行python3,可以安装 python-is-python3包,安装后输入python就也可以了,不过保持着能运行就不动原则,我没有操作这个,这步可选
sudo apt install python-is-python3

安装pip

发现pip和pip3也没有,自动提示了安装的命令,还挺方便的

ubuntu@VM-4-13-ubuntu:~$ pip
Command 'pip' not found, but can be installed with:
sudo apt install python3-pip
ubuntu@VM-4-13-ubuntu:~$ pip3
Command 'pip3' not found, but can be installed with:
sudo apt install python3-pip
ubuntu@VM-4-13-ubuntu:~$ 

运行提示的 sudo apt install python3-pip 就可以了,发现pip和pip3都存在,并不是指向一个命令,输入pip list/pip3 list 都没什么问题

ubuntu@VM-4-13-ubuntu:~$ which pip
/usr/bin/pip
ubuntu@VM-4-13-ubuntu:~$ which pip3
/usr/bin/pip3
ubuntu@VM-4-13-ubuntu:~$ ls -lsa /usr/bin/pip
4 -rwxr-xr-x 1 root root 221 Mar  6  2024 /usr/bin/pip
ubuntu@VM-4-13-ubuntu:~$ ls -lsa /usr/bin/pip3
4 -rwxr-xr-x 1 root root 221 Mar  6  2024 /usr/bin/pip3

然后问题就来了,安装时候报错
pip install xxxx

ubuntu@VM-4-13-ubuntu:~$ pip install xxxx
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.
    
    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.
    
    See /usr/share/doc/python3.12/README.venv for more information.

解释一下

这个错误信息是 Python 在 Ubuntu 系统上对于系统级包管理的保护机制。从 Python 3.7 开始,Python 包管理器 pip 被设计为默认不安装包到系统级路径,以避免潜在的系统冲突和安全问题。这个机制被称为 “externally-managed-environment”。

错误信息提供了几种解决方案:

  • 使用 apt 命令安装系统级 Python 包:
    sudo apt install python3-xyz

  • 创建虚拟环境:
    如果你想要安装的包不是 Debian 官方仓库中的,你可以创建一个虚拟环境。虚拟环境允许你在不影响系统其他部分的情况下安装和管理 Python 包。使用以下命令创建虚拟环境:

    python3 -m venv path/to/venv
    然后激活虚拟环境:

    source path/to/venv/bin/activate
    在虚拟环境中,你可以使用 pip 来安装包,这些包将只在这个虚拟环境中可用。

  • 使用 pipx 管理非 Debian 打包的 Python 应用:
    pipx 是一个工具,用于在隔离的环境中安装和运行 Python 应用程序。它会自动创建虚拟环境。使用以下命令安装 pipx:
    sudo apt install pipx
    pipx install xyz

  • 覆盖系统包:
    如果你确定要覆盖系统包,可以通过添加 --break-system-packages 选项来使用 pip 安装。但这样做可能会破坏你的 Python 安装或操作系统,因此不推荐这样做,除非你非常清楚你在做什么。

看下来还是最后一点最简单啊,哈哈,先按建议的来试试
pip list看了一眼没有pandas

ubuntu@VM-4-13-ubuntu:~$ python3
Python 3.12.3 (main, Jul 31 2024, 17:43:48) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
>>> 
ubuntu@VM-4-13-ubuntu:~$ sudo apt install python3-pandas 
Reading package lists... Done
Building dependency tree... Done
.....
省略安装日志
.....
ubuntu@VM-4-13-ubuntu:~$ 
ubuntu@VM-4-13-ubuntu:~$ pip3 list |grep "pandas"
pandas                2.1.4+dfsg
ubuntu@VM-4-13-ubuntu:~$ 

ok搞定了,再试下最后一种方法,先卸载掉,重新安装一下
pip uninstall pandas
报上面的错,直接加上 –break-system-packages
pip uninstall pandas --break-system-packages
报错,权限不够,加sudo
sudo pip uninstall pandas --break-system-packages
卸载成功,emmm,好像不用重新安装了,–break-system-packages 也挺好用的,另外几种就不测试了,不喜欢维护每个环境

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值