I’m installing a package using setup.py:
python setup.py install
My __init__.py in the package I’m installing includes some package level checks, one of it checks whether an attribute is available in settings.py. Since it is a redistributable package the settings.py is not delivered via the package, but the user has to take care that the settings are correctly set in their project wide settings.py.
from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
#check if settings are properly set
if not hasattr(settings, 'PACKAGE_SPECIFIC_SETTING'):
raise ImproperlyConfigured('Please add the PACKAGE_SPECIFIC_SETTING setting to your settings.py')
Now I’m wondering why the call setup.py install runs my __init__.py (and crashes because it, not very surprisingly, does not find any settings.)
I would have expected that setup.py only copies my package and does not run any code. The code should be run whenever another app using my package imports my package into his code.
Edit: Included setup.py as requested
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4 coding=utf-8
from distutils.core import setup
setup(
name='django-simple-lock',
version=__import__('lock').__version__,
license = 'GNU Lesser General Public License (LGPL), Version 3',
requires = ['python (>= 2.5)', 'django (>= 1.3)'],
provides = ['lock'],
description='Simple locking implementation as a reusable'
'Django app.',
long_description=open('README.rst').read(),
url='http://github.com/mr-stateradio/django-simple-lock',
packages=['lock', 'lock.tests'],
classifiers = [
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
'Programming Language :: Python',
'Topic :: Database',
'Topic :: Software Development :: Libraries :: Python Modules',
],
)
Edit: Included traceback:
/Users/Me/.virtualenvs/django1_4/bin/python setup.py install
Traceback (most recent call last):
File "setup.py", line 9, in <module>
version=__import__('lock').__version__,
File "/01_Development/django-simple-lock/lock/__init__.py", line 8, in <module>
raise ImproperlyConfigured('Please add the PACKAGE_SPECIFIC_SETTING setting to your settings.py')
Your
setup.pyis executable python. It is run as a python script to discover your package distribution configuration.Your
setup.pyruns this code:This imports your
lockpackage, so the__init__.pyfile in that package is loaded. Remove that call and your setup will succeed. Store the version somewhere else instead.Note that the trackback even tells you so explicitly:
Best practice is to store the version in the
setup.pyfile instead (some examples here and here). The Django project does use the__import__trick, but it’s__init__.pyonly contains the version information, nothing else.