AttributeError: 'module' object has no attribute '__file__' (django\utils\translation\trans_real.py)

Andrey Pochekutov avatarAndrey Pochekutov created an issue

i have config:

INSTALLED_APPS = (
	'django.contrib.auth',
	'django.contrib.contenttypes',
	'django.contrib.sessions',
	'django.contrib.admin',
	'django.contrib.messages',
	'django.contrib.markup',
	'sorl.thumbnail',
	'debug_toolbar',
	'piston',

...
)

start project in virtual env:

virtualenv --no-site-packages --distribute .env
pip install -E .env django  django-debug-toolbar sorl-thumbnail trans simplejson
pip install -E .env hg+http://bitbucket.org/jespern/django-piston

run server, and got error:

... File "c:\projects\repos\mypersonal\y2010\avrorakras\.env\lib\site-packages\dja ngo\utils\translation\trans_real.py", line 176, in translation default_translation = _fetch(settings.LANGUAGE_CODE) File "c:\projects\repos\mypersonal\y2010\avrorakras\.env\lib\site-packages\dja ngo\utils\translation\trans_real.py", line 160, in _fetch apppath = os.path.join(os.path.dirname(app.file), 'locale') AttributeError: 'module' object has no attribute 'file'

This error in code trans_real.py, where appname is piston:

        for appname in settings.INSTALLED_APPS:
            app = import_module(appname)
            apppath = os.path.join(os.path.dirname(app.__file__), 'locale')

after i add empty file init.py to .env\Lib\site-packages\piston\ server run successfily

Comments (31)

  1. jmagnusson

    I'm getting this even though I have applied Brodie's patch:

    /usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages/virtualenvwrapper/hook_loader.py:16: UserWarning: Module piston was already imported from None, but /Users/[user]/Sites/event/src/django-piston is being added to sys.path
    

    I'm on Python 2.7.1 and using Django 1.3 final.

  2. Anonymous

    This issue appears to be in 0.2.3. I got it when doing ./manage.py runserver. Downgraded to 0.2.2 and problem went away. Using virtualenv ( fairly recent ) and Django 1.3.1.

  3. Anonymous

    Same issue with me. Piston 0.2.3 on Django 1.4 alpha (svn trunk Rev: 17047). Downgrading to 0.2.2 works just fine.

  4. SocialCode

    Hi folks -

    So this isn't actually a Piston issue.

    Piston uses a packaging feature of setuptools called which allows you to have multiple packages installed that share a common package namespace. When you install Piston using pip, it ignores the __init__.py and creates an nspkg.pth file in site-packages like so:

    $ pip install django-piston
    Downloading/unpacking django-piston
      Downloading django-piston-0.2.3.tar.gz
      Running setup.py egg_info for package django-piston
    Installing collected packages: django-piston
      Running setup.py install for django-piston
        Skipping installation of $VIRTUAL_ENV/lib/python2.6/site-packages/piston/__init__.py (namespace package)
        Installing $VIRTUAL_ENV/lib/python2.6/site-packages/django_piston-0.2.3-py2.6-nspkg.pth
    Successfully installed django-piston
    Cleaning up...
    
    

    So when Django's i18n engine looks for piston.__file__ , it doesn't find it - Piston doesn't get imported the way a plain Python package does because of the nspkg.pth file. When you use setup.py to install it, the __init__.py file does get included, so piston.__file__ is then defined. Django should not be relying upon that attribute being defined.

    This is being tracked in the Django bug tracker: https://code.djangoproject.com/ticket/17304

    I'm looking into whether there are packages out there that rely upon Piston being a namespaced package (removing it as the fork has done would be backward incompatible to other packages that install themselves in the piston namespace), but for the time being, I can't accept this patch.

    -jag

  5. Mikhail Lukyanchenko

    Django realy should not wark this way. But world is not perfect. You can't say the problem doesn't exists just beacause upstream dependancy works in some non-standard or non-obvious maner. This issue renders Piston unusable for projects with I18L support enabled, namely almost any multi-language project.

  6. Joshua Ginsberg

    There's an easy workaround: don't use pip to install it. I'm hoping to discover how much the namespace_package feature is relied upon, because I don't like introducing backward incompatibilities, but I'm inclined to agree that we should work around Django's bug.

  7. danros
    • changed status to open

    Regardless of what breakage *may* occur with this patch, the unfortunate reality is that piston is broken when installing using the most common installation method. This surely trumps everything else?

    I know piston to be good software so I do the trivial workaround of 'touch init.py'. A newcomer however might dismiss piston as 'broken' and look elsewhere, which would be a shame.

  8. Nuno Khan

    guys i dont understand comment #16 ,can you elaborate a bit further? i installed pip, but after i got this error i downloaded the latest src and still gives me the same error. what should i do? i am using django 1.4

  9. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.