1.启动一个下载服务器(http.server)
- python2:python -m SimpleHTTPServer
- python3:python -m http.server
2.字符串转换为json对象(json.tool)
echo '{"job": "developer", "name": "lmx", "sex": "male"}'|python -m json.tool
3.shell中执行python命令
测试第三方模块是否导入:
python3 -c 'import scrapy'
4.设置pip自动补全
参考:https://www.cnblogs.com/yangmingxianshen/p/11029532.html
安装pip:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py # 运行安装脚本
pip3 completion --bash >> ~/.profile && source ~/.profile
5.pip常用命令
- 查找:pip search flask
- 安装:pip install flask=0.8
- 删除:pip uninstall Werkzeug
- 查看详细信息:pip show flask
- 检查依赖是否完整:pip check flask
- 查看已安装列表:pip list
- 将已安装的包导出至requirements:pip freeze > requirements.txt
- 从requirements中安装:pip install -r requirements.txt
- 将安装包下载至本地:pip3 download requests -d ./pkg
- 其他选项:wheel、hash、help
6.设置pip镜像源
国内镜像:
- 阿里:http://mirrors.aliyun.com/pypi/simple/
- 豆瓣:http://pypi.douban.com/simple/
- 清华(推荐):https://pypi.tuna.tsinghua.edu.cn/simple/
- 使用:pip install flask -i 源
临时使用:
pip3 install flask -i https://pypi.tuna.tsinghua.edu.cn/simple/
永久配置:
mkdir ~/.pip && echo -e "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple/" > ~/.pip/pip.conf
离线安装:将安装包下载至本地,然后选择从本地安装
#单个离线
pip3 download requests -d ./pkg
pip3 install requests --no-index --find-links=./pkg
#多个离线
pip3 freeze > requirements.txt
pip3 download -r requirements.txt -d ./pkg
pip3 install -r requirements.txt --no-index --find-links=./pkg
#--find-links=./pkg 等价于 -f ./pkg
pip3 install requests --no-index -f ./pkg
7.vim配置一键执行、语法检查(Syntastic)、编程提示(jedi-vim)
参考:https://blog.youkuaiyun.com/qq_26877377/article/details/80717755
安装vundle:
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
# 在VIM中安装插件:BundleInstall
VIM配置:~/.vimrc
"""""""""
"一键执行
"""""""""
map <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'
:!time bash %
elseif &filetype == 'python'
exec "!time python %"
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
"""""""""""""""
"添加vundle支持
"""""""""""""""
filetype off
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
if filereadable(expand("~/.vimrc.bundles"))
source ~/.vimrc.bundles
endif
"""""""""
"添加插件
"""""""""
if &compatible
set nocompatible
end
filetype off
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" Let Vundle manage Vundle
Bundle 'gmarik/vundle'
" python自动补全
Bundle 'davidhalter/jedi-vim'
Bundle "klen/python-mode"
" 括号匹配高亮
Bundle 'kien/rainbow_parentheses.vim'
" 可视化缩进
Bundle 'nathanaelkane/vim-indent-guides'
if filereadable(expand("~/.vimrc.bundles.local"))
source ~/.vimrc.bundles.local
endif
filetype on
"""""""""""""
"添加插件配置
"""""""""""""
问题:[Pymode]: error: Pymode requires vim compiled with +python. Most of features will be disabled.
8.python工作环境管理(pyenv、virtualenv)
参考:
https://blog.youkuaiyun.com/Vairsly/article/details/76385206
https://blog.youkuaiyun.com/github_35817521/article/details/53467610
https://blog.youkuaiyun.com/yxiaom/article/details/103468954
安装工具:
curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
配置工具:
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
source ~/.bashrc
pyenv --version
下载镜像:
wget http://mirrors.sohu.com/python/2.7.15/Python-2.7.15.tar.xz -P ~/.pyenv/cache/
安装python:
# 提前安装python依赖
pyenv install 2.7.15
pyenv常用命令:
- pyenv install --list(查看可安装的python版本)
- pyenv versions/version(查看本地已安装的python版本)
- pyenv global 2.7.15(切换python版本)
- pyenv uninstall 2.7.15(卸载python版本)
pyenv virtualenv命令:
- pyenv virtualenv 2.7.15 first_project(创建虚拟环境)
- pyenv virtualenvs(查看所有虚拟环境)
- pyenv activate first_project(进入虚拟环境)
- pyenv deactivate(退出虚拟环境)
- pyenv virtualenv-delete first_project(删除虚拟环境)
9.发送邮件
import yagmail
username = '2292752593@qq.com' # 邮箱账号
passwd = '******' # 授权码,不是邮箱密码
host = 'smtp.qq.com'
mail = yagmail.SMTP(user=username,
password=passwd,
host=host,
# smtp_ssl=True
) # 如果用的是qq邮箱或者你们公司的邮箱使用是安全协议的话,必须写上 smtp_ssl=True
mail.send(
to=['2233148634@qq.com', '3461403779@qq.com'], # 多个人列表,一个人字符串形式
cc='522435434@qq.com', # 抄送
subject='学习发送邮件', # 邮件标题
contents='你好,你今天开心吗?', # 邮件正文
attachments=[r'C:\Users\only\python\Arithmetic\bubble_sort.py',
r'C:\Users\only\python\Arithmetic\insertion_sort.py']
)
print('发送成功')