从某些目录中导入python脚本,并带有依赖脚本
我有以下问题,想问您解决问题的最佳方法是什么。
我以下文件结构:
- 主文件夹 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:
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信使用
sys.path.append
从sys
内置模块可以工作。这是语法:
请注意,您的路径不应导致文件,而应到达文件夹
I believe using
sys.path.append
method fromsys
built-in module should work.Here is the syntax :
please note that your path should not lead to a file, but to a folder