Python: what does “import” prefer - modules or packages?

后端 未结 2 1815
你的背包
你的背包 2021-01-17 09:59

Suppose in the current directory there is a file named somecode.py, and a directory named somecode which contains an __init__.py file.

2条回答
  •  攒了一身酷
    2021-01-17 10:45

    tested in Windows 10 (Python version 3.5) and on Ubuntu Linux (Python version 2.7 and 3.5) using the following directory:

    https://github.com/alphaCTzo7G/stackexchange/tree/master/python/order_import_module_vs_package

    Result

    The module is always loaded and the following message is printed out every time:

    "you have imported foo/init.py"

    So it seems to be consistent across these Systems, that the package is loaded first.

提交回复
热议问题