Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tests not being convered by 2to3 in setup.py?

I have a setup.py that needs to support both Python 2 and 3.

The code currently works and is installable in Python 2.x

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

python setup.py test

Causes a failure as one of the tests uses the StringIO class, and the import line goofs in Python 3 (it's currently from StringIO import StringIO, where in Python3 it should be from io import StringIO

I thought though that once you add the use_2to3 keyword all tests (including unittests) were processed by 2to3 before being tested.

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

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 as though 2to3 isn't getting run on a python setup.py test is that it blows up & 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 that import line should've been reworked to:

from io import StringIO

And (at worst) the tests should just individually fail.

like image 769
Adam Parkin Avatar asked Jun 01 '12 16:06

Adam Parkin


People also ask

Is setup py deprecated?

setuptools in Python setuptools is a library which is built on top of distutils that has been deprecated (and up for removal as of Python 3.12).

What should setup py contain?

The setup.py file may be the most significant file that should be placed at the root of the Python project directory. It primarily serves two purposes: It includes choices and metadata about the program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on.

What does Python setup py bdist_wheel do?

What is python setup py bdist_wheel? python setup.py bdist_wheel. This will build any C extensions in the project and then package those and the pure Python code into a . whl file in the dist directory.

What is install requires in setup py?

install_requires is a setuptools setup.py keyword that should be used to specify what a project minimally needs to run correctly. When the project is installed by pip, this is the specification that is used to install its dependencies.


1 Answers

In order for distribute to pick your module up and run in through 2to3, it must be listed in py_modules. So change that to:

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

Unfortunately this has a side-effect of installing test_mymodule when you install the project, which you probably did not want. For cases like this I will generally convert the project into a package with a mymodule.tests sub-package. This way the tests can be "installable" without adding additional clutter.

like image 98
Iguananaut Avatar answered Oct 01 '22 17:10

Iguananaut