我正在尝试修改pip,以便在每次安装或卸载后,我的requirements.txt都将得到更新。

为此,我已经在虚拟环境的pip中修改了bin/文件。

pip

#!/Users/username/ProjectEnv/bin/python3

# -*- coding: utf-8 -*-
import re
import sys

from pip import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    status = main()
    if status == 0 or status is None:
        sys.argv = ['pip', 'freeze', '>', '../Project/requirements.txt']
        sys.exit(main())


这没用。我尝试打印命令行参数并设置断点,但它们也无法正常工作。

pip

#!/Users/username/ProjectEnv/bin/python3

# -*- coding: utf-8 -*-
import re
import sys
import pdb
pdb.set_trace()

from pip import main

if __name__ == '__main__':
    for arg in sys.argv:
        print(arg)
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    status = main()
    if status == 0 or status is None:
        sys.argv = ['pip', 'freeze', '>', '../Project/requirements.txt']
        sys.exit(main())


我在这里做错了什么?

最佳答案

您可以只在pip库中自己调用freeze方法。它将返回一个生成器,为您提供在pip --freeze中打印到屏幕的内容的逐行输出。

from pip.operations import freeze

packages = freeze.freeze()
with open('../Project/requirements.txt', 'w') as f:
    for x in packages:
        f.write(x)
        f.write('\n')

关于python - 如何在virtualenv中修改 pip ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41649507/

10-16 11:06