首页 > 解决方案 > 在 Pip 安装期间将非 Python 文件复制到特定目录

问题描述

问题陈述:当我安装我的 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\dump目录file_I_want_to_cppy.tar.gz。但是当我在 pip 中发布包并尝试从 pip 安装它时,该文件夹仍然是空的!

知道我在这里可能做错了什么吗?

标签: pythonsetup.py

解决方案


经过大量研究,我已经弄清楚如何解决这个问题。让我总结一下我的发现,它可能对其他想要进行post_pip_install处理的人有所帮助。

安装程序.py

  • 安装包的不同选项:1) pip install pkg_name, 2)python -m setup.py sdist

  • 如果你想让它们以任何一种方式工作,需要install重复所有 3 个选项,如 setup.pyegg_info所示develop

  • 如果你*.whl通过创建文件python -m setup.py bdist_wheelpost pip install processing 将不会被执行!请将.tar.gz使用生成的格式上传sdist到 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

推荐阅读