繁体   English   中英

如何从其他文件夹 Python 导入文件?

[英]How to import file from other folder Python?

我的结构是:

- Project

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

我想在gui.py中使用system.py

所以我需要在gui.py中导入system 我尝试通过执行以下操作添加__ 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

但它也不起作用。

我能怎么做?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM