【发布时间】:2017-10-09 09:41:11
【问题描述】:
我想在安装时生成一个 python 文件。
我希望与python setup.py develop、python setup.py install 和pip install 一起工作。到目前为止一切顺利。
不过,我也希望它能够与 tox 一起使用。这就是我遇到问题的地方。
我使用的方法是调整 develop 和 install 命令以在 setup.py 中生成源代码,如下所示:
# make code as python 3 compatible as possible
from __future__ import absolute_import, division, print_function, unicode_literals
import subprocess
import setuptools
import os.path
import distutils.core
from setuptools.command.develop import develop
from setuptools.command.install import install
# Build anltr files on installation
# this is such a mess... it looks like there are
# no common steps to develop and install
class AntlrDevelopCommand(develop):
def run(self):
compile_grammar()
develop.run(self)
class AntlrInstallCommand(install):
def run(self):
compile_grammar()
install.run(self)
def compile_grammar():
here = os.path.dirname(__file__) or '.'
package_dir = os.path.join(here, 'latex2sympy')
subprocess.check_output(['antlr4', 'PS.g4', '-o', 'gen'], cwd=package_dir)
setuptools.setup(
name='latex2sympy',
version=0.1,
author='august.codes',
author_email='augustt198@gmail.com',
description='Parse latex markup into sympy: suitable for programmatic modifcation',
license='GPLv3',
keywords='MIT',
url='',
packages=['latex2sympy'],
classifiers=[
],
install_requires=['antlr-ast', 'sympy'],
cmdclass=dict(
install=AntlrInstallCommand,
develop=AntlrDevelopCommand),
test_suite='nose.collector'
)
然而,tox 的安装方法似乎以某种方式使setup.py 远离我的源代码,而tox 所代表的魔法黑匣子让弄清楚发生了什么让人恼火。
问题似乎归结为这种巫术魔法,它通过 setup.py 引入 exec.... 出于某种原因。
Command "/home/tom/active/latex2sympy/.tox/py35/bin/python3.5 -u -c "import setuptools, tokenize;__file__='/tmp/pip-e698cucb-build/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-lu2idbzz-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/tom/active/latex2sympy/.tox/py35/include/site/python3.5/latex2sympy" failed with error code 1 in /tmp/pip-e698cucb-build/
我尝试过的事情:
- 使用 -v -v -v -v 运行
- 手动重新运行 pip 命令
- 添加
pdb.set_trace(命令挂起,我看不到输出) - 添加 ipython shell(即使在 install_required 中也未安装 ipython)
- 运行
strace -F这表明setup.py确实位于相对于源代码的预期位置
我考虑过的尝试:
- 在运行时创建网络后门 shell(太懒了)
【问题讨论】:
-
看起来像 tox 旧版解释器支持来处理 PEP 263 定义(
tokenize.open隐式调用detect_encoding)并将 MS 换行符更改为 *nix 换行符。我会说这是 pip (我假设这就是 mktempingpip-*-builddirs)移交给 tox 之间的问题? -
我想你想使用 tox 进行测试,那么在 tox.ini 文件中指定
usedevelop = True会不会有问题?这更像是一种解决方法,但对于您的用例来说可能就足够了。
标签: python build code-generation setuptools tox