Python打包:在安装时生成python文件,使用tox

问题描述 投票:18回答:1

我想在安装时生成一个python文件。

我希望这个工作与python setup.py developpython setup.py installpip install。到现在为止还挺好。

但是我也希望这与tox一起使用。这是我遇到问题的地方。

我使用的方法是调整developinstall命令以在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='[email protected]',
    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(命令挂起,我看不到输出)
  • 添加python shell(即使在install_requires中也没有安装ipython)
  • 运行strace -F这表明setup.py确实在相对于源代码的预期位置

我考虑过的事情:

  • 在运行时创建网络后门shell(太懒)
python build code-generation setuptools tox
1个回答
1
投票

在项目的tox.ini文件中,您可以添加commands以在测试环境中运行。一个简单的例子看起来像这样:

[tox]
envlist = py27,py34,py35,py36

[testenv]
deps=
    pytest
    ; ... other dependencies
commands= 
    pytest --basetemp={envtmpdir} {posargs}
    ; Add your command here?

您是否可以添加命令以使tox执行您想要的操作? (将为每个环境运行该命令)。

© www.soinside.com 2019 - 2024. All rights reserved.