distutils
模块允许与Python模块一起包含和安装资源文件。如果资源文件应该在构建过程中生成,如何正确地包含它们?
例如,该项目是一个web应用程序,其中包含应编译为JavaScript并包含在CoffeeScript包中的Python源程序。有没有办法把它集成到一个正常的sdist/bdist进程中?
发布于 2013-01-23 00:21:20
最好的方法是编写一个自定义的build_coffeescript命令,并使其成为build的子命令。在对类似/重复问题的其他回复中提供了更多详细信息,例如这个问题:
发布于 2014-01-18 05:25:35
我花了一段时间弄清楚这个问题,各种建议都以不同的方式被打破了-它们破坏了依赖项的安装,或者它们在pip中不起作用,等等。这是我的解决方案:
在setup.py中:
from setuptools import setup, find_packages
from setuptools.command.install import install
from distutils.command.install import install as _install
class install_(install):
# inject your own code into this func as you see fit
def run(self):
ret = None
if self.old_and_unmanageable or self.single_version_externally_managed:
ret = _install.run(self)
else:
caller = sys._getframe(2)
caller_module = caller.f_globals.get('__name__','')
caller_name = caller.f_code.co_name
if caller_module != 'distutils.dist' or caller_name!='run_commands':
_install.run(self)
else:
self.do_egg_install()
# This is just an example, a post-install hook
# It's a nice way to get at your installed module though
import site
site.addsitedir(self.install_lib)
sys.path.insert(0, self.install_lib)
from mymodule import install_hooks
install_hooks.post_install()
return ret
然后,在调用setup函数时,传递arg:
cmdclass={'install': install_}
你可以使用同样的想法来构建而不是安装,给自己写一个装饰器来让它更容易,等等。这已经通过pip和直接的'python setup.py install‘调用进行了测试。
https://stackoverflow.com/questions/14441955
复制相似问题