[英]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\\dump
和file_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
处理的人有所帮助。
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如果你想让它们以任何一种方式工作,都需要
install
、 egg_info
和develop
所有 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 } )
pre-processing
instead of post-processing
, need to move install.run(self)
at the endpre-processing
而不是post-processing
,则需要在最后移动install.run(self)
-vvv
.-vvv
。 Example: pip install -vvv my_pkg
pip install -vvv my_pkg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.