Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:03:23+00:00 2026-06-07T00:03:23+00:00

I am using the os module to have relative paths in my Django projects

  • 0

I am using the os module to have relative paths in my Django projects settings.py file. The variable SITE_ROOT is set to the current working directory of the settings.py file and then used to reference all of the static/media directories also located in that same directory.

Heres my issue:

print os.getcwd()
print os.path.abspath(os.path.dirname(__file__))

In settings.py, the above statements both have identical outputs. but my template will only load if I use SITE_ROOT = os.path.abspath(os.path.dirname(__file__))

Django looks for the templates here:

TEMPLATE_DIRS = (
    os.path.join(SITE_ROOT, 'templates'),
)

SITE_ROOT set to os.getcwd() seems to make Django look for the templates folder in the directory ABOVE the settings.py file

I can just as easily not use os.getcwd() and my site runs fine, but I am curious what may be going on here 🙂

Anyone know?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:03:23+00:00Added an answer on June 7, 2026 at 12:03 am

    As mouad said, os.getcwd() won’t give you exactly what you’re expecting.

    os.getcwd() does a bit more than returning the current working directory. It defaults to $PWD in your env. It’s not where the script is located but where you were when you executed the script.

    Being in /home/user and doing python manage.py, os.getcwd() will return /home/user
    Being in /home/ and doing python user/manage.py, os.getcwd() will return /home

    But it’s still won’t be always true since it’s possible to use os.chdir(). It is in other word like doing cd. It will also change the return value of os.getcwd().

    On the other hand. __file__ is the path of the module file. So you have to use this to be certain to have a path relative to your module instead of the current working directory that may change.

    As ShawnFumo said, __file__ might not be always absolute. To get a better idea on how it works, you can check that answer: Python __file__ attribute. Also, as of Python3.4 __file__ should always be an absolute path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using module pattern in JavaScript. I have a file say controller.js which
I have successfully been using Win32::Ole module to browser through pages and submit forms.
I'm using the javascript module pattern, and have this so far: var APP; if(APP
we using symfony 1.0. We have an module work with Propel objects. The sql-querys
I have been trying to manupulate registry key from python using the win32api module
I am using modular MVC with codeigniter. I have a module playlist in which
I have a python module that I've been using over the years to process
I have a rails module included for reference below that I have included using
I have installed Numpy using ActivePython and when I try to import numpy module,
I have a module containing multiple global functions, and a global variable. The variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.