帅瞎了!手机也能写Python代码!手把手教你配置环境!
大数据DT
共 20002字,需浏览 41分钟
·
2021-03-14 22:38
导读:本文旨在讲解如何在Android平板或手机上搭建Python开发环境,帮助Python初学者有效利用碎片化时间进行学习,从而达到良好的学习效果。
01 Termux安装与配置
1. 系统要求
2. Termux安装
Termux 是一款支持 Linux 环境的 Android 终端模拟器APP。你无需对手机进行root或额外设置即可使用。这款APP会在手机中自动安装一个最小化基本系统,并支持使用 APT 包管理工具安装软件。
3. Termux基本操作
Ctrl + A -> 将光标移动到行首
Ctrl + E -> 将光标移动到行尾
Ctrl + C -> 中止当前进程
Ctrl + D -> 注销终端会话
Ctrl + K -> 从光标删除到行尾
Ctrl + U -> 从光标删除到行首
Ctrl + L -> 清除终端
Ctrl + Z -> 挂起(发送SIGTSTP到)当前进程
音量加 + E -> Esc键
音量加 + T -> Tab键
音量加 + 1 -> F1(音量增加 + 2 → F2…以此类推)
音量加 + 0 -> F10
音量加 + B -> Alt + B,使用readline时返回一个单词
音量加 + F -> Alt + F,使用readline时转发一个单词
音量加 + X -> Alt+X
音量加 + W -> 向上箭头键
音量加 + A -> 向左箭头键
音量加 + S -> 向下箭头键
音量加 + D -> 向右箭头键
音量加 + L -> | (管道字符)
音量加 + H -> 〜(波浪号字符)
音量加 + U -> _ (下划线字符)
音量加 + P -> 上一页
音量加 + N -> 下一页
音量加 + . -> Ctrl + \(SIGQUIT)
音量加 + V -> 显示音量控制
音量加 + Q -> 切换显示的功能键视
音量加 + K -> 切换显示的功能键视图
4. 修改软件源
sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list
sed -i 's@^\(deb.*games stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/game-packages-24 games stable@' $PREFIX/etc/apt/sources.list.d/game.list
sed -i 's@^\(deb.*science stable\)$@#\1\ndeb https://mirrors.tuna.tsinghua.edu.cn/termux/science-packages-24 science stable@' $PREFIX/etc/apt/sources.list.d/science.list
apt update && apt upgrade
5. 安装基本组件
pkg install -y zsh curl wget build-essential
pkg install -y git python nodejs-lts perl ipython
pkg install -y neovim lazygit ranger fzf
02 终端配置
1. Shell配置
安装oh-my-zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
plugins=(
git
python
colorize
zsh-interactive-cd
zsh-navigation-tools
zsh-autosuggestions
)
2. 主题美化
1)配色与字体
2)主题配置
安装
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
配置
3. 启用vi模式(可选)
# Better searching in command mode
bindkey -M vicmd '?' history-incremental-search-backward
bindkey -M vicmd '/' history-incremental-search-forward
# Beginning search with arrow keys
bindkey "\033[1~" beginning-of-line
bindkey "\033[4~" end-of-line
bindkey '^[[3~' delete-char
bindkey "^[OA" up-line-or-beginning-search
bindkey "^[OB" down-line-or-beginning-search
bindkey -M vicmd "k" up-line-or-beginning-search
bindkey -M vicmd "j" down-line-or-beginning-search
03 Python包安装与配置
1. 安装环境配置
# 配置pypi源
pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
# 依赖项
pip install wheel
pip install setuptools --upgrade
2. Python包安装
安装numpy和scipy
# 添加第三方仓库
curl -LO https://its-pointless.github.io/setup-pointless-repo.sh
bash setup-pointless-repo.sh
# 从仓库安装numpy scipy
pkg install -y numpy scipy
安装lxml
# 安装lxml的依赖项
pkg install -y libxml2 libxslt
# 安装lxml
pip install lxml
安装pillow
# 安装pillow的依赖项
pkg install -y libjpeg-turbo libtiff
pkg install -y littlecms tk libwebp libsodium
# 安装pillow
pip install pillow
安装matplotlib
# 安装matplotlib的依赖项
pkg install -y freetype libpng
pip install kiwisolver cycler
pip install pyparsing python-dateutil
# 安装matplotlib
pip install matplotlib
安装pandas
# 安装pandas的依赖项
pip install -y pytz cython
# 安装pandas
export CFLAGS="-Wno-deprecated-declarations -Wno-unreachable-code"
pip install pandas
安装jupyter
# 安装jupyter依赖项
pkg install -y libzmq libcrypt pkg-config
# 安装jupyter
pip install jupyter
04 IPython和NeoVim配置
1. IPython配置
安装IPython
# 安装yapf
pip install yapf
# 安装Pygments
pip install pygments
# 安装ipython
pip install ipython
创建配置文件
修改配置文件
# 配置终端颜色
c.InteractiveShell.colors = 'Linux'
c.TerminalInteractiveShell.autoformatter = 'yapf'
# 配置高亮方案,可通过pygmentize -L styles浏览所有可选配置
c.TerminalInteractiveShell.highlight_style = 'monokai'
# 配置魔术命令%editor使用的编辑器
c.TerminalInteractiveShell.editor = 'nvim'
2. NeoVim配置
pip install pynvim
创建init.vim文件进行基本配置
" 一般设置
set nocompatible "关闭与vi的兼容模式
set number "显示行号
set nowrap "不自动折行
set showmatch "显示匹配的括号
set scrolloff=3 "距离顶部和底部3行"
set encoding=utf-8 "编码
set fenc=utf-8 "编码
set fileencodings=utf-8
set hlsearch "搜索高亮
syntax on "语法高亮
set tabstop=4 "tab宽度
set shiftwidth=4
set smarttab
set backspace=indent,eol,start
set expandtab "tab替换为空格键
set fileformat=unix "保存文件格式
set splitbelow
set cmdheight=2
set completeopt=longest,menu
set splitright
set foldmethod=indent
set foldlevel=99
" 设置空格为leader键
let mapleader=" "
使用vim-plug安装NeoVim插件
curl -fLo ~/.config/nvim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
" vim-plug插件管理
call plug#begin('~/.config/nvim/plugged')
Plug 'junegunn/vim-plug'
call plug#end()
常用插件及设置
call plug#begin('~/.config/nvim/plugged')
Plug 'junegunn/vim-plug'
" git支持
Plug 'tpope/vim-fugitive'
" Python自动缩进插件
Plug 'vim-scripts/indentpython.vim'
" 项目管理工具
Plug 'mhinz/vim-startify'
" 快速对齐插件
Plug 'junegunn/vim-easy-align'
" 当前光标下的单词增加下划线
Plug 'itchyny/vim-cursorword'
" 快速选择插件
Plug 'tpope/vim-surround'
" 自定义代码片断
Plug 'honza/vim-snippets'
" 语法高亮支持
Plug 'sheerun/vim-polyglot'
" 主题、状态栏设置
Plug 'haishanh/night-owl.vim'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'ryanoasis/vim-devicons'
" coc扩展
Plug 'neoclide/coc.nvim', {'branch': 'release'}
" fzf模糊查找
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
" whichkey快捷菜单
Plug 'liuchengxu/vim-which-key
" 浮动窗口支持
Plug 'voldikss/vim-floaterm'
" ranger文件管理器支持
Plug 'kevinhwang91/rnvimr'
call plug#end()
" 启用标签栏
let g:airline#extensions#tabline#enabled = 1
" 支持图标字体
let g:airline_powerline_fonts = 1
" 设置状态栏主题
let g:airline_theme='night_owl'
" 设置主题
set termguicolors
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
syntax enable
colorscheme night-owl
安装、配置coc.nvim扩展
" coc扩展
let g:coc_global_extensions = [
\'coc-json',
\'coc-pyright',
\'coc-snippets',
\'coc-xml',
\'coc-explorer',
\'coc-prettier',
\'coc-highlight']
" 使用tab键进行补全选择
inoremap <silent><expr> <TAB>
\ pumvisible() ? "\<C-n>" :
\ <SID>check_back_space() ? "\<TAB>" :
\ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
" 使用回车进行补全选择
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
" Highlight the symbol and its references when holding the cursor.
autocmd CursorHold * silent call CocActionAsync('highlight')
" Add `:Format` command to format current buffer.
command! -nargs=0 Format :call CocAction('format')
" Add `:Fold` command to fold current buffer.
command! -nargs=? Fold :call CocAction('fold', <f-args>)
" Add `:OR` command for organize imports of the current buffer.
command! -nargs=0 OR :call CocAction('runCommand', 'editor.action.organizeImport')
" 添加状态栏显示支持
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}
" 启用Prettier进行文件自动格式化
command! -nargs=0 Prettier :CocCommand prettier.formatFile
let g:prettier#autoformat = 1
" 设置Coc Search
nnoremap <leader>? :CocSearch <C-R>=expand("<cword>")<CR><CR>
{
"python.defaultInterpreterPath": "/data/data/com.termux/files/usr/bin/python",
"python.pythonPath": "/data/data/com.termux/files/usr/bin/python",
"python.linting.pylintEnable":true,
"python.formatting.provider": "yapf",
"python.formatting.yapfArgs": [
"--style",
"{SPACES_AROUND_POWER_OPERATOR: True, SPACES_BEFORE_COMMENT: 1}"
],
"explorer.width": 38,
"explorer.quitOnOpen": true,
"explorer.sources": [
{
"name": "buffer",
"expand": false
},
{
"name": "file",
"expand": true
}
],
"explorer.file.column.indent.indentLine": true,
"explorer.file.showHiddenFiles": true,
"explorer.icon.enableNerdfont": true,
"explorer.keyMappingMode": "none",
"explorer.buffer.showHiddenBuffers": false,
"explorer.keyMappings.global": {
"o": ["wait", "expanded?", "collapse", "expand"],
"<cr>": ["wait", "expandable?", "cd", "open"],
"?": "help",
"q": "quit"
},
"coc.preferences.formatOnSaveFiletypes": ["*"],
"prettier.printWidth": 100,
"prettier.eslintIntegration": true,
"prettier.disableLanguages": [],
"prettier.formatterPriority": 1,
"prettier.useTabs": true,
"prettier.trailingComma": "all",
"prettier.singleQuote": false,
"prettier.tabWidth": 4
}
配置vim-which-key
" 将空格设置为whichkeyleader键
nnoremap <silent> <leader> :WhichKey '<Space>'<CR>
" whichkey基本配置
let g:which_key_timeout = 100
let g:which_key_display_names = {'<CR>': '↵', '<TAB>': '⇆'}
let g:which_key_map = {}
let g:which_key_sep = '→'
let g:which_key_use_floating_win = 0
let g:which_key_max_size = 0
" 呼出whichkey时隐藏状态栏
autocmd! FileType which_key
autocmd FileType which_key set laststatus=0 noshowmode noruler
\| autocmd BufLeave <buffer> set laststatus=2 noshowmode ruler
" 自定义whichkey
let g:which_key_map['?'] = 'search word'
let g:which_key_map['/'] = [ ':call Comment()' , 'comment' ]
let g:which_key_map['.'] = [ ':e $MYVIMRC' , 'open init' ]
let g:which_key_map[';'] = [ ':Commands' , 'commands' ]
let g:which_key_map['e'] = [ ':CocCommand explorer --toggle --sources=file+' , 'explorer' ]
let g:which_key_map['n'] = [ ':let @/ = ""' , 'no highlight' ]
let g:which_key_map['q'] = [ '<Plug>(coc-fix-current)' , 'quickfix' ]
let g:which_key_map['u'] = [ ':UndotreeToggle' , 'undo tree']
let g:which_key_map['t'] = [':FloatermNew --wintype=normal --height=6' , 'terminal']
let g:which_key_map['r'] = [ ':FloatermNew ranger' , 'ranger']
let g:which_key_map['f'] = [':FloatermNew fzf' , 'fzf']
let g:which_key_map['g'] = [':FloatermNew lazygit' , 'git']
let g:which_key_map['p'] = [':FloatermNew python' , 'python']
call which_key#register('<Space>', "g:which_key_map")
在NeoVim中运行Python脚本
"在普通模式下,按r来运行Python脚本
noremap r :call RunPython()<CR>
func! RunPython()
exec "w"
if &filetype == 'python'
exec "!time python %"
endif
endfunc
05 结语
评论