首页 > 解决方案 > 如何从其他文件夹Python导入文件?

问题描述

我的结构是:

- Project

  - Configuration
        - __init__.py
        - system.py
  -GUI
        - gui.py
        - __init__.py

我想system.pygui.py.

所以我需要导入system. gui.py我尝试__ init__.py通过执行以下操作添加文件:

from .Configuration import system

但我有以下信息:attempted relative import with no known parent package

我也试过这个:

import sys; sys.path.insert(0, '../Configuration')

from system import system

但它也不起作用。

我能怎么做?

标签: pythonimport

解决方案


  • 利用from Configuration import system
  • cd .../Project(项目是当前目录)
  • python -m GUI.gui

-m options: python 搜索sys.path命名模块并将其内容作为__main__模块执行。与 -c 选项一样,当前目录将添加到 sys.path 的开头


推荐阅读