Python setuptools 入口点和子应用程序作为子进程

标签 python subprocess setuptools entry-point pkg-resources

我正在 Windows 上工作并开发一个将分发给最终用户的应用程序。

我有一个 setupttools 发行版,其中包含大量 python 包。此发行版声明了一些指向我的代码中的各种函数的 console_scripts 入口点。

这些入口点之一注定是更新程序应用程序。我目前正在使用 subprocess 将其作为子进程启动,并指定 python 脚本的完整路径。

我想做的是使用生成的 setuptools 入口点 stub 可执行文件作为要启动的子进程。

我可以通过以下方式获取子应用程序的入口点:

import pkg_resources
updatefunc = pkg_resources.load_entry_point('iti_reporter', 'console_scripts', 'my_update_ui')

这给了我它的Python函数。有没有办法从这个函数转到驻留在我的 venv 中生成的 setuptools 脚本包装器?

感谢您的帮助。

最佳答案

通过执行脚本本来会执行的操作来解决这个问题:

def _get_entry_script(dist, entrytype, entryname):
    import pkg_resources
    mainfunc = pkg_resources.load_entry_point(dist, entrytype, entryname)
    entry_load_script = """import sys; from pkg_resources import load_entry_point;
    sys.exit(
        load_entry_point('%s', '%s', '%s')()
    )"""
    script = entry_load_script%(dist, entrytype, entryname)
    script = ''.join(x.strip() for x in script.split('\n') if x.strip())
    return script

def _get_updater_script_arg_dev():
    return _get_entry_script('mydist', 'console_scripts', 'mypkg_update_ui')

def launch_updater():
    cmd = (os.path.abspath(sys.executable), '-c', _get_updater_script_arg_dev(), '--arg1', '--arg2')
    return subprocess.Popen(cmd)

关于Python setuptools 入口点和子应用程序作为子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18134712/

相关文章:

python - Predix:没有名为 _tkinter 的模块

python - 如何检查 Python 正则表达式引擎是否将给定字符视为 'special'?

python pip 为 entry_points 配置 python 可执行文件

python - 无法安装 python-setuptools : ./configure: 没有这样的文件或目录

Python代码用setuptools打包并用pip安装后看不到数据文件

python - 如何使用 Pandas 数据框删除不在集群中的值?

python - 编写一个快速函数来检查矩阵中的元素

python - 如何执行我的脚本并在 PYQT5 的 GUI 中打印它?

python - 带有子进程的 Python 和 C++ 程序之间的通信速度非常慢

python - 在 python 中,如何检查 subprocess.Popen 对象的标准输出是否有任何可读内容?