如何设置 python CLI 应用程序,以便在不直接引用解释器的情况下使用它?

发布于 2025-01-16 19:47:06 字数 701 浏览 4 评论 0原文

我想构建一个应用程序,假设它名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疑心病 2025-01-23 19:47:06

您应该将 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.

聆听风音 2025-01-23 19:47:06

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.

遥远的绿洲 2025-01-23 19:47:06

我知道这个问题已经得到了回答,但以防万一将来有人遇到这个问题。

以下是我在从许多不同来源阅读此内容后正在开发的新包的工作原理。

太棒了;使用 setuptools 并将命令添加为 setup.cfg[options.entry_points] 下的 console_scripts 选项。

您还需要 setup.cfgsetup.py 中的一些其他字段才能进行包发现。请查看 setuptools 文档。

示例入口点:

[options.entry_points]
console_scripts =
    mycmd = mypackage.mymodule:myfunc

对于本地开发,您可以使用 pip install -e 以可编辑(开发)模式安装软件包。 这将允许您像 pip intall 一样测试命令。代码>编辑它。

或者,您可以创建一个 __main__.py,当您从根运行 python -m mypackage 时,它会初始化您的模块。

from mypackage import app


if __name__ == '__main__':
    app.main()

我个人选择了两者。通常,您的 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 a console_scripts option under [options.entry_points] in setup.cfg.

You will also need some other fields in setup.cfg or setup.py for package discovery to work. Check out the setuptools docs for that.

Example entry point:

[options.entry_points]
console_scripts =
    mycmd = mypackage.mymodule:myfunc

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 you pip intalled it.

Alternatively, you could create a __main__.py that initializes your module when you run python -m mypackage from the root.

from mypackage import app


if __name__ == '__main__':
    app.main()

I personally opted for both. Usually your main.py (or app.py in my example) belongs in your package not project level dir. Then you would create __main__.py there as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文