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

后端 未结 3 391
借酒劲吻你
借酒劲吻你 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:28

    This is an example:

    files:

    test
    |
    import_test
    ├── foo
    │   ├── bar.py
    │   ├── bar.pyc
    │   ├── __init__.py
    │   └── __init__.pyc
    ├── __init__.py
    ├── __init__.pyc
    └── tata
        ├── foo.py
        ├── foo.pyc
        ├── __init__.py
        ├── __init__.pyc
        └── yoyo.py
    

    yoyo.py:

    #!/usr/bin/env python
    # encoding: utf-8
    from __future__ import absolute_import
    from ..foo import bar
    
    
    print 'cool'
    

    Test command:

    cd test    
    python -m import_test.tata.yoyo
    

    output:

    cool
    

提交回复
热议问题