如何设置 python CLI 应用程序,以便在不直接引用解释器的情况下使用它?
我想构建一个应用程序,假设它名为 helloworld,带有 python 命令行界面。我的应用程序作为多个嵌套模块和一个顶级模块 main.py。
假设布局是:
helloworld/
helloworld/
__init__.py
module1/
module2/
module3/
setup.py
main.py
此时,一旦安装了我的项目,我就可以使用运行它 <代码>#> python path/to/helloworld/main.py arg1 arg2 arg3
我希望能够使用以下命令与程序进行交互: <代码>#> helloworld arg1 arg2 arg3,以类似的方式,Flask 框架附带了一个命令行应用程序,我可以与 #> 一起使用烧瓶运行
。
我环顾四周,发现了一些建议使用 shebang 的问题。据我所知,这不是我想要的。
我想要一个独立的应用程序,我可以使用 pip 安装,然后直接启动,而不需要任何 python 解释器的引用。
我该怎么办?
编辑
对我来说,一个重要的要求是可以使用 pip 安装该软件包并且命令行界面立即可用。 Shebangs/手动修改路径/创建额外的 shell 脚本并不能解决我的问题。
I want to build an application, say it's called helloworld, with a command line interface in python. My application as multiple nested modules and a top level module main.py.
Say the layout is :
helloworld/
helloworld/
__init__.py
module1/
module2/
module3/
setup.py
main.py
At this point, once my project is installed I can run it using#> python path/to/helloworld/main.py arg1 arg2 arg3
I want to be able to interact with the program using commands such as :#> helloworld arg1 arg2 arg3
, in a similar manner as for example, the Flask framework comes with a command line application I can use with #> flask run
.
I looked around and found questions suggesting using a shebang. As far as I can tell, this is not what I want.
I want a self contained application I can install using pip and then launch directly without any reference to the python interpreter.
How should I go about this ?
EDIT
It is an important requirement to me that the package can be installed using pip and the command line interface is immediately available. Shebangs/fiddling manually with the path/creating an additional shell script do not solve my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该将 main.py 添加到您的 PATH 中。当您运行 Flask run 时,您的终端会在 PATH 中查找命令 Flask 并运行它指向的程序。您可以将其视为 Flask 程序的一种快捷方式。
通过将程序添加到 PATH,您可以告诉计算机,如果您在终端中输入
helloworld
,终端应该运行/my/path/to/helloworld.py
。我不知道您使用的是什么操作系统,因此这里是有关如何添加 PATH 变量的最常见操作系统的链接。
Windows
Linux
Mac OSX< /a>
编辑:在您给出更多解释后,
setuptools
就是您要寻找的。请查看此参考以了解如何使用它。You should add main.py to your PATH. What happens when you are running
flask run
is that your teminal looks up the command flask in PATH and runs the program that it is pointing to. You could see it as a kind of shortcut to the program Flask.By adding your program to your PATH, you can tell the computer that if you type
helloworld
in your terminal, the terminal should run/my/path/to/helloworld.py
.I don't know what OS you are on, so here are links for most common OS on how to add a PATH variable.
Windows
Linux
Mac OSX
Edit: After you gave more explanation,
setuptools
is what you are looking for. Please look at this reference to see how to use it.有一个解释这里 如何告诉 setuptools 在 pip 安装期间将脚本添加到 Python Scripts 文件夹中。然后,您可以将该脚本设置为调用 Python 脚本的 bat 文件。通过这种方式,您无需编写
.py
即可运行脚本,并且它会自动发生。There's an explanation here of how to tell setuptools to add a script to the Python Scripts-folder during the pip installation. You can then make that script be for example a bat-file that calls your Python-script. In that way you achieve both that you do not need to write
.py
for your script to run, and that it happens automatically.我知道这个问题已经得到了回答,但以防万一将来有人遇到这个问题。
以下是我在从许多不同来源阅读此内容后正在开发的新包的工作原理。
太棒了;使用
setuptools
并将命令添加为setup.cfg
中[options.entry_points]
下的console_scripts
选项。您还需要
setup.cfg
或setup.py
中的一些其他字段才能进行包发现。请查看 setuptools 文档。示例入口点:
对于本地开发,您可以使用
pip install -e
以可编辑(开发)模式安装软件包。 这将允许您像pip intall
一样测试命令。代码>编辑它。或者,您可以创建一个
__main__.py
,当您从根运行python -m mypackage
时,它会初始化您的模块。我个人选择了两者。通常,您的
main.py
(或我的示例中的app.py
)属于您的包而不是项目级别目录。然后您也可以在那里创建__main__.py
。I know this has already been answered, but just in case someone comes across this in the future..
Here's what worked for a new package I'm developing after reading up on this from many different sources.
tldr; use
setuptools
and add your command as aconsole_scripts
option under[options.entry_points]
insetup.cfg
.You will also need some other fields in
setup.cfg
orsetup.py
for package discovery to work. Check out the setuptools docs for that.Example entry point:
For local development, you can install the package in editable (development) mode with
pip install -e .
which will allow you to test the command as if youpip intall
ed it.Alternatively, you could create a
__main__.py
that initializes your module when you runpython -m mypackage
from the root.I personally opted for both. Usually your
main.py
(orapp.py
in my example) belongs in your package not project level dir. Then you would create__main__.py
there as well.