How do I import a Python script from a sibling directory?

前端 未结 2 874
陌清茗
陌清茗 2020-12-05 06:05

Here is the directory structure:

parent_dir/
    foo_dir/
        foo.py
    bar_dir/
        bar.py

How do I import bar.py into foo.py?

2条回答
  •  青春惊慌失措
    2020-12-05 07:03

    You can use the sys and os modules for generalized imports. In foo.py start with the lines

    import sys
    import os
    sys.path.append(os.path.abspath('../bar_dir'))
    import bar
    

提交回复
热议问题