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

后端 未结 3 756
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 00:51

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

相关标签:
3条回答
  • 2021-01-12 01:37

    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.

    0 讨论(0)
  • 2021-01-12 01:48

    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.

    0 讨论(0)
  • 2021-01-12 01:49

    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__'
    
    0 讨论(0)
提交回复
热议问题