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

后端 未结 3 755
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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 "", line 1, in 
    AttributeError: module 'mod2' has no attribute '__file__'
    

提交回复
热议问题