How to reference python package when filename contains a period

前端 未结 5 380
一生所求
一生所求 2020-12-03 02:51

I am using django and I have a file named models.admin.py and I want to do the following idea in models.py:

from \"models.admin\" import *

5条回答
  •  情歌与酒
    2020-12-03 03:05

    If you really want to, you can import a module with an unusual filename (e.g., a filename containing a '.' before the '.py') using the imp module:

    >>> import imp
    >>> a_b = imp.load_source('a.b', 'a.b.py')
    >>> a_b.x
    "I was defined in a.b.py!"
    

    However, that's generally a bad idea. It's more likely that you're trying to use packages, in which case you should create a directory named "a", containing a file named "b.py"; and then "import a.b" will load a/b.py.

提交回复
热议问题