bgarcial
bgarcial

Reputation: 3193

Different settings.py for each virtual environment in a project

I have a project in where exist two virtual environments such as follow:

I want perform in these two virtual environments different configurations. In my testing environment would go libraries or packets (like selenium), that possibly don't will be needed in the development environment and so...

According to above mentioned I have the following directory structure in relation to the requirements files and settings files of this way:

Requirements files (root_project/requirements)

I have the files:

In addition, after that I've write the packages in each requirements file, I proceed to install the packages in the testing and development environments of this way:

$ workon env_dev
$ pip install -r requirements/development.txt

$ workon env_test
$ pip install -r requirements/testing.txt

Differents settings for each virtual environment (testing and development)

Then, I proceed to create the settings folder inside my root_project folder for manage different settings files por each environment

root_project/settings will have:

__init__.py file to make this folder a Python package

base.py will contain all the settings that are common in all environments. The other setting files inherit from this one.

development.py is for local development.

testing.py is for testing.

production.py will be used in the production environment.

staging.py just in case of run a staging version on the production server of my project.

The files __init__.py , development.py , testing.py , production.py and staging.py , I have edited with these lines for that inherit from base.py (which still don't create):

# -*- coding: utf-8 -*-
from .base import * 

And, in my root_project/name_project/ django create by default the settings.py file. This file, I've moved to root_project/settings directory and I've renamed to base.py, because this file settings.py will be my base.py

With virtualenvwrapper, I specify that file will work with a virtual environment specific:

env_test will work with testing with testing.py

env_dev will work with development.py

In each path [$VIRTUAL_ENV/bin] of each virtual environment (testing and development) I configure this files (development.py and settings.py) to use, focus me in the postactivate file and predeactivate file such as follow

In env_dev environment

Go to $VIRTUAL_ENV/bin path and I edit the postactivate file adding:

export DJANGO_SETTINGS_MODULE="taskbuster.settings.development"

And I also go to $VIRTUAL_ENV/bin path and I edit the predeactivate file adding:

unset DJANGO_SETTINGS_MODULE

In env_test environment

Go to $VIRTUAL_ENV/bin path and I edit the postactivate file adding:

export DJANGO_SETTINGS_MODULE="taskbuster.settings.testing"

I've just adding in postactivate file, not in predeactivate.

When I want check if all this process works, activating the env_dev environment I get this output

(env_dev)➜  taskbuster_project  ./manage.py runserver
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/bgarcial/.virtualenvs/tb_dev/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/bgarcial/.virtualenvs/tb_dev/lib/python3.4/site-packages/django/core/management/__init__.py", line 303, in execute
    settings.INSTALLED_APPS
  File "/home/bgarcial/.virtualenvs/tb_dev/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/home/bgarcial/.virtualenvs/tb_dev/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/bgarcial/.virtualenvs/tb_dev/lib/python3.4/site-packages/django/conf/__init__.py", line 92, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/bgarcial/.virtualenvs/tb_dev/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2189, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2201, in _find_and_load_unlocked
ImportError: No module named 'taskbuster.settings'
(env_dev)➜  taskbuster_project

With my env_test environment occur the same situation Can I have some error configuration.

Excuse me to everyone for the amount of lines in this question. As an additional data all this process I have been doing based in this full complete tutorial http://www.marinamele.com/taskbuster-django-tutorial/settings-different-environments-version-control just in case.

Thanks. Best Regards

Upvotes: 0

Views: 887

Answers (2)

Blueice
Blueice

Reputation: 143

After editing the postactivate and predeactivate you have to reactivate the environment. When you are in env_dev, you can either do:

$workon env_test   //change to another env and come back
$Workon env_dev

OR

$deactivate  //just deactivate and activate the concerned env
$workon env_dev

This should work.

Upvotes: 0

Pynchia
Pynchia

Reputation: 11590

You have set export DJANGO_SETTINGS_MODULE="taskbuster.settings.testing", but above you say the settings sit under root_project/settings. So, if I understand your setup correctly, you need to move the settings directory under one called taskbuster.

Of course, the quicker alternative is to change the env var to export DJANGO_SETTINGS_MODULE="settings.testing"

It depends on what you desire in terms of directory structure.

Upvotes: 1

Related Questions