简体   繁体   English

python setup.py 配置在自定义目录中安装文件

[英]python setup.py configuration to install files in custom directories

I want to create a setup.py which would install my files into custom directories.我想创建一个 setup.py,它将我的文件安装到自定义目录中。 I have a certain prefix, where I would like to get the following result:我有一个特定的前缀,我想得到以下结果:

/my/prefix/
  bin/
    script.sh
  libexec/
    one.py
    two.py
    ...
  lib/pythonX.Y/site-packages/
    package/...

My initial project is following:我的初始项目如下:

/
  script.sh
  one.py
  two.py
  ...
  setup.py
  package/...
    __init__.py
    ...

What would be the best way to achieve that?实现这一目标的最佳方法是什么? I would like to be able to install it later with something like:我希望以后能够安装它,例如:

python setup.py install --prefix=/my/prefix

I can get "package" nicely installed in the correct directory as lib/pythonX.Y/site-packages under --prefix is the default location.我可以将“包”很好地安装在正确的目录中,因为--prefix下的lib/pythonX.Y/site-packages是默认位置。 But is there a clean way to get script.sh into "bin" and other python files into "libexec"?但是有没有一种干净的方法可以将script.sh放入“bin”并将其他 python 文件放入“libexec”? The only way I see to achieve that would be to manually copy those files in my setup.py script.我看到实现该目标的唯一方法是在我的setup.py脚本中手动复制这些文件。 Maybe there is a cleaner and more standard way to do that?也许有一种更清洁、更标准的方法来做到这一点?

The scripts are handled by use of the scripts parameter to the setup function. 通过使用setup函数的scripts参数来处理scripts For libexec you can treat them as data files and use a data options. 对于libexec,您可以将它们视为数据文件并使用数据选项。

setup(...
    scripts=glob("bin/*"),
    data_files=[(os.path.join(sys.prefix, 'libexec', 'mypackage'), glob("libexec/*"))],
    ...
)

I'm not sure how that would work with a --prefix option, I've never tried that. 我不确定如何使用--prefix选项,我从来没有尝试过。

I ended up with setup.py like that:我最终得到了这样的setup.py

setup(name='mylib',
  scripts=['script.sh'],
  data_files=[('libexec', ['one.py', 'two.py'])]
)

Of course, you could iterate over all python files for libexec, but I only have 2-3 python files I need there.当然,您可以为 libexec 遍历所有 python 个文件,但我只需要 2-3 个 python 个文件。


Additionally, I can have setup.cfg with the following:此外,我可以使用以下内容设置setup.cfg

[install]
prefix=/my/prefix

and instead of python setup.py install --prefix=/my/prefix I can just do:而不是python setup.py install --prefix=/my/prefix我可以这样做:

python setup.py install

This answer was posted as edits ( edit 1 , edit 2 ) to the question python setup.py configuration to install files in custom directories by the OP Ago under CC BY-SA 3.0.该答案作为对问题python setup.py 配置的编辑(编辑 1编辑 2 )发布,以在 CC BY-SA 3.0 下由 OP Ago在自定义目录中安装文件。

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

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