import Python module when there is sibling file with the same name

后端 未结 3 412
借酒劲吻你
借酒劲吻你 2021-01-28 04:50

Suppose I have following files

tata/foo.py
tata/yoyo.py

foo/__init__.py
foo/bar.py

In file foo.py I do

import foo         


        
3条回答
  •  忘掉有多难
    2021-01-28 05:18

    This seems to be a classical issue described in PEP 328

    a local module or package can shadow another hanging directly off sys.path

    to deal with it:

    1. Code should be executed as module rather than script (-m option).
    2. Use Python 3 which has so-called "absolute import behaviour" or add

      from __future__ import absolute_import
      

提交回复
热议问题