如何在你的Android手機上配置 Python 環境?

手機ttf格式用什麼開啟

推薦學習

金三即過,這300道python高頻面試都沒刷,銀四怎麼闖?

如何在你的Android手機上配置 Python 環境?

本文旨在講解如何在Android平板或手機上搭建Python開發環境,幫助Python初學者有效利用碎片化時間進行學習,從而達到良好的學習效果。

對於大部分初學Python的人來說,由於工作、生活等因素常常無法進行連貫、系統的學習。初學者為了利用上下班通勤等碎片化時間進行學習,通常會在平板上觀看學習影片並做記錄。雖然這種方法很好,但效果還是不如邊學邊練來得好。

因此,你需要的是這個:

如何在你的Android手機上配置 Python 環境?

不多說,讓我們開始吧!

一、Termux安裝與配置

1。 系統要求

Android 7。0以上的平板或手機。這裡建議使用兼顧了便攜性和螢幕尺寸的8寸平板。

2G以上可用空間。由於需要安裝各種軟體包,對儲存空間的消耗還是比較大的。

2。 Termux安裝

在安裝Termux前,需要了解一下什麼是Termux。

Termux 是一款支援 Linux 環境的 Android 終端模擬器APP。你無需對手機進行root或額外設定即可使用。這款APP會在手機中自動安裝一個最小化基本系統,並支援使用 APT 包管理工具安裝軟體。

目前Termux官網(

https://termux。com/

)建議使用F-Droid進行安裝,不再支援透過Google Play進行安裝。

主程式

Termux

下載地址:

https://f-droid。org/repo/com。termux_106。apk

美化工具

Termux:Styling

下載地址:

https://f-droid。org/repo/com。termux。styling_29。apk

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。 修改軟體源

安裝完Termux後,使用如下命令自動替換官方源為清華映象源:

sed -i ‘s@^\(deb。*stable main\)$@#\1\ndeb https://mirrors。tuna。tsinghua。edu。cn/termux/termux-packages-24 stable main@’ $PREFIX/etc/apt/sources。listsed -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。listsed -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。listapt update && apt upgrade

5。 安裝基本元件

在配置Python開發環境前,我們還需要使用如下命令安裝一些基本軟體包:

pkg install -y zsh curl wget build-essentialpkg install -y git python nodejs-lts perl ipythonpkg install -y neovim lazygit ranger fzf

二、終端配置

1。 Shell配置

安裝

oh-my-zsh

Shell的型別有很多種,Termux下預設使用的是

bash

,這裡使用功能更強大的

zsh

來代替

bash

。為了讓初學者更好地使用

zsh

,需要先安裝高檔大氣上檔次,狂拽炫酷吊炸天的

oh-my-zsh

sh -c “$(curl -fsSL https://raw。github。com/ohmyzsh/ohmyzsh/master/tools/install。sh)”

安裝過程中,會提示是否切換shell為

zsh

,輸入

回車

Y

即可。

此外,還需要安裝

zsh-autosuggestions

外掛,用於自動補全:

git clone https://github。com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-$HOME/。oh-my-zsh/custom}/plugins/zsh-autosuggestions

oh-my-zsh

需要使用

nano

neovim

等終端編輯器修改

。zshrc

檔案來進行配置,在

plugins

部分新增外掛支援:

plugins=( git python colorize zsh-interactive-cd zsh-navigation-tools zsh-autosuggestions)

2。 主題美化

2。1 配色與字型

長按螢幕,點選

More

,選擇

Style

可以選擇配色和字型,推薦使用

Neon

配色和

JetBrains Mono

字型,也可以透過修改

~/。termux/

目錄下的

colors。properties

font。ttf

檔案進行自定義。

2。2 主題配置

接下來安裝

powerlevel10k

的主題:

安裝

輸入如下命令下載

powerlevel10k

git clone ——depth=1 https://gitee。com/romkatv/powerlevel10k。git ${ZSH_CUSTOM:-$HOME/。oh-my-zsh/custom}/themes/powerlevel10k

修改

。zshrc

檔案,將

ZSH_THEME=“robbyrussell”

改為

ZSH_THEME=“powerlevel10k/powerlevel10k”

配置

重新開啟Termux,輸入

p10k configure

進入

powerlevel10k

的配置介面。第一次會自動下載預設字型,安裝後會自動退出,重新開啟Termux即可。

可以根據配置嚮導的提示,定製適合自己風格的終端介面。

如何在你的Android手機上配置 Python 環境?

3。 啟用vi模式(可選)

Termux命令列中修改已經輸入的命令比較麻煩。透過開啟

vi

模式,使用者可以像在

vi

編輯器裡一樣進行操作。對於

vi

使用者來說,進入這種模式後,編輯和修改命令就顯得十分得心應手了。

使用

nano

neovim

等終端編輯器修改

。zshrc

檔案來進行配置,在

plugins

部分新增

vi-mode

項,開啟

vi

模式。在命令列狀態下,按

Esc

鍵,即可進入

vi

模式的

普通模式

如何在你的Android手機上配置 Python 環境?

不過在預設的vi模式存在按鍵bug,需要在

。zshrc

檔案最後新增如下配置:

# Better searching in command modebindkey -M vicmd ‘?’ history-incremental-search-backwardbindkey -M vicmd ‘/’ history-incremental-search-forward# Beginning search with arrow keysbindkey “\033[1~” beginning-of-linebindkey “\033[4~” end-of-linebindkey ‘^[[3~’ delete-charbindkey “^[OA” up-line-or-beginning-searchbindkey “^[OB” down-line-or-beginning-searchbindkey -M vicmd “k” up-line-or-beginning-searchbindkey -M vicmd “j” down-line-or-beginning-search

三、Python包安裝與配置

1。 安裝環境配置

# 配置pypi源pip install pip -Upip config set global。index-url https://pypi。tuna。tsinghua。edu。cn/simple# 依賴項pip install wheelpip install setuptools ——upgrade

2 Python包安裝

安裝

numpy

scipy

# 新增第三方倉庫curl -LO https://its-pointless。github。io/setup-pointless-repo。shbash setup-pointless-repo。sh# 從倉庫安裝numpy scipypkg install -y numpy scipy

安裝

lxml

# 安裝lxml的依賴項pkg install -y libxml2 libxslt# 安裝lxmlpip install lxml

安裝

pillow

# 安裝pillow的依賴項pkg install -y libjpeg-turbo libtiff pkg install -y littlecms tk libwebp libsodium# 安裝pillowpip install pillow

安裝

matplotlib

# 安裝matplotlib的依賴項pkg install -y freetype libpngpip install kiwisolver cycler pip install pyparsing python-dateutil# 安裝matplotlibpip install matplotlib

安裝

pandas

# 安裝pandas的依賴項pip install -y pytz cython# 安裝pandasexport CFLAGS=“-Wno-deprecated-declarations -Wno-unreachable-code”pip install pandas

安裝

jupyter

# 安裝jupyter依賴項pkg install -y libzmq libcrypt pkg-config# 安裝jupyter pip install jupyter

待安裝完成,輸入

jupyter notebook

啟動,將地址複製到瀏覽器中即可開啟Jupyter。

如何在你的Android手機上配置 Python 環境?

四、IPython和NeoVim配置

對於希望在終端下進行使用的同學,推薦IPython+NeoVim組合。

1。 IPython配置

安裝

IPython

# 安裝yapfpip install yapf# 安裝Pygmentspip install pygments# 安裝ipythonpip install ipython

建立配置檔案

使用

ipython profile create

命令在

~/。ipython/profile_default/

目錄下的建立

ipython_config。py

配置檔案。

修改配置檔案

使用neovim、nano等終端編輯器修改

~/。ipython/profile_default/

目錄下的

ipython_config。py

檔案,新增如下配置:

# 配置終端顏色c。InteractiveShell。colors = ‘Linux’c。TerminalInteractiveShell。autoformatter = ‘yapf’ # 配置高亮方案,可透過pygmentize -L styles瀏覽所有可選配置c。TerminalInteractiveShell。highlight_style = ‘monokai’ # 配置魔術命令%editor使用的編輯器c。TerminalInteractiveShell。editor = ‘nvim’

2。 NeoVim配置

在配置NeoVim前,需要安裝pynvim外掛,以擴充套件NeoVim對Python的支援。

pip install pynvim

建立init。vim檔案進行基本配置

在命令列下,透過

nvim

命令進入NeoVim編輯器,輸入

:e $MYVIMRC

編輯NeoVim配置檔案,使用

:w

進行儲存,基本設定如下:

“ 一般設定set nocompatible ”關閉與vi的相容模式set number “顯示行號set nowrap ”不自動折行set showmatch “顯示匹配的括號set scrolloff=3 ”距離頂部和底部3行“set encoding=utf-8 ”編碼set fenc=utf-8 “編碼set fileencodings=utf-8set hlsearch ”搜尋高亮syntax on “語法高亮set tabstop=4 ”tab寬度set shiftwidth=4 set smarttabset backspace=indent,eol,startset expandtab “tab替換為空格鍵set fileformat=unix ”儲存檔案格式set splitbelowset cmdheight=2set completeopt=longest,menuset splitrightset foldmethod=indentset foldlevel=99“ 設定空格為leader鍵let mapleader=” “

使用

vim-plug

安裝NeoVim外掛

vim-plug

是一款Vim外掛管理工具,支援非同步並行,可以快速安裝、更新或解除安裝外掛。可以透過如下命令進行安裝,或手動下載

plug。vim

檔案,複製到在

~/。config/nvim/autoload

資料夾中。

curl -fLo ~/。config/nvim/autoload/plug。vim ——create-dirs \ https://raw。githubusercontent。com/junegunn/vim-plug/master/plug。vim

使用

vim-plug

時,需要在init。vim中新增下列程式碼塊。

” vim-plug外掛管理call plug#begin(‘~/。config/nvim/plugged’)Plug ‘junegunn/vim-plug’call plug#end()

儲存後,在普通模式下輸入

:so %

使配置檔案生效,再透過

:PlugInstall

進行安裝。

常用外掛及設定

下面列舉了一下常用外掛和基本設定:

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 termguicolorslet &t_8f = “\[38;2;%lu;%lu;%lum”let &t_8b = “\[48;2;%lu;%lu;%lum”syntax enablecolorscheme night-owl

如何在你的Android手機上配置 Python 環境?

安裝、配置coc。nvim擴充套件

coc。nvim是一款支援擴充套件的外掛,類似於油猴,可以透過安裝擴充套件,以實現像Vscode一樣使用NeoVim,下面列舉了Python相關的coc擴充套件和配置:

“ coc擴充套件let g:coc_global_extensions = [ \’coc-json‘, \’coc-pyright‘, \’coc-snippets‘, \’coc-xml‘, \’coc-explorer‘, \’coc-prettier‘, \’coc-highlight‘]” 使用tab鍵進行補全選擇inoremap \ pumvisible() ? “\” : \ check_back_space() ? “\” : \ coc#refresh()inoremap pumvisible() ? “\” : “\”function! s:check_back_space() abort let col = col(’。‘) - 1 return !col || getline(’。‘)[col - 1] =~# ’\s‘endfunction“ 使用回車進行補全選擇inoremap pumvisible() ? coc#_select_confirm() \: ”\u\\=coc#on_enter()\“” 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‘, )“ 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。formatFilelet g:prettier#autoformat = 1” 設定Coc Searchnnoremap ? :CocSearch =expand(“”)

此外,coc擴充套件可以透過在

~/。config/nvim/

資料夾中建立

coc-settings。json

檔案來進行配置:

{ “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”], “”: [“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

vim-which-key是一款快捷鍵對映外掛,可以透過簡單的設定進行快捷鍵自定義功能的實現。下面列舉了vim-which-key的簡單設定:

“ 將空格設定為whichkeyleader鍵nnoremap :WhichKey ’” whichkey基本配置let g:which_key_timeout = 100let g:which_key_display_names = {’‘: ’↵‘, ’‘: ’⇆‘}let g:which_key_map = {}let g:which_key_sep = ’→‘let g:which_key_use_floating_win = 0let g:which_key_max_size = 0“ 撥出whichkey時隱藏狀態列autocmd! FileType which_keyautocmd FileType which_key set laststatus=0 noshowmode noruler \| autocmd BufLeave set laststatus=2 noshowmode ruler” 自定義whichkeylet 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‘] = [ ’(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(’‘, “g:which_key_map”)

如何在你的Android手機上配置 Python 環境?

如何在你的Android手機上配置 Python 環境?

如何在你的Android手機上配置 Python 環境?

如何在你的Android手機上配置 Python 環境?

在NeoVim中執行Python指令碼

可以透過在NeoVim中新增自定義功能,從而在編寫時運行當前Python指令碼。

“在普通模式下,按r來執行Python指令碼noremap r :call RunPython() func! RunPython() exec ”w“ if &filetype == ’python‘ exec ”!time python %“ endifendfunc

如何在你的Android手機上配置 Python 環境?

五、結語

本文主要分享了一下在Android平板上配置Python學習環境的經驗,相關配置檔案請參見以下連結:

https://gitee。com/knightz1224/termux_config

,謝謝大家支援!

作者:Python中文社群

原文連結:https://mp。weixin。qq。com/s/v0_NkTOxmvUuaVvt5EYBVA