简体   繁体   English

如何使用Distutils使Python脚本作为普通的Linux命令运行?

[英]How to make a Python script runs as trivial linux command with Distutils?

I need to run python module as trivial Linux command without worrying about the location of the file. 我需要将python模块作为普通的Linux命令运行,而不必担心文件的位置。 Can I do it with Distutils? 我可以用Distutils吗?

Specific about what I need. 具体我需要什么。 A user should do simple installation and be able to use it: 用户应该进行简单的安装并可以使用它:

python setup.py install
mymodule --arg value

How to do it? 怎么做?

If you want to be able to run the command by specifying just it's name, it needs to be installed in a directory listed in the PATH environment variable. 如果希望仅通过指定名称即可运行该命令,则需要将其安装在PATH环境变量中列出的目录中。 For user programs, the usual locations are /bin , /usr/bin (with /usr/local/bin being fairly common too). 对于用户程序,通常的位置是/bin/usr/bin/usr/local/bin也很常见)。

So setup your package to install your script in one those locations. 因此,设置软件包以将脚本安装在这些位置之一。 However, this will require root privileges. 但是,这将需要root特权。

If you want to be able to install without root privileges, the alternative is to ensure that the script's directory is listed in the PATH variable. 如果您希望能够在没有root特权的情况下进行安装,则可以选择确保在PATH变量中列出了脚本的目录。

If you specify a list of scripts in setup() , distutils will automatically install them into an appropriate directory: 如果您在setup()指定脚本列表,distutils将自动将它们安装到适当的目录中:

from distutils.core import setup

setup(
    name='somemodule',
    scripts=['mymodule']
)

Note that mymodule here for your example will need to be a runnable python script with that name; 请注意,此处的mymodule示例必须是具有该名称的可运行python脚本。 it probably shouldn't be your actual module but rather import your module if necessary. 它可能不应该是您的实际模块,而是在必要时导入您的模块。

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

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