2017-05-08 118 views
1

只有在requirements.txt更改時,如何才能運行目標make install使用Makefile安裝python requirements.txt only requirements.txt已更改

我不想每次升級包,當我做make install

我找到了一些解決方法通過創建假的文件_requirements.txt.pyc醜陋。它會拒絕安裝PIP要求第二次因爲requirements.txt沒有變化

$ make install-pip-requirements 
make: Nothing to be done for 'install-pip-requirements'. 

但我的目標是要做到:

# first time, 
$ make install # create virtual environment, install requirements 

# second time 
$ make install # detected and skipping creating virtual env, 
       # detect that requirements.txt have no changes 
       # and skipping installing again all python packages 
make: Nothing to be done for 'install'. 

Python包的樣子:

. 
├── Makefile 
├── README.rst 
├── lambda_handler.py 
└── requirements.txt 

我使用文件,Makefile,用於python中的一些自動化:

/opt/virtual_env: 
    # create virtual env if folder not exists 
    python -m venv /opt/virtual_env 

virtual: /opt/virtual_env 

# if requirements.txt is modified than execute pip install 
_requirements.txt.pyc: requirements.txt 
    /opt/virtual_env/bin/pip install -r --upgrade requirements.txt 
    echo > _requirements.txt.pyc 

requirements: SOME MAGIG OR SOME make flags   
    pip install -r requirements.txt 

install-pip-requirements: _requirements.txt.pyc 

install: virtual requirements 

我相信

必須這樣做更好的方法

;)

+0

你能更詳細地解釋你的python包的外觀嗎?你使用任何類型的Python項目模板?你爲什麼用'sudo'運行make?爲@ovanes更新了 – ovanes

+0

,添加了文件夾樹,並刪除了sudo,因爲與問題無關(假設我是root)。 –

回答

3

不知道它會在這一點上回答你的問題。更好的方法是使用完全成熟的Python PIP項目模板。

我們使用cookiecutter創建一個特定的點子包與此cookiecutter template

它有一個Makefile,它不會不斷重新安裝所有的依賴關係,它使用Python tox,它允許自動在不同的python中運行項目測試。您仍然可以在開發virtualenv中開發,但只有在添加新軟件包時,我們纔會更新它,其他所有內容都由tox來處理。

但是,你到目前爲止所展示的是試圖從頭開始編寫一個Python構建,這是通過大量的項目模板完成的。如果你真的想了解那裏發生了什麼,你可以分析these templates


隨着後續:因爲你希望它有一個makefile工作,我建議從PIP命令去除--upgrade標誌。我懷疑你的要求不包括項目工作所需的版本。我們做了一個經驗,不要在那裏放置版本可能會嚴重製動。因此,我們的requirements.txt樣子:

configure==0.5 
falcon==0.3.0 
futures==3.0.5 
gevent==1.1.1 
greenlet==0.4.9 
gunicorn==19.4.5 
hiredis==0.2.0 
python-mimeparse==1.5.2 
PyYAML==3.11 
redis==2.10.5 
six==1.10.0 
eventlet==0.18.4 

使用不--upgrade原因要求PIP簡單地驗證什麼是在virtualenv中,什麼不是。滿足所需版本的所有內容都將被跳過(無需下載)。你也可以參考git版本,要求如下:

-e git+http://some-url-here/path-to/[email protected]#egg=package-name-how-to-appear-in-pip-freeze 
+0

我不知道爲什麼,但我仍然認爲,我必須更好的方式https://youtu.be/wf-BqAjZb8M?t=1390。我期待一個makefile的解決方案:( –

+0

@ Andrei.Danciuc:好吧,我提供了第二個選擇,將版本引入'requirements.txt' – ovanes

0

@Andrei。Danciuc,make只需要兩個文件進行比較;您可以使用運行pip install的任何輸出文件。

例如,我通常使用「vendored」文件夾,因此我可以將路徑別名爲「vendored」文件夾而不是使用虛擬文件。

# Only run install if requirements.txt is newer than vendored folder 
vendored-folder := vendored 
.PHONY: install 
install: $(vendored-folder) 

$(vendored-folder): requirements.txt 
    rm -rf $(vendored-folder) 
    pip install -r requirements.txt -t $(vendored-folder) 

如果您沒有使用售貨文件夾,下面的代碼應該適用於virtualenv和全局設置。

# Only run install if requirements.txt is newer than SITE_PACKAGES location 
.PHONY: install 
SITE_PACKAGES := $(shell pip show pip | grep '^Location' | cut -f2 -d':') 
install: $(SITE_PACKAGES) 

$(SITE_PACKAGES): requirements.txt 
    pip install -r requirements.txt 
相關問題