I'm currently trying to make a module written for Python2 compatible with Python3.2.
I started by making sure that all code can be automatically converted with 2to3
and added into setup.py:
use_2to3 = True,
So far, everything is working fine.
Now, I want to test the converted files.
The tests are written such that they can run without 2to3
.
My folder structure is:
# ls /path/to/mymodule
setup.py
mymodule/
tests/
build/
To my setup.py, I added
test_suite = "tests",
Now I execute
rm -rf build/
python3 setup.py build
python3 setup.py test
in order to test my automatically converted code.
But it fails because the test still runs on the mymodule
directory:
File "/path/to/mymodule/mymodule/main.py", line 35
logger.info(u'Scanning {path}'.format(path=self.path))
^
SyntaxError: invalid syntax
[This Syntax error is in MODULE code, not in TEST code.]
[I expect the path to be: /path/to/mymodule/build/lib/mymodule/main.py]
In my build/
are the correctly converted files.
If distribute is converting them correctly, why aren't the tests also executed for the converted files?
Am I missing something? I already checked the documentation of setuptools/distribute for a missing parameter. I don't want to include my tests into the module, as there are several resource files just for testing that would take up unnecessary space.
=> Can I configure my setup.py to run the tests for a different folder when running in python3?
First of all, the line logger.info(u'Scanning {path}'.format(path=self.path))
is not valid in Python 3.2. The syntax for u''
isn't valid as the u
was removed in Python 3.0 and reintroduced in Python 3.3, not 3.2.
This means you should convert your tests to work in Python 3.2 without such Unicode string literals. One alternative is to use bytes literals and decode them (b''.decode('UTF-8')
) or you could simply use strings as they're already Unicode in 3.x+.
Secondly, I'd recommend using tox
for testing your codebase in multiple Python versions. It'll use the provided setup.py
file, create several virtual environments and tests the codebase in each Python version separately. This means you can run your tests in 2.x and 3.x and verify that everything still works.
The 2to3
is not the recommended approach anyway - Python 2.x projects should be ported by adapting the codebase to be compatible with 2.x and 3.x.