Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: 'Module' object has no attribute '__file__'

Tags:

django

I'm setting up a new dev environment on a Windows box, and after successfully installing Python and django, I cloned my repository on the new machine.

After running manage.py syncdb successfully, I ran manage.py runserver, but hitting localhost:8000 results in the error on the title. I ran django-admin.py startproject testproject and ran manage.py runserver from there, and that project loads fine, so I think I may have ruled out my django install, but I need a next step here.

Here is the stacktrace from my app that bombs. Any help you can give me would be appreciated.

Environment:


Request Method: GET
Request URL: http://localhost:8000/

Django Version: 1.4.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'api',
 'contact',
 'lessons',
 'mainsite',
 'piston',
 'registration',
 'utils')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Matthew\myapp\harmonyschedules\mainsite\views.py" in index
  16.     return render_to_response('mainsite/index_redes.html', context_instance=RequestContext(request))
File "C:\Python27\lib\site-packages\django\shortcuts\__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "C:\Python27\lib\site-packages\django\template\loader.py" in render_to_string
  169.         t = get_template(template_name)
File "C:\Python27\lib\site-packages\django\template\loader.py" in get_template
  145.     template, origin = find_template(template_name)
File "C:\Python27\lib\site-packages\django\template\loader.py" in find_template
  128.             loader = find_template_loader(loader_name)
File "C:\Python27\lib\site-packages\django\template\loader.py" in find_template_loader
  95.             mod = import_module(module)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)
File "C:\Python27\lib\site-packages\django\template\loaders\app_directories.py" in <module>
  24.     template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates')

Exception Type: AttributeError at /
Exception Value: 'module' object has no attribute '__file__'
like image 759
Matthew Calabresi Avatar asked Sep 22 '12 02:09

Matthew Calabresi


3 Answers

Update for Python 3.X

While you can import modules without an __init__.py file present,

The __init__.py files are required to make Python treat the directories as containing packages

https://docs.python.org/3/tutorial/modules.html

Without __init__.py, the module has no __file__ attribute.

example/
|-- mod1
|   `-- __init__.py
`-- mod2


>>> import mod1
>>> mod1.__file__
'/tmp/example/mod1/__init__.py'
>>> import mod2
>>> mod2.__file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'mod2' has no attribute '__file__'
like image 146
Wyrmwood Avatar answered Nov 12 '22 08:11

Wyrmwood


This problem occurs when trying to use application when it's not a Python Package, so make sure your application has __init__.py file in its root directory.

like image 39
WBAR Avatar answered Nov 12 '22 09:11

WBAR


The problem happens when importing your settings.INSTALLED_APPS:

for app in settings.INSTALLED_APPS:
    try:
        mod = import_module(app)
    except ImportError as e:
        raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
    template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates')

It seems that it will be much easier to determine which one of the modules is causing the problem. One common way to make debugging this kind of problem in Django easier is by using Werkzeug, where you can have an in-browser debugger to quickly see what values the variables are when error occurs.

Now, I strongly suspect the module that's causing this problem is piston[relevant thread]. You can fix it by creating a __init__.py file in whatever directory piston is in.

like image 17
K Z Avatar answered Nov 12 '22 10:11

K Z