[英]Importing different Django projects settings
I am trying to import different Django
projects settings file from the same python module. 我正在尝试从同一python模块导入不同的Django
项目设置文件。
project_path = ["/home/Desktop/test1", "/home/Desktop/test2", "/home/Desktop/test3"]
for j, i in enumerate(project_path):
if os.path.exists(i):
sys.path.append(i)
os.system("fm -rf " + project_path[j - 1] + "/settings.pyc")
import settings
print "settings file path>>>", settings.__file__
project_directory = os.path.dirname(settings.__file__)
print "Application direcotry>>>", project_directory
project_name = os.path.basename(project_directory)
print "Application name>>>", project_name
sys.path.append(os.path.join(project_directory, '..'))
project_module = __import__(project_name, '', '', [''])
# Set DJANGO_SETTINGS_MODULE appropriately.
sys.path.remove(i)
and I am getting the following results: 我得到以下结果:
settings file path>>> home/Desktop/test1/settings.pyc
Application direcotry>>> home/Desktop/test1
Application name>>> test1
settings file path>>> home/Desktop/test1/settings.pyc
Application direcotry>>> home/Desktop/test1
Application name>>> test1
settings file path>>> home/Desktop/test1/settings.pyc
Application direcotry>>> home/Desktop/test1
Application name>>> test1
My question is even I am removing the projects path and appending new one then also it is importing the same application path. 我的问题是,即使我要删除项目路径并追加新路径,也要导入相同的应用程序路径。 So I am not able to import the next application path. 因此,我无法导入下一个应用程序路径。
I even tried to reload the settings file. 我什至尝试重新加载设置文件。
import settings
settings = reload(settings)
Try to import with package name and comment unrelevant lines. 尝试使用包名称导入并注释不相关的行。 Make sure folder test1/test2/test3 is package first. 确保文件夹test1 / test2 / test3首先是软件包。 If not, add __init__.py
inside the folder. 如果没有,请在文件夹内添加__init__.py
。
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
project_path = ["/home/Desktop/test1", "/home/Desktop/test2", "/home/Desktop/test3"]
for path in project_path:
sys.path.append(path)
package_name = path.split('/')[2]
cmd = 'import ' + package_name + '.settings'
sys.path.remove(path)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.