python - 使用Makefile安装pythonrequirements.txt仅更改requirements.txt

标签 python makefile

仅当requirements.txt发生更改时,如何运行目标make install

我不想每次执行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

我确信

Must be a better way

这样做;)

最佳答案

@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

关于python - 使用Makefile安装pythonrequirements.txt仅更改requirements.txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43856980/

相关文章:

python - 为什么 Python docx 或 lxml 将属性从一个节点移动到另一个节点?

python - TensorFlow 对象检测 API : classification weights initialization when changing number of classes at training using pre-trained models

python - 在 Django Rest Framework 中格式化 TimeField 输入和输出的正确方法?

c++ - 制作并重新编译带有子目录的 header

android-ndk 添加静态库到 android.mk

python - 在 Python 中访问多索引系列中的值

python - 如何在 linux (nouveau) 上升级到 Opengl 2.0?

c++ - CMake 找不到 OpenCV 库

c++ - 使用 make 将 Qt 库复制到输出目录

c - 无法使用Makefile链接数学库