Search code examples
pythondjangopython-3.xcygwin

Running Django in Cygwin can't always find SECRET_KEY


I'm using the Windows version of Python 3.4.3 (can't use Cygwin's Python because I require pywin32), Cygwin 2.0.4(0.287/5/3), and Django 1.7.8 (can't use 1.8.x for dependency reasons).

My problem is that about half the time I do anything Django related in the Cygwin shell, I get error -

Traceback (most recent call last):
  File "C:\dev\web\.env\lib\site-packages\django\test\testcases.py", line 182, in __call__
    self._pre_setup()
  File "C:\dev\web\.env\lib\site-packages\django\test\testcases.py", line 754, in _pre_setup
    self._fixture_setup()
  File "C:\dev\web\.env\lib\site-packages\django\test\testcases.py", line 887, in _fixture_setup
    if not connections_support_transactions():
  File "C:\dev\web\.env\lib\site-packages\django\test\testcases.py", line 874, in connections_support_transactions
    for conn in connections.all())
  File "C:\dev\web\.env\lib\site-packages\django\db\utils.py", line 257, in all
    return [self[alias] for alias in self]
  File "C:\dev\web\.env\lib\site-packages\django\db\utils.py", line 254, in __iter__
    return iter(self.databases)
  File "C:\dev\web\.env\lib\site-packages\django\utils\functional.py", line 55, in __get__
    res = instance.__dict__[self.func.__name__] = self.func(instance)
  File "C:\dev\web\.env\lib\site-packages\django\db\utils.py", line 148, in databases
    self._databases = settings.DATABASES
  File "C:\dev\web\.env\lib\site-packages\django\conf\__init__.py", line 46, in __getattr__
    self._setup(name)
  File "C:\dev\web\.env\lib\site-packages\django\conf\__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\dev\web\.env\lib\site-packages\django\conf\__init__.py", line 115, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

In this case, I'm attempting to run a simple unit test using nose. Strange thing is that it intermittently occurs about 50% of the time.

I can confirm that my PYTHONPATH is set properly with this small test.

printf "\033c";printf "+++ python sys.path\n\n";python -c "import sys;print(sys.path)";printf "\n+++ building...\n\n"; nosetests app -v

Which outputs.

+++ python sys.path                                                                                                                                                          

['', 'C:\\dev\\web', 'C:\\WINDOWS\\system32\\python34.zip', 'C:\\dev\\web\\.env\\DLLs', 'C:\\dev\\web\\.env\\lib', 'C:\\dev\\web\\.env\\Scripts', 'C:\\Python34\\Lib', 'C:\\Python34\\DLLs', 'C:\\dev\\web\\.env', 'C:\\dev\\web\\.env\\lib\\site-packages', 'C:\\dev\\web\\.env\\lib\\site-packages\\win32', 'C:\\dev\\web\\.env\\lib\\site-packages\\win32\\lib', 'C:\\dev\\web\\.env\\lib\\site-packages\\Pythonwin']                                                                           

+++ building...                                                                                                                                                              

ERROR                                                                                                                                                                        

======================================================================                                                                                                       
ERROR: test_foo (asd.tests.FooTests)                                                                                                                                         
----------------------------------------------------------------------                                                                                                       
Traceback (most recent call last):                                                                                                                                           
  File "C:\dev\web\.env\lib\site-packages\django\test\testcases.py", line 182, in __call__                                                                               
    self._pre_setup()                                                                                                                                                        
  File "C:\dev\web\.env\lib\site-packages\django\test\testcases.py", line 754, in _pre_setup                                                                             
    self._fixture_setup()                                                                                                                                                    
  File "C:\dev\web\.env\lib\site-packages\django\test\testcases.py", line 887, in _fixture_setup                                                                         
    if not connections_support_transactions():                                                                                                                               
  File "C:\dev\web\.env\lib\site-packages\django\test\testcases.py", line 874, in connections_support_transactions                                                       
    for conn in connections.all())                                                                                                                                           
  File "C:\dev\web\.env\lib\site-packages\django\db\utils.py", line 257, in all                                                                                          
    return [self[alias] for alias in self]                                                                                                                                   
  File "C:\dev\web\.env\lib\site-packages\django\db\utils.py", line 254, in __iter__                                                                                     
    return iter(self.databases)                                                                                                                                              
  File "C:\dev\web\.env\lib\site-packages\django\utils\functional.py", line 55, in __get__                                                                               
    res = instance.__dict__[self.func.__name__] = self.func(instance)                                                                                                        
  File "C:\dev\web\.env\lib\site-packages\django\db\utils.py", line 148, in databases                                                                                    
    self._databases = settings.DATABASES                                                                                                                                     
  File "C:\dev\web\.env\lib\site-packages\django\conf\__init__.py", line 46, in __getattr__                                                                              
    self._setup(name)                                                                                                                                                        
  File "C:\dev\web\.env\lib\site-packages\django\conf\__init__.py", line 42, in _setup                                                                                   
    self._wrapped = Settings(settings_module)                                                                                                                                
  File "C:\dev\web\.env\lib\site-packages\django\conf\__init__.py", line 115, in __init__                                                                                
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")                                                                                                  
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.                                                                                       

----------------------------------------------------------------------                                                                                                       
Ran 0 tests in 0.327s                                                                                                                                                        

FAILED (errors=1)  

What would cause such discrepancy?


Solution

  • Here is what ended up being the true cause.

    I had environment variables DJANGO_SETTINGS_MODULE and django_settings_module - same name, different case. DJANGO_SETTINGS_MODULE was set appropriately to config.settings.local, while django_settings_module was set to asd.

    I had no idea that 1) both of these were defined and 2) the shell (Cygwin shell, anyways) randomly returns a value despite case differences.