python的第三方小工具(linux版)
1)vim插件
- 加入一下配置,vim编辑器中按
即可测试代码运行效果,无需退出编辑器
root@jiaxing:~# vim ~/.vimrc
#" Quickly Run
#" ===============================================
nnoremap <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
exec "w"
if &filetype == 'c'
exec "!g++ % -o %<"
exec "!time ./%<"
elseif &filetype == 'cpp'
exec "!g++ % -o %<"
exec "!time ./%<"
elseif &filetype == 'java'
exec "!javac %"
exec "!time java %<"
elseif &filetype == 'sh'
exec "!time bash %"
elseif &filetype == 'python'
exec "!time python3 %"
elseif &filetype == 'html'
exec "!firefox % &"
elseif &filetype == 'go'
exec "!go build %<"
exec "!time go run %"
elseif &filetype == 'mkd'
exec "!~/.vim/markdown.pl % > %.html &"
exec "!firefox %.html &"
endif
endfunc
2)IPython shell
- 对python shell的增强的第三方工具
root@jiaxing:~# apt -y install ipython3
root@jiaxing:~# ipython3
Python 3.10.12 (main, Jan 17 2025, 14:35:34) [GCC 11.4.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.31.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
- 在 IPython shell中与操作系统交互
- 使用% magic函数与操作系统交互
In [4]: %ls /server/
python/ scripts/
In [5]: %cd /server/
/server
In [6]: %pwd
Out[6]: '/server'
In [7]:
- 使用!可以加linux中的任何命令
In [7]: !wc -l /server/python/os/test-venv/sys_mon.py
103 /server/python/os/test-venv/sys_mon.py
In [8]:
创建和管理python虚拟环境
- python虚拟环境旨在不同的项目创建彼此独立运行的运行环境。
- 在虚拟环境下,每一个项目都有自己的依赖包,与其他项目无关。
- 不同的虚拟环境中同一个包可以有不同的版本,并且虚拟环境的数量没有限制
#新版本的python使用venv模块(原名pyvenv,从python3.6开始弃用)来创建和管理虚拟环境
#venv通常会安装获得的python最新版本。如果系统中有多个版本的python,则可以通过执行python3命令(或要使用的其他版本的python命令)来选择一个指定版本的python。
- 1.安装venv
root@jiaxing:/server/scripts# apt install -y python3-venv
- 2.创建虚拟环境
#该目录下会创建一个包含python解释器、标准库,以及各种支持文件的python副本
root@jiaxing:/server/scripts# python3 -m venv /server/python/test-venv
root@jiaxing:/server/scripts#
root@jiaxing:/server/python/os/test-venv# ll
total 24
drwxr-xr-x 5 root root 4096 Jan 21 16:18 ./
drwxr-xr-x 3 root root 4096 Jan 21 15:22 ../
drwxr-xr-x 2 root root 4096 Jan 21 15:22 bin/
drwxr-xr-x 2 root root 4096 Jan 21 15:22 include/
drwxr-xr-x 3 root root 4096 Jan 21 15:22 lib/
lrwxrwxrwx 1 root root 3 Jan 21 15:22 lib64 -> lib/
-rw-r--r-- 1 root root 71 Jan 21 15:22 pyvenv.cfg
- 3.虚拟环境创建完必须激活它
root@jiaxing:/server/python/os/test-venv# source /server/python/os/test-venv/bin/activate
(test-venv) root@jiaxing:/server/python/os/test-venv#
- 4.关闭虚拟环境
(test-venv) root@jiaxing:/server/python/os/test-venv# deactivate
root@jiaxing:/server/python/os/test-venv#
http-server一秒启动一个下载服务器
root@jiaxing:/server/python# cat custom_http_server.py
import http.server
import socketserver
import os
PORT = 8000
class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
# 检查请求的文件是否以 .sh 或 .py 结尾
if self.path.endswith(('.sh', '.py')):
# 添加 Content-Disposition 头,强制浏览器下载
filename = os.path.basename(self.path)
self.send_header("Content-Disposition", f'attachment; filename="{filename}"')
super().end_headers()
with socketserver.TCPServer(("", PORT), CustomHTTPRequestHandler) as httpd:
print(f"Serving at port {PORT}")
httpd.serve_forever()
root@jiaxing:/server/python# python3 custom_http_server.py
python包管理工具pip
1.安装pip
root@jiaxing:/server/scripts# apt install -y python3-pip
root@jiaxing:/server/scripts# pip --version
pip 24.3.1 from /usr/local/lib/python3.10/dist-packages/pip (python 3.10)
更新pip工具
python -m pip install --upgrade pip
-->输出:
Requirement already satisfied: pip in c:\users\101202012006\appdata\local\programs\python\python313\lib\site-packages (24.3.1)
#第一部分(python -m pip)让python运行pip模块;
#第二部分(install --upgrade)让pip更新一个已安装的包;
#最后部分(pip)指定要更新哪个第三方包
#输出表明当前的pip版本
#使用linux默认不会安装pip
sudo apt install python3-dev python3-pip python3-venv
2.将pip源更换为国内镜像仓库
- pip默认使用国外的镜像仓库,下载速度慢,可以替换为国内镜像仓库解决
#临时使用镜像仓库,通过-i选项提供镜像源即可
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider
#更改配置统一指定pip安装源
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
#更改此配置默认从阿里云镜像源下载,如果没有找到合适的包则改从清华镜像源下载
pip config set global.extra-index-url https://pypi.tuna.tsinghua.edu.cn/simple
pytest测试工具
- 使用pip安装第三方包pytest
python -m pip install --user pytest
-->输出:
......
Successfully installed iniconfig-2.0.0 pluggy-1.5.0 pytest-8.3.4
#(--user)表示python只为当前用户安装指定的包,注意:如果命令运行错误可以尝试不加--user运行安装
#输出表明成功安装了pytest最新的版本
使用:
python -m pytest
#pytest会自动检测当前目录下的py文件,会优先检测py文件中以test_开头的函数方法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通