Praful Bagai
Praful Bagai

Reputation: 17372

How to access a file from outside the Django Directory

Currently, I'm working in file Proj/gui/apps/dashboard/views.py . I want to import a file which is present outside the Django directory, ie Proj/services/constants/global_variables.py. How do I do it?

Here is my Project directory.

Proj
    |-- gui
    |   |-- apps
    |   |   |   |-- dashboard
    |   |   |       |-- __init__.py
    |   |   |       |-- models.py
    |   |   |       |-- tests.py
    |   |   |       |-- views.py
    |   |-- gui
    |   |   |-- __init__.py
    |   |   |-- settings.py
    |   |   |-- urls.py
    |   |   `-- wsgi.py
    |   |-- manage.py
    |-- service
        |-- constants
        |   |-- device_parameters.json
        |   |-- global_variables.py
        |   |-- __init__.py

Upvotes: 1

Views: 3363

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174624

Two suggestions:

  1. Add your global variables to settings.py; then:

    from django.conf import settings
    print(settings.SOME_VAR) # where SOME_VAR is from your global_variables.py
    

    or, move your file to the same directory as settings.py, and then in the bottom of settings.py, add:

    try:
        import global_variables as gv
    except ImportError:
        pass
    

    Then, you can do:

    from django.conf import settings
    from django.core.exception import ImproperlyConfigured
    
    if hasattr(settings, 'gv'):
        print(settings.gv.SOME_VAR)
    else:
        raise ImproperlyConfigured
    
  2. Add the full path of the directory of the module to sys.path; but this is discouraged because django provides a way for having your own custom settings.


I think an example would be better here. Suppose you want to have a custom variable MY_VAR and have it available to your django project.

Simply add it to settings.py, anywhere. I would recommend at the bottom:

# normal settings.py template code
# ..
# Custom settings

MY_VAR = True

Now anywhere you want to access that variable, import the settings helper, like this:

from django.conf import settings
from django.shortcuts import render

def my_view(request):
    return render(request, 'file.html', {'x': settings.MY_VAR})

If you already have all your settings in another python file, step one is to move it to the same location as settings.py; then you can either do a "wild card" import (not recommended), or import it with an alias.

So if you have

-- my_proj
   |-- __init__.py
   |-- settings.py
   |-- urls.py
   |-- wsgi.py
   |-- my_settings.py

At the bottom of settings.py you would write this snippet; which tries to import my_settings.py:

try:
   import my_settings as cs  # cs = custom settings, but this can be anything
except ImportError:
   pass  # silently fail if the file cannot be found

Now, all the names in my_settings.py can be accessed using:

from django.conf import settings
from django.shortcuts import render

def my_view(request):
    return render(request, 'file.html', {'x': settings.cs.MY_VAR})

Upvotes: 1

bruno desthuilliers
bruno desthuilliers

Reputation: 77892

Add /Proj/service to your sys.path, it'll make the constants package available. You'll find more infos here : http://docs.python.org/2/tutorial/modules.html#the-module-search-path

Upvotes: 3

Related Questions