I am trying to import different Django
projects settings file from the same python module.
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. If not, add __init__.py
inside the folder.
#!/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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.