简体   繁体   English

在 Pip 安装期间将非 Python 文件复制到特定目录

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

Problem Statement: when I install my pip package, a specific file inside the package get coped to Temp directory问题陈述:当我安装我的 pip 包时,包内的特定文件被处理到Temp目录

Approach:方法:

My Package directory Sturcture is following:我的包目录结构如下:

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

I'm tweaking my setup.py file to do the job.我正在调整我的 setup.py 文件来完成这项工作。 Following is my 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,
            }
     )

MANIFEST.in清单文件

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

Testing build:测试构建:

> python setup.py bdist_wheel

It is working during build.它在构建期间工作。 I can see there is a directory formed C:\\temp\\dump and file_I_want_to_cppy.tar.gz inside it.我可以看到里面有一个形成C:\\temp\\dumpfile_I_want_to_cppy.tar.gz的目录。 But when I release the package in pip and try to install it from pip, the folder remains Empty!但是当我在 pip 中发布包并尝试从 pip 安装它时,该文件夹仍然为空!

Any idea what I might be doing wrong here?知道我在这里可能做错了什么吗?

After a lot of research I have figure out how to resolve this issue.经过大量研究,我已经弄清楚如何解决这个问题。 Let me summarize my findings, it might be helpful for other who wants to do post_pip_install processing.让我总结一下我的发现,它可能对其他想要进行post_pip_install处理的人有所帮助。

setup.py设置文件

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

  • If you want to make them work in either ways, need to have install , egg_info and develop all 3 options repeated as shown in setup.py如果你想让它们以任何一种方式工作,都需要installegg_infodevelop所有 3 个重复的选项,如 setup.py 所示

  • If you create *.whl file by python -m setup.py bdist_wheel , post pip install processing won't be executed!如果您通过python -m setup.py bdist_wheel创建*.whl文件,则不会执行post pip install processing Please upload .tar.gz format generated using bdist to PyPi/Artifacts to make post pip install processing work.请将使用bdist生成的.tar.gz格式上传到 PyPi/Artifacts 以使post pip install processing工作。 Again, Please note: It will not work when installing from a binary wheel再次,请注意:从二进制轮安装时它不起作用

  • upload the pip package: twine upload dist/*.tar.gz上传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 } )

Few More Things from my research:我的研究中还有几件事:

  1. If you want to do pre-processing instead of post-processing , need to move install.run(self) at the end如果要进行pre-processing而不是post-processing ,则需要在最后移动install.run(self)
  2. while pip installing, if you want to see custom messages of pre/post instllation, use -vvv .在 pip 安装时,如果您想查看安装前/安装后的自定义消息,请使用-vvv Example: pip install -vvv my_pkg示例: pip install -vvv my_pkg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM