I’m semi-new to setuptools in python. I recently added a dependency to my project and encountered an issue with the dependency. Here’s the problem:
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
from mypackage import VERSION
setup(
name='mypackage',
...
version=VERSION,
packages=['mypackage'],
install_requires=['six'])
The problem is that mypackage imports six and thus setup.py fails on fresh installs (six isn’t already installed) due to the from mypackage import VERSION line. I have solved the problem by hacking in a dummy module import (seen below), but I really hope there is a better way that doesn’t require me to maintain the version number in two locations or a separate file.
try:
import six
except ImportError:
# HACK so we can import the VERSION without needing six first
import sys
class HackObj(object):
def __call__(*args):
return HackObj()
def __getattr__(*args):
return HackObj()
sys.modules['six'] = HackObj()
sys.modules['six.moves'] = HackObj()
I am posting the solution that I have been using since many months after asking this question. Thanks to kynan for indirectly prompting me to provide an answer to this question. While the solution kynan posted is great, I personally don’t like having to add a single file just for a version number as it means the version number would be saved in the program under package.version.version. PEP 396 indicates that version numbers should be at the top-level of a package’s or module’s namespace under the name
__version__.The solution I now use uses a simple regex to parse the version number from the line in the python program that defines
__version__. It looks like:The process is similar for modules, replacing
with