从某些目录中导入python脚本,并带有依赖脚本

发布于 2025-02-10 11:20:36 字数 337 浏览 4 评论 0原文

我有以下问题,想问您解决问题的最佳方法是什么。

我以下文件结构:

  1. 主文件夹 1.1 subfolder1 1.1.1 subsubFolder1

i在主文件夹中具有主python脚本,以及子文件夹中的其他SRIPT,它们正在从子填充器中导入其他一些脚本。 我需要在主文件夹中运行主脚本,该文件夹是从子文件夹导入脚本的。 每个子文件夹都是独立的,我只需要在子文件夹中导入并运行可用的脚本(本身就是导入其他脚本)。

我会遇到一个错误,因为当我导入脚本时,运行目录在调用脚本时不会自动更改。

因此,我需要,如果导入脚本,则从相对路径导入了依赖脚本。

谢谢

I have the following problem and would like to ask you what is the best way to solve it.

I the following structure of files:

  1. Main Folder
    1.1 Subfolder1
    1.1.1 SubSubfolder1

I have main python script in main folder, and other sripts in subfolders, which are importing some other scripts from subsubfloders.
I need to run the main script in Main Folder, which is importing scripts from subfolders.
Each subfolder is standalone, and I just need to import and run scripts available in subfolders (which themselves are importing some other scripts).

I am getting an error because when I import scripts, the running directory is not changing automatically when the script is called.

So I need that if the script is imported, the dependent scripts were imported from the relative path.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞琼窗梦回ˉ 2025-02-17 11:20:36

我相信使用sys.path.appendsys内置模块可以工作。
这是语法:

sys.path.append(YourPath)
import YourScript

请注意,您的路径不应导致文件,而应到达文件夹

I believe using sys.path.append method from sys built-in module should work.
Here is the syntax :

sys.path.append(YourPath)
import YourScript

please note that your path should not lead to a file, but to a folder

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文