python - 如何从其他文件夹Python导入文件?
问题描述
我的结构是:
- Project
- Configuration
- __init__.py
- system.py
-GUI
- gui.py
- __init__.py
我想system.py
在gui.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
但它也不起作用。
我能怎么做?
解决方案
- 利用
from Configuration import system
cd .../Project
(项目是当前目录)python -m GUI.gui
-m options
: python 搜索sys.path
命名模块并将其内容作为__main__
模块执行。与 -c 选项一样,当前目录将添加到 sys.path 的开头。
推荐阅读
- matlab - 手动从图像中提取边界并防止所选点重叠
- java - 尝试运行代码时出现 Java 错误:找不到文件
- php - php下载excel文件
- gcc - 如何在 Windows 2000 机器上使用 Go 编译器获取 GCC9.x 或最新的 gcc
- sql-server - 有没有办法在 SQL Server 中将日期格式化为 MMDDYYYY 而不是 YYYYMMDD?
- node.js - 如何在生产中使用 express-rate-limit
- jquery - 关闭一些 ajax 调用的 nprogress
- windows - 为什么“${0%/*}”在我的机器上没有按预期工作?
- scala - 如何管理两个 Akka Actor 之间的同步?
- java - 使用 String[] args 传递参数