setup.py
configparser
, enum34
(enum
), argparse
(for py26)# The easiest!
# just import the new name
import configparser
parser = configparser.ConfigParser()
backports.shutil_get_terminal_size
,
backports.shutil_which
if PY2: # or sys.version_info < (3, 3) if you need to support python3.2
from backports.shutil_get_terminal_size import get_terminal_size
else:
from shutil import get_terminal_size
contextlib2
, functools32
, mock
if PY2:
import contextlib2 as contextlib
else:
import contextlib
# or
if PY2:
from contextlib2 import ExitStack
else:
from contextlib import ExitStack
functools32
) intentionally
error when installed in python 3import sys
from setuptools import setup
if sys.version_info < (3,):
install_requires = ['functools32']
else:
install_requires = []
setup(
...,
install_requires=install_requires,
)
wheel
s are pre-built installations of packagessetup.py
" and zip
the result# Make a wheel with a python 2 interpreter
$ ./venv27/bin/pip wheel . --no-deps
...
# Try and install with a python 3 interpreter
$ ./venv3/bin/pip install test_pkg-0.0.0-py2.py3-none-any.whl
Processing ./test_pkg-0.0.0-py2.py3-none-any.whl
Collecting functools32 (from test-pkg==0.0.0)
Using cached functools32-3.2.3-2.zip
Complete output from command python setup.py egg_info:
This backport is for Python 2.7 only.
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-0bughj6v/functools32/
problem: the setuptools metadata is written once: at build time
$ unzip *.whl
$ grep ^Requires test_pkg-0.0.0.dist-info/METADATA
Requires-Dist: functools32
solution: use environment markers
setup(
...
extras_require={
':python_version=="2.7"': ['functools32'],
},
)
==
pypy
specific packages can use :platform_python_implementation=="PyPy"
)$ ./venv27/bin/pip wheel . --no-deps
...
$ ./venv3/bin/pip install test_pkg-0.0.0-py2.py3-none-any.whl
Processing ./test_pkg-0.0.0-py2.py3-none-any.whl
Installing collected packages: test-pkg
Successfully installed test-pkg-0.0.0
$ ./venv27/bin/pip install test_pkg-0.0.0-py2.py3-none-any.whl
Processing ./test_pkg-0.0.0-py2.py3-none-any.whl
Collecting functools32; python_version == "2.7" (from test-pkg==0.0.0)
Installing collected packages: functools32, test-pkg
Successfully installed functools32-3.2.3.post2 test-pkg-0.0.0
~*~success!~*~