我正在 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/