Question

I'm trying to maintain dependencies using pip install -r requirements.txt. However, some of required packages do not support Python 3 directly, but can be converted manually using 2to3.

Is there a way to force pip to run 2to3 on those packages automagically when doing pip install -r requirements.txt?

Was it helpful?

Solution

No, it needs to be part of the package setup configuration instead. See Supporting both Python 2 and 3 with Distribute.

You add metadata to your package installer:

setup(
    name='your.module',
    version = '1.0',
    description='This is your awesome module',
    author='You',
    author_email='your@email',
    package_dir = {'': 'src'},
    packages = ['your', 'your.module'],
    test_suite = 'your.module.tests',
    use_2to3 = True,
    convert_2to3_doctests = ['src/your/module/README.txt'],
    use_2to3_fixers = ['your.fixers'],
    use_2to3_exclude_fixers = ['lib2to3.fixes.fix_import'],
)

Such a package would then automatically run 2to3 on installation into a Python 3 system.

2to3 is a tool, not a magic bullet, you cannot apply it to an arbitrary package pip downloads from PyPI. The package needs to support it in the way it is coded. Thus, running it automatically from pip is not going to work; the responsibility lies with the package maintainer.

Note that just because 2to3 runs successfully on a package, it does not necessarily follow the package will work in Python 3. Assumptions about bytes vs. unicode usually crop up when you actually start using the package.

Contact the maintainers of the packages you are interested in and ask what the status is for that package for Python 3. Supplying patches to them usually helps. If such requests and offers for help fall on deaf ears, for Open Source packages you can always fork them and apply the necessary changes yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top