<p>也许这就是你要找的:<a href="https://stackoverflow.com/questions/9905743/how-do-i-run-the-python-sdist-command-from-within-a-python-automated-script-wi">How do i run the python 'sdist' command from within a python automated script without using subprocess?</a></p>
<p>我将展示一些运行babelpython代码的替代方法,而不创建新的子进程,从高到低。在</p>
<p>这是一种黑客攻击,取自上面链接的答案:</p>
<pre><code>from setuptools.dist import Distribution
from babel.messages.frontend import extract_messages
dist = Distribution({'name': 'my-project', 'version': '1.0.0'}) # etc.
dist.script_name = 'setup.py'
cmd = extract_messages(dist)
cmd.ensure_finalized()
cmd.run() # TODO: error handling
</code></pre>
<p>pylabel脚本实际上是这样做的:</p>
^{pr2}$
<p>但是你可以避免在系统argv实际上从babel调用CommandInterface python代码。在</p>
<p>这是我最喜欢的称呼:</p>
<pre><code>from babel.messages.frontend import CommandLineInterface
CommandLineInterface().run(['pybabel','extract','-F','babel.cfg','-k','lazy_gettext','-o','messages.pot','sample_project'])
CommandLineInterface().run(['pybabel','init','-i','messages.pot','-d','translations','-l','en'])
CommandLineInterface().run(['pybabel','compile','-d','translations'])
CommandLineInterface().run(['pybabel','update','-d','translations'])
</code></pre>
<p>这是最接近底层代码的,除非您想开始复制/粘贴和定制python代码。同样,这是一个100%的python解决方案,它不调用新进程。在</p>
<p>祝你好运</p>