简体   繁体   English

导入具有可变路径的模块?

[英]Importing modules with variable paths?

I have dev and production systems.我有开发和生产系统。

My production system is different from my dev system in that that it adds one directory to the beginning path.我的生产系统与我的开发系统不同,它在开始路径中添加了一个目录。

for eg.例如。 on dev system:在开发系统上:

main->module1->module2主->模块1->模块2

becomes on production:开始生产:

project_name->main->module1->module2.项目名称->main->module1->module2。

Because of that I have to change all my imports to accommodate for this change.因此,我必须更改所有导入以适应此更改。

I wanted to make settings file in the main folder and include it in every file and call exec("import %s.modulexxx" % path).我想在主文件夹中创建设置文件并将其包含在每个文件中并调用 exec("import %s.modulexxx" % path)。

But the problem is how to access settings file (because I also need to know my directory path).但问题是如何访问设置文件(因为我还需要知道我的目录路径)。

Is there a way to include some file below, for eg.有没有办法在下面包含一些文件,例如。 :

if it is main->module1->module2, in module2 I could include../../settings.py so if it changes to project_name->main->module1->module2 it would still work because it would still be 2 level below.如果它是 main->module1->module2,在 module2 中我可以包含../../settings.py 所以如果它更改为 project_name->main->module1->module2 它仍然可以工作,因为它仍然是 2水平以下。

Any help?有什么帮助吗?

Why not just modify sys.path to include the directory one level down?为什么不直接修改sys.path以包含向下一级的目录? Then the same imports will work in both places.然后相同的导入将在两个地方工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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