在 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,
}
)
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
测试版本:
> python setup.py bdist_wheel
它正在构建期间工作。我可以看到里面有一个 C:\temp\dump
和 file_I_want_to_cppy.tar.gz
组成的目录。但是当我在 pip 中发布包并尝试从 pip 安装它时,文件夹仍然是空的!
知道我这里可能做错了什么吗?
经过大量研究,我找到了解决此问题的方法。让我总结一下我的发现,这可能对其他想做 post_pip_install
处理的人有帮助。
setup.py
安装包的不同选项:1) pip install pkg_name
, 2) python -m setup.py sdist
如果您想让它们以任何一种方式工作,需要重复 install
、egg_info
和 develop
所有 3 个选项,如 setup.py
如果您通过python -m setup.py bdist_wheel
创建*.whl
文件,post 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
}
)
我研究的其他一些东西:
- 如果你想做
pre-processing
而不是post-processing
,需要把install.run(self)
移到最后
- 在 pip 安装时,如果您想查看 pre/post 安装的自定义消息,请使用
-vvv
。示例:pip install -vvv my_pkg
问题陈述:当我安装我的 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,
}
)
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
测试版本:
> python setup.py bdist_wheel
它正在构建期间工作。我可以看到里面有一个 C:\temp\dump
和 file_I_want_to_cppy.tar.gz
组成的目录。但是当我在 pip 中发布包并尝试从 pip 安装它时,文件夹仍然是空的!
知道我这里可能做错了什么吗?
经过大量研究,我找到了解决此问题的方法。让我总结一下我的发现,这可能对其他想做 post_pip_install
处理的人有帮助。
setup.py
安装包的不同选项:1)
pip install pkg_name
, 2)python -m setup.py sdist
如果您想让它们以任何一种方式工作,需要重复
install
、egg_info
和develop
所有 3 个选项,如 setup.py如果您通过
python -m setup.py bdist_wheel
创建*.whl
文件,post 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 } )
我研究的其他一些东西:
- 如果你想做
pre-processing
而不是post-processing
,需要把install.run(self)
移到最后 - 在 pip 安装时,如果您想查看 pre/post 安装的自定义消息,请使用
-vvv
。示例:pip install -vvv my_pkg