2015-05-29 73 views
0

如果所需的庫尚未安裝,是否可以編寫一個小的Python腳本來自動安裝所需的Python庫導入?通過代理安裝python包的自動化腳本?

目前我使用

try: 
    import xlrd #Library to iterate through excel docs 
except ImportError: 
    raise ImportError('XLRD not installed, use "sudo pip install xlrd"\n') 

,但希望更多的東西自動化。

+1

你爲什麼不發佈你的程序有一個'setup.py' /'requirements.txt'? – jonrsharpe

+0

我如何讓他們輕鬆安裝所有要求? – ryan

+1

pip install -r requirements.txt? – ryan

回答

0

正如在評論中提到的那樣,setup.py方法是一個很好的方法,非常簡單。

除此之外,如果您需要安裝在多臺服務器,我建議你看一下解決方案像http://www.ansible.com/homehttps://puppetlabs.com/

作爲一次性的,你可以做的依賴太:

ssh [email protected] 'pip install xlrd' 

編輯 - 繼續評論:

一旦你創建了setup.py指定xlrd的依賴關係,你必須在每臺機器上運行它你想安裝你的應用程序。爲了完成上面我建議的自動化工具。

你也可以這樣做,但說真的髒:

try: 
    import xlrd #Library to iterate through excel docs 
except ImportError: 
    from subprocess import call 

    call(["pip", "install", "xlrd"]) 
+0

可以在setup.py中安裝_requires而不是進入代理設置? – ryan

+0

或者我如何通過本地安裝? – ryan

+0

結賬我的編輯 – Maresh