Tests do not convert to 2to3 in setup.py?

I have setup.py that should support both Python 2 and 3.

This code currently works and installs in Python 2.x

If I add the use_2to3 = True sentence to my setup.py, then the module can be installed in Python 3, however, do:

 python setup.py test 

It crashes because one of the tests uses the StringIO class and the import string in Python 3 (currently from StringIO import StringIO , where in Python3 it should be from io import StringIO

I thought that after you add the use_2to3 keyword, all tests (including unittests) were processed on 2to3 before testing.

What am I missing? In case this helps, the bulk of my setup.py looks like this:

 from setuptools import setup setup( name='myproject', version='1.0', description='My Cool project', classifiers = [ 'Programming Language :: Python', 'Programming Language :: Python :: 3', ], py_modules=['mymodule'], test_suite='test_mymodule', zip_safe=False, use_2to3 = True, ) 

Edit: The reason I feel that 2to3 does not start on python setup.py test is because it explodes and the bottom of the stacktrace reads:

 File "/home/aparkin/temp/mymodule/test_mymodule.py", line 18, in <module> from StringIO import StringIO 

But if I ran 2to3 on test_mymodule.py, then this import line should have been reworked for:

 from io import StringIO 

And (in the worst case) the tests should just fail.

+6
source share
1 answer

To distribute, to select your module and run it through 2to3, it must be specified in py_modules. So change this to:

 py_modules=['mymodule', 'test_mymodule'], 

Unfortunately, this has a side effect when setting test_mymodule when setting up a project that you probably did not want. For such cases, I usually convert the project into a package with the mymodule.tests subpackage. Thus, tests can be “installable” without adding any additional interference.

+1
source

Source: https://habr.com/ru/post/917204/


All Articles