简体   繁体   English

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

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

My structure is:我的结构是:

- Project

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

I would like use system.py in gui.py .我想在gui.py中使用system.py

So I need to import system in gui.py .所以我需要在gui.py中导入system I tried to add __ init__.py files by doing:我尝试通过执行以下操作添加__ init__.py文件:

from .Configuration import system

but I have the following message: attempted relative import with no known parent package .但我有以下消息: attempted relative import with no known parent package

I also tried this:我也试过这个:

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

from system import system

But it doesn't work either.但它也不起作用。

How can I do?我能怎么做?

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

-m options : python search sys.path for the named module and execute its contents as the __main__ module. -m options : python 搜索命名模块的sys.path并将其内容作为__main__模块执行。 As with the -c option, the current directory will be added to the start of sys.path .与 -c 选项一样,当前目录将添加到 sys.path 的开头

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

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