[英]The simple answer to importing Python modules
The problem/question of importing modules have been asked a lot in stackoverflow but they are usually filled with multiple convoluted answers. 在stackoverflow中已经对导入模块的问题/问题提出了很多要求,但是通常它们都充满了多个复杂的答案。 Is there any guru out here who can just provide a short direct solution? 这里有没有能提供简短直接解决方案的专家?
So I have my directory structure here and without using changing sys.path
or using PYTHONPATH
. 因此,我在这里拥有目录结构,而无需使用更改sys.path
或PYTHONPATH
。
How do I 我如何
a. 一种。 Allow all files in dir_a and dir_b be able to import each other's functions 允许dir_a和dir_b中的所有文件都能导入彼此的功能
b. b。 Allow all scripts in script_group1 and script_groupt2 to import any module in dir_a and dir_b 允许在script_group1和script_groupt2所有脚本导入任何模块中dir_a和dir_b
my_scripts
|_ script_group1
|_ script1.py
|_ script2.py
|_ script_group2
|_ script3.py
|_ script4.py
my_modules
|_ dir_a
|_ file_a.py
|_ file_b.py
|_ dir_b
|_ file_c.py
|_ file_d.py
EDIT: Ok so after waiting a few days and no one seems to have a good answer or explanation, I decided to accept the fact that this is the way Python is. 编辑:好的,所以等了几天,似乎没有人给出好的答案或解释,我决定接受一个事实,那就是Python是这样的。 The cleanest possible solution for my case would be to edit sys.path but only in the python files in the "my_scripts" directory. 对于我而言,最干净的解决方案是编辑sys.path,但只能在“ my_scripts”目录中的python文件中进行编辑。 These would be the "top-level" scripts that will import common modules in the "my_modules" directory. 这些将是“顶级”脚本,它将在“ my_modules”目录中导入通用模块。 Any common functions will only be written in modules in "my_modules" directory. 任何常用功能都只会写在“ my_modules”目录中的模块中。 No common functions will be written in "my_scripts" 不会在“ my_scripts”中编写通用功能
Inter-module importing between modules in "my_modules" will be done using relative imports. 将使用相对导入来完成“ my_modules”中模块之间的模块间导入。 While scripts in "my_scripts" will perform absolute importing of modules in "my_modules" with 虽然“ my_scripts”中的脚本将使用以下命令绝对导入“ my_modules”中的模块
sys.path.insert(0, path_to_my_modules)
In this way the contamination and damage is limited only to scripts in "my_scripts" whereas using PYTHONPATH will cause a global system wide epidemic. 这样,污染和损害仅限于“ my_scripts”中的脚本,而使用PYTHONPATH将导致整个系统范围的流行。
EDIT: Ok so after waiting a few days and no one seems to have a good answer or explanation, I decided to accept the fact that this is the way Python is. 编辑:好的,所以等了几天,似乎没有人给出好的答案或解释,我决定接受一个事实,那就是Python是这样的。 The cleanest possible solution for my case would be to edit sys.path but only in the python files in the "my_scripts" directory. 对于我而言,最干净的解决方案是编辑sys.path,但只能在“ my_scripts”目录中的python文件中进行编辑。 These would be the "top-level" scripts that will import common modules in the "my_modules" directory. 这些将是“顶级”脚本,它将在“ my_modules”目录中导入通用模块。 Any common functions will only be written in modules in "my_modules" directory. 任何常用功能都只会写在“ my_modules”目录中的模块中。 No common functions will be written in "my_scripts" 不会在“ my_scripts”中编写通用功能
Inter-module importing between modules in "my_modules" will be done using relative imports. 将使用相对导入来完成“ my_modules”中模块之间的模块间导入。 While scripts in "my_scripts" will perform absolute importing of modules in "my_modules" with 虽然“ my_scripts”中的脚本将使用以下命令绝对导入“ my_modules”中的模块
sys.path.insert(0, path_to_my_modules)
In this way the contamination and damage is limited only to scripts in "my_scripts" whereas using PYTHONPATH will cause a global system wide epidemic. 这样,污染和损害仅限于“ my_scripts”中的脚本,而使用PYTHONPATH将导致整个系统范围的流行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.