문제

I'm working on a system that has two django projects. A server and a client. The server is responsible for managing several client instances. This system relies on Sentry/Raven to process error logging.

My problem is that Sentry needs me to create and configure each client(sentry project) by hand. Since the number of client instances is large and I already have to do this by hand on my server project. I was trying to automatize the process, so that when I create a new client on the server, it creates a new Sentry project.

Much like in this question, I tried to access directly to the Sentry ORM on my project. But this revealed to be a dead end. So I wrote a python scrypt, to do this.

In said script, I import the DJANGO_SETTINGS_MODULE from sentry and work my way around with it until I have what I need.

sys.path.append("/sentry/")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'sentry_configuration_file')

from sentry.models import *

#Do my thing here

If I run the script on my shell, it works perfectly.

However, when I use subprocess to call it inside of my Django project

from subprocess import call
call("/sentry/venv/bin/python /sentry/my_script.py", shell=True)

The script generates the following error on the "from sentry.models import * line:

ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'configurations.settings' (Is it on sys.path?): No module named configurations.settings

You may have noticed that sentry is installed inside a virtualenv. However, I don't need it activated when I call this script on my bash, as long as I provide the correct path to the virtualenv's python.

I'm lost here. I see no reason in particular for the script to fail using subprocess.call when it runs fine using the shell.

Any pointers would be greatly apreciated.

Thanks.

도움이 되었습니까?

해결책

If anyone ever comes across with this question, I managed to solve the issue by replacing subprocess.call by subprocess.Popen

The cool thing about Popen is that you can specify the environment of the process with the argument "env"

So

my_env = os.environ
my_env["DJANGO_SETTINGS_MODULE"] = "sentry_configuration_file"

result = Popen(command, shell=True, env=my_env)

Worked like a charm.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top