[英]How to package python dependencies properly
我目前正在 python 中編寫一個 Vim 插件,該插件依賴於某些 python 包(硒和請求)。
Vim 內置了自己的 python。因此,即使我在 python 中編寫插件,用戶也可能不一定安裝了 Z23EEEB7947BDD755DFCZB6。
我可以想到2個選項:
pip -target
/venv 手動發送)
我錯過了什么嗎? 我應該怎么辦?
IMO,您不應從 vim 觸發pip
。 最終用戶可能無法安裝,或者可能不想在他的 python 環境中安裝東西。 我會對這樣的 Vim 插件感到非常惱火。
相反,我會清楚地記錄需要哪些 Python 包,並在加載插件時測試它們是否可用。 從這里,您可以靜默禁用插件,或發出警告,或以降級模式運行...
例如,當我需要非常規的 Python 包時,我使用以下測試。
function! lh#python#can_import(module) abort
let flavour = lh#python#best_still_avail()
if empty(flavour) | return 0 | endif
try
exe flavour.' import '.a:module
catch /.*/
return 0
endtry
return 1
endfunction
NB: lh#python#best_still_avail()
is another function that tries to detect whether we are using python 2 or 3. If you always use :pyx
, :python
or :python3
, you could simply use exe 'pyx import '.a:module
取而代之的是exe 'pyx import '.a:module
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.