python setup.py sdist和自定义设置关键字不能一起使用

弗朗切斯科·蒙特萨诺(Francesco Montesano)

字幕:不仅是sdist

我正在尝试获取setup.py要与之配合使用的软件包文件sdistsetup.py文件的相关部分是:

from setuptools.command.test import test
[...]
class Tox(test):
   "as described in 
    http://tox.readthedocs.org/en/latest/example/basic.html?highlight=setuptools#integration-with-setuptools-distribute-test-commands"
   [...]
def run_tests(self):
    if self.distribution.install_requires:
        self.distribution.fetch_build_eggs(
            self.distribution.install_requires)
    if self.distribution.tox_requires:
        self.distribution.fetch_build_eggs(self.distribution.tox_requires)
    # import here, cause outside the eggs aren't loaded
    import tox
    import shlex
    args = self.tox_args
    if args:
        args = shlex.split(self.tox_args)
    else:
        args = ""
    errno = tox.cmdline(args=args)
    sys.exit(errno)


entry_points ={}
distutils_ext = {'distutils.setup_keywords': [
                    "tox_requires = setuptools.dist:check_requirements", ]
                 }
entry_points.update(distutils_ext)

setup(
      install_requires=['six', 'numpy', 'matplotlib', 'scipy', 'astropy>=1',
                  'Pillow', ],

    cmdclass={
        'test': PyTest,  # this is to run python setup.py test
        'tox': Tox,
    },

    # list of packages and data
    packages=find_packages(),

    # tests
    tests_require=['pytest', 'pytest-cov'],
    tox_requires=['tox'],
    # other keywords, mostly metadata
)

如果我跑步python setup.py sdist,开始时会收到警告:

/usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'tox_requires'
  warnings.warn(msg)

但是sdist可以正常工作,它会创建一个tar.gz文件,可以用来安装我的软件包。

但是,如果我第二次运行它,它将以(这是Pillow构建的开始)开始:

warning: no previously-included files found matching '.editorconfig'
Building using 4 processes
_imaging.c: In function ‘getink’:

并开始将所有必需的软件包构建到.eggs目录中。

如果删除* egg-info目录,则可以重新运行该命令。如果我注释掉该tox_requires=[...]行,则可以根据需要多次构建sdist。

现在,根据setuptools文档,上面的命令应该是运行将新参数添加到setup函数的正确方法。


按照副标题,问题不仅存在,sdist而且可能是由于我对安装工具和需求的工作方式不了解。

如果我python setup.py tox在没有安装Tox的地方运行,那么在安装一些测试包后,不应安装(即pytestpytest-cov):

追溯(最近一次通话最近):[...]文件“ /usr/lib/python2.7/dist-packages/setuptools/command/test.py”,第127行,with_project_on_sys_path func()文件“ setup.py如果self.distribution.tox_requires是run_tests中第65行,则为:AttributeError:分发实例没有属性'tox_requires'


[更新]这tox_requires也使安装过程中的点子非常混乱。如果已注释掉,我可以安装该软件包而不会出现任何问题;否则,它将开始编译软件包的源,并且系统地失败,因为numpy在构建类似的东西时找不到scipy


如何获得setuptools以识别和正确使用tox_requires

解决此问题后,我认为我可以摆脱这里的虚假安装,在实现Tox类方面做得更好,也许可以覆盖更多的东西,test也可以直接从中派生Command

扬·维尔辛斯基

下述完整的(有效的)解决方案由8个文件(包括short README.rst)组成,共有43行代码。这比原始问题中的代码少。

尽管很短,但它以非常方便的方式支持许多开发和测试方案。

无论如何,它不能完全回答您的问题,但是我敢肯定,它可以满足其背后的要求。

三行长 setup.py

从技术上讲,可以将test包括tox自动化在内的命令放入您的setup.py,但是结果可能非常混乱且难以理解。

可以用简单得多的方式实现相同的结果:

  • 对于开发人员,假设:

    • 使用 git
    • tox安装到系统中
  • 对于包用户:

    • 安装生成的软件包没有特殊要求
  • (可选)如果您希望用户通过单个命令测试程序包并将测试报告保存在中央服务器中:

    • 安装devpi-server并授予您的用户访问权限
    • 要求您的用户安装 $ pip install devpi

该解决方案基于以下工具和软件包:

  • pbr:简化包创建,包括 通过git标签进行版本控制,以及创建git commit消息AUTHORSChangeLog从git commit消息创建。
  • pytest:出色的测试框架,但可以使用任何其他框架代替它。
  • tox:出色的构建和测试自动化工具。
  • coverage:衡量测试覆盖率的工具(工作起来比之前更简单pytest-cov

您也可以选择使用:

  • devpi-server:具有密码保护访问权限的私人PyPi服务器。允许简单测试并提供测试报告收集。
  • devpi:类似于点子的工具。除安装外,还支持运行tox定义的测试(安装,运行测试,按步发布报告)。

创作包

创建新的项目目录并初始化git:

$ mkdir francesco
$ cd francesco
$ git init

创建一个包或模块

在这里,我们创建了单个模块francesco,但是对于更多的模块或包,同样如此。

francesco.py

def main():
    print("Hi, it is me, Francesco, keeping things simple.")

requirements.txt

创建软件包列表以实际安装软件包:

six

test_requirements.txt

定义测试所需的软件包:

pytest
coverage

tests/test_it.py

启动测试套件:

from francesco import main


def test_this():
    main()
    print("All seems fine to me")
    assert True

setup.py

您是否梦想过愚蠢的简单setup.py它去了:

from setuptools import setup

setup(setup_requires=["pbr"], pbr=True)

setup.cfg

元数据属于配置文件:

[metadata]
name = francesco
author = Francesco Montesano
author-email = [email protected]
summary = Nice and simply installed python module supporting testing in different pythons
description-file = README.rst
[files]
modules=francesco
[entry_points]
console_scripts =
    francesco = francesco:main

tox.ini

要配置tox自动构建和测试:

[tox]
envlist = py27, py34

[testenv]
commands =
    coverage run --source francesco -m pytest -sv tests
    coverage report
    coverage html
deps =
    -rtest_requirements.txt

README.rst

永远不要忘记README.rst

===========================================
Complex package with 3 line long `setup.py`
===========================================

Can we keep`setup.py` simple and still support automated testing?

...

tox:在所有受支持的python版本中构建sdist并运行测试

在项目目录根目录下,只需运行单个命令tox

$ tox
GLOB sdist-make: /home/javl/sandbox/setuppy/setup.py
py27 inst-nodeps: /home/javl/sandbox/setuppy/.tox/dist/francesco-0.0.0.zip
py27 runtests: PYTHONHASHSEED='2409409075'
py27 runtests: commands[0] | coverage run --source francesco -m pytest -sv tests
============================= test session starts ==============================
platform linux2 -- Python 2.7.9, pytest-2.8.7, py-1.4.31, pluggy-0.3.1 -- /home/javl/sandbox/setuppy/.tox/py27/bin/python2.7
cachedir: .cache
rootdir: /home/javl/sandbox/setuppy, inifile: 
collecting ... collected 1 items

tests/test_it.py::test_this Hi, it is me, Francesco, keeping things simple.
All seems fine to me
PASSED

=========================== 1 passed in 0.01 seconds ===========================
py27 runtests: commands[1] | coverage report
Name           Stmts   Miss  Cover
----------------------------------
francesco.py       2      0   100%
py27 runtests: commands[2] | coverage html
py34 inst-nodeps: /home/javl/sandbox/setuppy/.tox/dist/francesco-0.0.0.zip
py34 runtests: PYTHONHASHSEED='2409409075'
py34 runtests: commands[0] | coverage run --source francesco -m pytest -sv tests
============================= test session starts ==============================
platform linux -- Python 3.4.2, pytest-2.8.7, py-1.4.31, pluggy-0.3.1 -- /home/javl/sandbox/setuppy/.tox/py34/bin/python3.4
cachedir: .cache
rootdir: /home/javl/sandbox/setuppy, inifile: 
collecting ... collected 1 items

tests/test_it.py::test_this Hi, it is me, Francesco, keeping things simple.
All seems fine to me
PASSED

=========================== 1 passed in 0.01 seconds ===========================
py34 runtests: commands[1] | coverage report
Name           Stmts   Miss  Cover
----------------------------------
francesco.py       2      0   100%
py34 runtests: commands[2] | coverage html
___________________________________ summary ____________________________________
  py27: commands succeeded
  py34: commands succeeded
  congratulations :)

得到sdist

ls .tox/dist
francesco-0.0.0.zip

使用python 2.7 virtualenv开发

激活Python 2.7 virtualenv

$ source .tox/py27/bin/activate

运行测试

(py27) $ py.test -sv tests

==============================================================================================
test session starts
===============================================================================================
platform linux2 -- Python 2.7.9, pytest-2.8.7, py-1.4.31, pluggy-0.3.1
-- /home/javl/sandbox/setuppy/.tox/py27/bin/python2.7 cachedir: .cache
rootdir: /home/javl/sandbox/setuppy, inifile: collected 1 items

tests/test_it.py::test_this Hi, it is me, Francesco, keeping things
simple. All seems fine to me PASSED

============================================================================================
1 passed in 0.01 seconds
============================================================================================

测量测试范围

(py27)$ coverage run --source francesco -m pytest -sv tests
.....
(py27)$ coverage report
Name           Stmts   Miss  Cover
----------------------------------
francesco.py       2      0   100%

在网络浏览器中查看覆盖率报告

(py27)$ coverage html
(py27)$ firefox htmlcov/index.html

发布新的软件包版本

(可选)在本地安装 devpi-server

这里没有介绍devpi-server的安装,但是安装非常简单,特别是如果仅将其安装到本地计算机以进行个人测试时。

提交源代码,分配版本标签

确保已提交所有源代码。

评估版本标签:

$ git tag -a 0.1

通过tox重新运行测试并构建sdist

确保您已停用virtualenv(否则与tox冲突):

(py27)$ deactivate

运行tox

$ tox
.....
...it builds as usual, may fail, if you have forgotten to commit some changes or files...

查找软件包新版本的sdist:

$ ls .tox/dist/francesco-0.1.0.
.tox/dist/francesco-0.1.0.zip

大功告成 您可以像往常一样将软件包的新测试版本分发给用户。

(可选)将sdist上传到devpi-server并在本地进行测试

假设执行以下步骤,您已经devpi-server安装并运行。

$ devpi login javl
...enter your password...
$ devpi upload .tox/dist/francesco-0.1.0.zip

在干净的环境中测试包装

(如果激活,请禁用virtualenv):

$ cd /tmp
$ mkdir testing
$ cd testing
$ devpi test francesco
received http://localhost:3141/javl/dev/+f/4f7/c13fee84bb7c8/francesco-0.1.0.zip
unpacking /tmp/devpi-test6/downloads/francesco-0.1.0.zip to /tmp/devpi-test6/zip
/tmp/devpi-test6/zip/francesco-0.1.0$ tox --installpkg /tmp/devpi-test6/downloads/francesco-0.1.0.zip -i ALL=http://localhost:3141/javl/dev/+simple/ --recreate --result-json /tmp/devpi-test6/zip/toxreport.json
-c /tmp/devpi-test6/zip/francesco-0.1.0/tox.ini
py27 create: /tmp/devpi-test6/zip/francesco-0.1.0/.tox/py27
py27 installdeps: -rtest_requirements.txt
py27 inst: /tmp/devpi-test6/downloads/francesco-0.1.0.zip
py27 installed: coverage==4.0.3,francesco==0.1.0,py==1.4.31,pytest==2.8.7,six==1.10.0,wheel==0.24.0
py27 runtests: PYTHONHASHSEED='3916044270'
py27 runtests: commands[0] | coverage run --source francesco -m pytest -sv tests
============================= test session starts ==============================
platform linux2 -- Python 2.7.9, pytest-2.8.7, py-1.4.31, pluggy-0.3.1 -- /tmp/devpi-test6/zip/francesco-0.1.0/.tox/py27/bin/python2.7
cachedir: .cache
rootdir: /tmp/devpi-test6/zip/francesco-0.1.0, inifile:
collecting ... collected 1 items

tests/test_it.py::test_this Hi, it is me, Francesco, keeping things simple.
All seems fine to me
PASSED

=========================== 1 passed in 0.01 seconds ===========================
py27 runtests: commands[1] | coverage report
Name           Stmts   Miss  Cover
----------------------------------
francesco.py       2      0   100%
py27 runtests: commands[2] | coverage html
py34 create: /tmp/devpi-test6/zip/francesco-0.1.0/.tox/py34
py34 installdeps: -rtest_requirements.txt
py34 inst: /tmp/devpi-test6/downloads/francesco-0.1.0.zip
py34 installed: coverage==4.0.3,francesco==0.1.0,py==1.4.31,pytest==2.8.7,six==1.10.0,wheel==0.24.0
py34 runtests: PYTHONHASHSEED='3916044270'
py34 runtests: commands[0] | coverage run --source francesco -m pytest -sv tests
============================= test session starts ==============================
platform linux -- Python 3.4.2, pytest-2.8.7, py-1.4.31, pluggy-0.3.1 -- /tmp/devpi-test6/zip/francesco-0.1.0/.tox/py34/bin/python3.4
cachedir: .cache
rootdir: /tmp/devpi-test6/zip/francesco-0.1.0, inifile:
collecting ... collected 1 items

tests/test_it.py::test_this Hi, it is me, Francesco, keeping things simple.
All seems fine to me
PASSED

=========================== 1 passed in 0.01 seconds ===========================
py34 runtests: commands[1] | coverage report
Name           Stmts   Miss  Cover
----------------------------------
francesco.py       2      0   100%
py34 runtests: commands[2] | coverage html
____________________________________________________________________________________________________ summary _____________________________________________________________________________________________________
  py27: commands succeeded
  py34: commands succeeded
  congratulations :)
wrote json report at: /tmp/devpi-test6/zip/toxreport.json
posting tox result data to http://localhost:3141/javl/dev/+f/4f7/c13fee84bb7c8/francesco-0.1.0.zip
successfully posted tox result data

您可以在Web浏览器中检查测试结果:

$ firefox http://localhost:3141

然后搜索“ francesco”软件包,单击软件包名称,在表列中找到“毒素结果”,单击此处以显示环境设置和测试结果。

让您的用户测试软件包

假设您devpi-server正在运行,并且您的用户可以访问它。

用户应devpi安装以下命令:

$ pip install devpi

(请注意,该工具未从安装任何工具devpi-server

帮助您的用户获得访问权限devpi-server(此处不覆盖)。

然后,用户只需运行测试:

$ devpi test francesco

运行测试后(它会自动使用tox,但用户不必关心它),您将在devpi Web界面上与之前找到的位置相同的位置找到测试结果。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在python的setup.py中使用“ provides”关键字参数

来自分类Dev

python setup.py中“ provides”关键字参数的用法

来自分类Dev

来自Git存储库的python setup.py sdist

来自分类Dev

如何使用setup.py中的自定义PyPI编写Python包?

来自分类Dev

python setup.py:如何设置生成的.so的路径

来自分类Dev

python setup.py构建忽略一些文件

来自分类Dev

python setup.py构建忽略一些文件

来自分类Dev

在setup.py中使用额外的python包索引URL

来自分类Dev

如何使用setup.py防止意外发布Python pacakge

来自分类Dev

如何在Python 2.7中使用“ setup.cfg”代替setup.py

来自分类Dev

setup.py之后的Python ImportError

来自分类Dev

Python:pip找不到setup.py

来自分类Dev

在setup.py中执行python版本

来自分类Dev

Python setup.py安装失败

来自分类Dev

Python distutils排除setup.py

来自分类Dev

指示debhelper使用python3,但使用python2运行setup.py

来自分类Dev

使用snapcraft构建python2 snap,python setup.py egg_info错误

来自分类Dev

setup.py包含自定义数据文件

来自分类Dev

Python setup.py - 运行 setup.py install 时不要构建轮子

来自分类Dev

如何在Python中的setup.py中包含和安装本地依赖项?

来自分类Dev

IPython与加载python setup.py开发模块不一致吗?

来自分类Dev

Python:使用setup.py install(distutil)时如何强制覆盖文件

来自分类Dev

python,更改用户站点目录或使用--user安装setup.py --prefix

来自分类Dev

从python软件包安装脚本中安装CMake宏脚本(使用setup.py)

来自分类Dev

使用Python setup.py通过development vs install安装不同的依赖项

来自分类Dev

使用 python setup.py install 在 dist 文件夹中创建 tar.gz

来自分类Dev

Python 使用 setup.py 手动将包安装到虚拟环境

来自分类Dev

coverage.py和setup.py及Django测试

来自分类Dev

不使用setuptools解析setup.py

Related 相关文章

  1. 1

    在python的setup.py中使用“ provides”关键字参数

  2. 2

    python setup.py中“ provides”关键字参数的用法

  3. 3

    来自Git存储库的python setup.py sdist

  4. 4

    如何使用setup.py中的自定义PyPI编写Python包?

  5. 5

    python setup.py:如何设置生成的.so的路径

  6. 6

    python setup.py构建忽略一些文件

  7. 7

    python setup.py构建忽略一些文件

  8. 8

    在setup.py中使用额外的python包索引URL

  9. 9

    如何使用setup.py防止意外发布Python pacakge

  10. 10

    如何在Python 2.7中使用“ setup.cfg”代替setup.py

  11. 11

    setup.py之后的Python ImportError

  12. 12

    Python:pip找不到setup.py

  13. 13

    在setup.py中执行python版本

  14. 14

    Python setup.py安装失败

  15. 15

    Python distutils排除setup.py

  16. 16

    指示debhelper使用python3,但使用python2运行setup.py

  17. 17

    使用snapcraft构建python2 snap,python setup.py egg_info错误

  18. 18

    setup.py包含自定义数据文件

  19. 19

    Python setup.py - 运行 setup.py install 时不要构建轮子

  20. 20

    如何在Python中的setup.py中包含和安装本地依赖项?

  21. 21

    IPython与加载python setup.py开发模块不一致吗?

  22. 22

    Python:使用setup.py install(distutil)时如何强制覆盖文件

  23. 23

    python,更改用户站点目录或使用--user安装setup.py --prefix

  24. 24

    从python软件包安装脚本中安装CMake宏脚本(使用setup.py)

  25. 25

    使用Python setup.py通过development vs install安装不同的依赖项

  26. 26

    使用 python setup.py install 在 dist 文件夹中创建 tar.gz

  27. 27

    Python 使用 setup.py 手动将包安装到虚拟环境

  28. 28

    coverage.py和setup.py及Django测试

  29. 29

    不使用setuptools解析setup.py

热门标签

归档