简体   繁体   中英

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 .

So I need to import system in gui.py . I tried to add __ init__.py files by doing:

from .Configuration import system

but I have the following message: 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
  • cd.../Project (Project is current dir)
  • python -m GUI.gui

-m options : python search sys.path for the named module and execute its contents as the __main__ module. As with the -c option, the current directory will be added to the start of sys.path .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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