Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top