Skip to content

心得体会:

1. Bash Experience

尝试python3安装库 -> 失败

bash
# 升级 pip 到最新版本

python3 -m pip install --upgrade pip

# 安装 pandas 和其他常用库

pip3 install pandas matplotlib numpy

出现:

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.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

使用apt则可以安装(这里是系统级安装)

bash
sudo apt install python3-pandas python3-matplotlib python3-numpy

尝试用venv建立虚拟环境 -> 失败:

bash
python3 -m venv venv
Error: [Errno 95] Operation not supported: 'lib' -> '/home/idler/CodeSpace/ADS-projects/P2/venv/lib64'
# 该错误说明文件管理系统不支持(如果是原生Linux特性的话可以正常使用venv)

使用df -T 命令查看文件管理系统

bash
df -T /home/idler/CodeSpace/ADS-projects/P2
Filesystem     Type             1K-blocks      Used Available Use% Mounted on
vmhgfs-fuse    fuse.vmhgfs-fuse 504967164 139870244 365096920  28% /home/idler/CodeSpace

可以看到这里是vmware host-guest file system

所以这里使用文件管理系统兼容性更好的virtualenv替代venv

bash
# 安装 python3-virtualenv
sudo apt update
sudo apt install python3-virtualenv

# 创建虚拟环境
virtualenv --always-copy venv

然后就可以正常激活venv并安装虚拟环境里的依赖了

bash
# 激活并安装依赖
source venv/bin/activate
pip install seaborn

但是出了一个问题:

bash
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x71f1afc81ee0>: Failed to establish a new connection: [Errno 111] Connection refused'))': /simple/seaborn/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x71f1b13d6780>: Failed to establish a new connection: [Errno 111] Connection refused'))': /simple/seaborn/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x71f1afb29340>: Failed to establish a new connection: [Errno 111] Connection refused'))': /simple/seaborn/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x71f1afb32b40>: Failed to establish a new connection: [Errno 111] Connection refused'))': /simple/seaborn/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x71f1afb32e10>: Failed to establish a new connection: [Errno 111] Connection refused'))': /simple/seaborn/
ERROR: Could not find a version that satisfies the requirement seaborn (from versions: none)
ERROR: No matching distribution found for seaborn

可以看到这里是ProxyError,这是代理配置问题。pip 正在尝试通过代理连接,但代理不可用。可能是没关vpn就关机导致的,把vpn打开就行了。然后代理正常工作。

bash
python3 src/plot_results.py

然后代码运行顺利,python画图顺利。

再可以创建requirements.txt文件来管理依赖

bash
# 安装完包后,导出依赖列表
pip freeze > requirements.txt

# 在新环境或其他机器上,一键安装所有依赖
pip install -r requirements.txt