9

I'm in the process of learning Django and Python (as well as programming in general). For the sake of simplicity, I am using sqlite3 as my database while I'm going through tutorials for Django and such.

I am a multi-platform user (Mac OS, Windows, Linux) depending on where I am at the time. So, what I have done is put my programming projects in my Dropbox so that I can work on the same code from anywhere.

The problem is that, in the settings.py file for a particular project, I specify the database path like so:

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.sqlite3',                             # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'C:/Users/David/Dropbox/programming/mysite/database',       # Or path to database file if using sqlite3.

... but when I'm using MacOS or Linux, obviously the C:/ chokes. I was wondering if someone had a suggestion of a simple remedy to this. Of course, one way I could do it would be to set up my database remotely on my webserver via MySQL or something, but I thought there might be a simple way to do it such as with an 'if' statement.

David Michael
  • 91
  • 1
  • 2
  • OT: may I ask you how do you manage to work crossplatform? did you install the os specific python on every machine you used? did you setup a virtualenv on every machine too? thx – Bakaburg Aug 23 '16 at 12:07

3 Answers3

22

Using relative paths in settings.py is a common enough to be considered a best practice by many. Something like this may help.

from os.path import dirname, join

PROJECT_DIR = dirname(__file__)

DATABASES = {
    # ...
    'NAME': join(PROJECT_DIR, 'your_db_name.db'),
    # ...
}
istruble
  • 13,363
  • 2
  • 47
  • 52
2

I use both of the above methods where appropriate. Relative paths are in all of my projects, but some required platform detection.

A third method is to import a local_settings file outside version control into the main settings file. from local_settings import * - this local settings file can be different for every environment. If you want to get fancy, you can put in some kind of environment detection that suits you and import the appropriate windows, osx, or linux file.

This method is clean and probably preferable if you have three environments as it won't clutter your settings.py with all environment setups.

Django Local Settings

I personally have even more granular settings files for each environment such as a file for database choices (sometimes, I want a blank sqlite db to easily reset), enabling S3, the debug toolbar, local development (template debug, thumbnail debug etc), caching on/off so that I can mix and match whatever I need for production, staging, various levels of staging debugging, and local development.

Community
  • 1
  • 1
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • The accepted answer to that question you linked to is spot on and also a best practice -- wrap the `from local_settings import *` in a try catch. http://stackoverflow.com/a/4909964/169121 – istruble Jun 04 '12 at 23:17
0

Not a great solution, but you could just test what platform you're running on, like:

import platform
'NAME': 'C:/Users/David/Dropbox/programming/mysite/database' if platform.system() == 'Windows' else '<Linux path to sqlite>'
cberner
  • 3,000
  • 3
  • 22
  • 34