Django project won't run if settings.py not in same folder as manage.py


I'm having an issue with PTVS 2.1 alpha where a Django project won't run if the settings file is in a separate folder to manage.py. With PTVS 2.0 this issue did not occur.

I have the following Django project structure:
  • DjangoProject1\DjangoProject1.pyproj
  • DjangoProject1\DjangoProject1.sln
  • DjangoProject1\README.md
  • DjangoProject1\project\
  • DjangoProject1\project\manage.py
  • DjangoProject1\project\config\settings.py
In the project properties I have configured:

Under general:
  • Startup file: project\manage.py
  • Working directory: .
  • Interpreter: project's virtualenv
Under debug:
  • Launch mode: Django launcher
  • Settings module: config.settings
Within manage.py:
if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
    os.environ.setdefault("DJANGO_CONFIGURATION", "Local")

    from configurations.management import execute_from_command_line
Again, no issues with running this configuration using PTVS 2.0. But with PTVS 2.1, running the project doesn't even launch an interpreter window. It just exits with "Build failed" in the status window.

If I try to Execute Project in Python Interactive it reports that "Could not import settings 'config.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named config.settings".


Zooba wrote Feb 19 at 1:06 PM

Yes, this became a more complicated build step in 2.1 (to support Windows Azure publishing better), so it will fail earlier.

I just fixed a related issue yesterday, so I'll check whether this one is also fixed. If so, we should be putting out a dev build soon which will have it, or it may be possible to fix it yourself - I think it only requires editing a .targets file.

Zooba wrote Feb 19 at 6:05 PM

It doesn't quite work with the newest version, but I'm fixing that. When the next dev build comes out, it will have the fix.

One more option you'll have to change is Working Directory - it should be project, so that the "import config.settings" command works correctly. We don't automatically adjust the working directory to the directory containing the startup file, so if you change one you also need to change the other.

(Alternatively, you could add a project/__init__.py file and change the settings module to project.config.settings, but I don't think that's going to be the best solution for you. However, that should work fine with the 2.1 Alpha.)

rowandh wrote Feb 19 at 11:52 PM

Thanks for looking into it. I should have mentioned that I'd tried unsuccessfully changing the working directory to project. I also tried the second option with a project/__init__.py and pointing settings to project.config.settings without much luck.

One suggestion would be to fail louder - I had very little information about why my "build" was failing other than that provided when I ran it in the Interactive Window.

Zooba wrote Feb 20 at 5:45 PM

Yeah, without my other fix it simply isn't going to work. We inadvertently hardcoded the project name in a few places where we should have been using the DjangoSettingsModule property.

There'll shortly be a new version of Microsoft.PythonTools.Django.targets online (pushing changes now) which has most of the fixes. You can download this and replace your existing one (C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Python Tools or similar) to get the benefits. You'll probably also need Microsoft.PythonTools.Web.targets as well.

I'm planning to get a dev build out very soon (tomorrow, hopefully), which will also have the fixes in it. It may be generally less stable than the Alpha though.

Zooba wrote Feb 25 at 11:16 PM

There's a new dev build out that has the fixes you need. You can get it from here (bearing in mind the usual warnings - this is more alpha than our alphas usually are).