How to write Django1.9 environment-independent wsgi.py

I often write it like this,

wsgi.py


import os
import sys

from django.core.wsgi import get_wsgi_application

sys.path.append("/path/to/project/hoge")
sys.path.append("/path/to/project/hoge/hoge")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hoge.settings")

application = get_wsgi_application()

If you write like this, it seems that you can use it even if the environment changes.

wsgi.py


import os
import sys

from django.core.wsgi import get_wsgi_application

FILE_PATH = os.path.dirname(__file__)
PROJECT_NAME = os.path.basename(FILE_PATH)

sys.path.append(os.path.dirname(FILE_PATH))
sys.path.append(FILE_PATH)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", PROJECT_NAME + ".settings")

application = get_wsgi_application()

Recommended Posts

How to write Django1.9 environment-independent wsgi.py
How to write custom validations in the Django REST Framework
How to write a Python class
[Django] How to test Form [TDD]
How to write soberly in pandas
How to reflect CSS in Django
How to get started with Django
How to write Docker base image
Notes on how to write requirements.txt
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to do arithmetic with Django template
How to set optuna (how to write search space)
How to check the version of Django
How to write Python document comments (Docstrings)
How to delete expired sessions in Django
How to do Server-Sent Events in Django
How to convert DateTimeField format in Django
How to write this process in Perl?
How to write Ruby to_s in Python
Summary of how to write AWS Lambda
How to write pydoc and multi-line comments
How to implement Rails helper-like functionality in Django
[Django] How to resolve errors when installing mysqlclient
How to set Django DB to mongodb visual studio 2019
How to develop a cart app with Django
How to reflect ImageField in Django + Docker (pillow)
How to run some script regularly in Django
Answer to "Offline real-time how to write F02 problem"
[Django] How to get data by specifying SQL.
How to print debug messages to the Django console
How to implement "named_scope" of RubyOnRails with Django
How to write a ShellScript Bash for statement
Answer to "Offline Real-time How to Write F01 Problem"
How to create a Rest Api in Django
Answer to "Offline Real-time How to Write E13 Problem"
How to write async and await in Vue.js
How to write a named tuple document in 2020
[Go] How to write or call a function
How to write a ShellScript bash case statement
How to use xml.etree.ElementTree
How to use Python-shell
20th Offline Real-time How to Write Problems in Python
How to use tf.data
How to use virtualenv
[Python] [Django] How to use ChoiceField and how to add options
Scraping 2 How to scrape
How to use Seaboan
How to use image-match
How to use shogun
How to get multiple model objects randomly in Django
How to install Python
How to use Pandas 2
How to read PyPI
How to install pip
How to use numpy.vectorize
How to install archlinux
How to deploy a Django application on Alibaba Cloud
How to use pytest_report_header
How to restart gunicorn
How to install python