从已安装的软件包中执行Python脚本

发布于 2025-02-09 16:55:01 字数 354 浏览 0 评论 0原文

I have a Python package that contains a script:

pip show -f my-package

Results in

my-package-path/script.py

I would like to execute script.py, something like:

pip install my-package
python3 my-package-path.script.py

But it doesn't work.这样做的标准方式是什么?

I have a Python package that contains a script:

pip show -f my-package

Results in

my-package-path/script.py

I would like to execute script.py, something like:

pip install my-package
python3 my-package-path.script.py

But it doesn't work. What's the standard way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夜司空 2025-02-16 16:55:01

如果您访问并修改my -package setup.py(如果使用setuptools) - 您可以将Entry_points添加到setup()呼叫,如果正确完成,则可以运行将脚本直接添加到路径中。示例:

NAME="AISTool"

setup(
    name=f"{NAME}",
    # ... extra stuff removed ...
    entry_points={
        'console_scripts': [
            f'{NAME}=AISTool.main:main'
        ],
    },
    # ... extra stuff removed ...
)

现在,当该软件包安装后,将有一个可执行的可执行文件aistool,该软件包运行main(Main()方法从aistool.main.main.main

If you access and modify my-package setup.py (if setuptools is used) - you could add entry_points to setup() call and if thats done correctly, you can run the script directly as its added to the path. Example:

NAME="AISTool"

setup(
    name=f"{NAME}",
    # ... extra stuff removed ...
    entry_points={
        'console_scripts': [
            f'{NAME}=AISTool.main:main'
        ],
    },
    # ... extra stuff removed ...
)

Now, when that package gets installed, there will be an executable called AISTool which runs main() method from AISTool.main

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