Linux服务器环境部署与Python配置(实习笔记)
Contents
参考资料
服务器环境部署与操作
服务器连接与文件传输
-
SSH 登录
1 2ssh test@114.xx.xxx.xxx # 密码:`xxx@2022` -
文件传输
1 2 3 4 5# 上传本地文件到服务器 scp /local/path/file.txt test@114.xx.xxx.xxx:/home/test/ # 下载服务器文件到本地 scp test@114.xx.xxx.xxx:/home/test/file.txt /local/path/
安装 Python 3.9(源码编译)
-
更新软件包
1sudo apt update -
下载 Python 3.9.4
1wget https://www.python.org/ftp/python/3.9.4/Python-3.9.4.tar.xz -
解压并进入目录
1 2tar xf Python-3.9.4.tar.xz cd Python-3.9.4 -
编译与安装
1 2 3sudo ./configure --enable-optimizations --with-lto --enable-shared sudo make sudo make altinstall # 使用 altinstall 避免覆盖系统 python3 -
下创建符号链接
1 2 3 4 5 6 7# 查找 libpython 位置 whereis libpython3.9.so.1.0 # 在 /usr/lib/ 下创建符号链接(源路径使用绝对路径) sudo ln -s /home/test/Python-3.9.4/libpython3.9.so.1.0 /usr/lib/ ln -s /usr/local/bin/python3.9 /usr/bin/python3.9 ln -s /usr/local/bin/python3.9 /usr/local/bin/python3.9
Python 环境配置
-
检查版本与包
1 2python --version pip3.9 list -
更新 pip
1pip3.9 install --upgrade pip -
安装常用包(使用阿里云镜像加速)
1pip3.9 install pandas numpy matplotlib seaborn plotly toml dash jupyter-dash jupyter-server-proxy -i https://mirrors.aliyun.com/pypi/simple/
常用语句
-
软链接管理
1 2 3 4 5# 查看软链接 ls -l /usr/bin/python* # 删除软链接(注意不要加斜杠) rm /usr/bin/python3.9 -
进程管理
1 2 3 4 5 6 7 8 9 10 11# 查看端口占用 sudo netstat -tlnp | grep :端口号 # 停止指定进程 kill -9 [PID号] # 后台运行程序 nohup python3.9 stable3_1.py -i "2022年售后维修在线编辑汇总(1).xlsx" > output.log 2>&1 & # 查看后台输出 tail -f output.log -
文件格式转换(PyQt5)
1 2# 将 .ui 文件转换为 .py 文件 pyuic5 -o output.py input.ui -
运行 Python 脚本
1python stable3_1.py -i "2022年售后维修在线编辑汇总(1).xlsx"
注意事项
- 使用
altinstall而非install安装 Python,避免影响系统自带 Python - 生产环境中建议使用虚拟环境(venv 或 conda)隔离项目依赖
- 长期运行的服务建议使用 systemd 或 supervisor 进行进程守护