![](/img/trans.png)
[英]How to `pip install` a package that has non-Python dependencies?
[英]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\\dump
和file_I_want_to_cppy.tar.gz
的目錄。 但是當我在 pip 中發布包並嘗試從 pip 安裝它時,該文件夾仍然為空!
知道我在這里可能做錯了什么嗎?
經過大量研究,我已經弄清楚如何解決這個問題。 讓我總結一下我的發現,它可能對其他想要進行post_pip_install
處理的人有所幫助。
安裝包的不同選項: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
! 請將使用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 } )
pre-processing
而不是post-processing
,則需要在最后移動install.run(self)
-vvv
。 示例: pip install -vvv my_pkg
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.