簡體   English   中英

在 Pip 安裝期間將非 Python 文件復制到特定目錄

[英]Copy a non-Python file to specific directory during Pip Install

問題陳述:當我安裝我的 pip 包時,包內的特定文件被處理到Temp目錄

方法:

我的包目錄結構如下:

my-app/
├─ app/
│  ├─ __init__.py
│  ├─ __main__.py
├─ folder-with-extra-stuff/
│  ├─ __init__.py
│  ├─ file_I_want_to_cppy.tar.gz 
├─ setup.py
├─ MANIFEST.in

我正在調整我的 setup.py 文件來完成這項工作。 以下是我的setup.py

#!/usr/bin/env python

from setuptools import setup, find_packages
from setuptools.command.install import install
import os
import sys
import shutil

rootDir = os.path.abspath(os.path.dirname(__file__))

def run_custom_install():
 
    print("--------Start running custom command -------")
    temp_dir = r'c:\temp' if sys.platform == "win32" else r'/tmp'
    temp_col_dir = temp_dir + os.sep + 'dump'
    os.makedirs(temp_dir, exist_ok=True)
    os.makedirs(temp_col_dir, exist_ok=True)
    print("----------locate the zip file ---------------")
    ColDirTests = os.path.abspath(os.path.join(rootDir, 'my-app','folder-with-extra-stuff'))
    _src_file = os.path.join(ColDirTests , 'file_I_want_to_cppy.tar.gz ')
    print(f"******{_src_file}**********")
    if os.path.exists(_src_file):
        print(f"-----zip file has been located at {_src_file}")
        shutil.copy(_src_file, temp_col_dir)
    else:
        print("!!!!Couldn't locate the zip file for transfer!!!!")

class CustomInstall(install):
    def run(self):
        print("***********Custom run from install********")
        install.run(self)
        run_custom_install()

ver = "0.0.0"
setup(
    name='my_pkg',
    version=ver,
    packages=find_packages(),
    python_requires='>=3.6.0',
    install_requires = getRequirements(),
    include_package_data= True,
    cmdclass={
            'install' : CustomInstall,
            }
     )

清單文件

include README.md
include file_I_want_to_cppy.tar.gz
recursive-include my-app *
global-exclude *.pyc
include requirements.txt
prune test

測試構建:

> python setup.py bdist_wheel

它在構建期間工作。 我可以看到里面有一個形成C:\\temp\\dumpfile_I_want_to_cppy.tar.gz的目錄。 但是當我在 pip 中發布包並嘗試從 pip 安裝它時,該文件夾仍然為空!

知道我在這里可能做錯了什么嗎?

經過大量研究,我已經弄清楚如何解決這個問題。 讓我總結一下我的發現,它可能對其他想要進行post_pip_install處理的人有所幫助。

設置文件

  • 安裝包的不同選項:1) pip install pkg_name ,2) python -m setup.py sdist

  • 如果你想讓它們以任何一種方式工作,都需要installegg_infodevelop所有 3 個重復的選項,如 setup.py 所示

  • 如果您通過python -m setup.py bdist_wheel創建*.whl文件,則不會執行post pip install processing 請將使用bdist生成的.tar.gz格式上傳到 PyPi/Artifacts 以使post pip install processing工作。 再次,請注意:從二進制輪安裝時它不起作用

  • 上傳pip包: twine upload dist/*.tar.gz

     from setuptools import setup, find_packages from setuptools.command.install import install from setuptools.command.egg_info import egg_info from setuptools.command.develop import develop rootDir = os.path.abspath(os.path.dirname(__file__)) def run_post_processing(): print("--------Start running custom command -------") # One can Run any Post Processing here that will be executed post pip install class PostInstallCommand(install): def run(self): print("***********Custom run from install********") install.run(self) run_post_processing() class PostEggCommand(egg_info): def run(self): print("***********Custom run from Egg********") egg_info.run(self) run_post_processing() class PostDevelopCommand(develop): def run(self): print("***********Custom run from Develop********") develop.run(self) run_post_processing() ver = "0.0.0" setup( name='my_pkg', version=ver, packages=find_packages(), python_requires='>=3.6.0', install_requires = getRequirements(), include_package_data= True, cmdclass={ 'install' : PostInstallCommand, 'egg_info': PostEggCommand, 'develop': PostDevelopCommand } )

我的研究中還有幾件事:

  1. 如果要進行pre-processing而不是post-processing ,則需要在最后移動install.run(self)
  2. 在 pip 安裝時,如果您想查看安裝前/安裝后的自定義消息,請使用-vvv 示例: pip install -vvv my_pkg

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM