Skip to content

Instantly share code, notes, and snippets.

@lucaswiman
Last active September 14, 2016 02:04
Show Gist options
  • Save lucaswiman/218ba1a1c68ba76c4b4bf2fec9ba9aa7 to your computer and use it in GitHub Desktop.
Save lucaswiman/218ba1a1c68ba76c4b4bf2fec9ba9aa7 to your computer and use it in GitHub Desktop.
Upgrading a simple package to python 3

How to make a library Python 3 compatible

Install:

  • tox, for running the test suite parameterized by multiple versions.
  • future, for doing some of the auto-conversion.
pip install tox future

tox for running the test suite.

Now in the project directory, add a tox.ini file:

[tox]
envlist = py27,py35

[testenv]
usedevelop = True
commands =
  COMMAND TO RUN TESTS GOES HERE
deps =
    -r requirements.txt
    OTHER TEST DEPENDENCIES (nose, pytest, etc)

The tests can now be run across python versions by typing tox or tox -e py35 for a particular environment.

futurize for fixing dumb issues

git ls-files | grep '[.]py$' | xargs futurize --stage1 -w

Examine the diff and fix any obvious issues.

Commit those changes, and run the build to see what fails (if anything). Fix test failures. If there are a lot of test failures, look into using six or futurize's more advanced capabilities, but for simple projects, a few try: ... except ImportError: ... cases fix the test failures.

Update trove classifiers in setup.py or setup.cfg

setup( ...
      classifiers=[
          'Development Status :: 3 - Alpha',
          'Intended Audience :: Developers',
          'License :: OSI Approved :: MIT License',
          'Natural Language :: English',
          'Operating System :: OS Independent',
          'Programming Language :: Python',
          'Topic :: Software Development :: Libraries',
          'Topic :: System :: Filesystems',
          'Topic :: Utilities',
          'Programming Language :: Python :: 2',
          'Programming Language :: Python :: 2.7',
          'Programming Language :: Python :: 3',
          'Programming Language :: Python :: 3.5',
          ],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment