简体   繁体   English

我如何运行脚本文件作为python setup.py安装的一部分?

[英]How would I run a script file as part of the python setup.py install?

Question

I know how to use setup.py with setuptools to register a script. 我知道如何使用setup.pysetuptools来注册脚本。 How would I run another script file (let say a make file) as part of the python setup.py install . 我将如何运行另一个脚本文件(比如一个make文件)作为python setup.py install

Background 背景

I imagine that I would use something like: 我想我会使用类似的东西:

os.system('make maketarget') #from somewhere in the package

But setuptools.setup receives a dict so I can't just add this line inside setup() / and I need the script to run after the basic package is installed by setup.py install . 但是setuptools.setup收到一个字典,所以我不能在setup() /中添加这一行,我需要在setup.py install基本软件包之后运行该脚本。

I know I can add a command to setup.py but I want this script to be called inside the install step. 我知道我可以向setup.py添加命令,但我想在安装步骤中调用此脚本。

I can also default to just placing a: 我也可以默认只放置一个:

if sys.argv[-1] == 'install':
    os.system('do something in the shell')

and just place this block after the setup(), but somehow this doesn't look very pytonic (and also error prone, I need to find where this package installed exactly etc) 并且只是在setup()之后放置这个块,但不知怎的,这看起来不是非常pytonic(也容易出错,我需要找到这个软件包的安装位置等)

You can subclass the existing install command from setuptools and extend it with the desired functionality. 您可以从setuptools继承现有的install命令,并使用所需的功能对其进行扩展。 Afterwards you can register it as the install command in the setup function: 然后,您可以在setup函数中将其注册为install命令:

from setuptools.command.install import install

class MyInstall(install):

    def run(self):
        install.run(self)
        print("\n\n\n\nI did it!!!!\n\n\n\n")

# in the setup function:
cmdclass={'install': MyInstall}

Instead of the print statement just call your script. 而不是print语句只需调用您的脚本。

Please be aware that there are many ways of packaging python software (eggs, etc.). 请注意,有很多方法可以打包python软件(鸡蛋等)。 Your script won't be called for these ways if you only call it inside the install step. 如果您只在安装步骤中调用它,则不会为这些方法调用您的脚本。

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

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