This project has moved and is read-only. For the latest updates, please go here.

Import existing Django project in PTVS

Sep 17, 2013 at 2:23 PM
I want to import an existing Django project in PTVS.
Project structure is:

I specified the path to I added the path to the RGT project as a search path.
It's not working on win7/VS2012/PTVS2.0: the settings file is not found (on sys.path...). Django or PTVS seems to expect the settings file to be in the src folder.
Do you have any suggestions for this?
Also, what is the recommended way to import a Django project? I noticed two ways: create a default Django project, and add existing files to it, or import sources (but then there is no wsgi file with it).

Sep 17, 2013 at 10:10 PM
Does your RGT folder have an in it? I would expect your to contain something like:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DjangoProject1.settings")
which will specify how to get to your settings file. If you can post that, then that might help figure out what needs to be tweaked to make this work. If RGT has an I would expect the value to be RGT.settings and I would suggest adding the RGT folder to the search path.

If you're doing add existing code and doesn't already exist we wouldn't create it for you. Typically that gets created by the Django new project / new app scripts.
Sep 18, 2013 at 7:31 AM
Thank you for your message! Here are more details.
The current setup uses the following.
Project was created from modifying default Django project (so I have a wsgi file with it).
In the, I added a line:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DjangoProject1.src.RGT.settings")
This points to the right settings file. Tracing through the code, VS reads the settings file, so it all seems fine, until the Django overrides the correct value with the default one:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\2.0\", line 76, in exec_file
exec(code_obj, global_variables)
File "c:\users\dan\documents\visual studio 2012\Projects\DjangoProject1\DjangoProject1\DjangoProject1\src\RGT\", line 15, in <module>
File "C:\Python27\lib\site-packages\django\core\", line 459, in execute_manager
File "C:\Python27\lib\site-packages\django\core\", line 382, in execute
File "C:\Python27\lib\site-packages\django\core\", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "C:\Python27\lib\site-packages\django\core\", line 69, in load_command_class
module = import_module('' % (app_name, name))
File "C:\Python27\lib\site-packages\django\utils\", line 35, in import_module
File "C:\Python27\lib\site-packages\django\core\management\commands\", line 8, in <module>
from django.core.servers.basehttp import AdminMediaHandler, run, WSGIServerException, get_internal_wsgi_application
File "C:\Python27\lib\site-packages\django\core\servers\", line 26, in <module>
from django.views import static
File "C:\Python27\lib\site-packages\django\views\", line 95, in <module>
template_translatable = ugettext_noop(u"Index of %(directory)s")
File "C:\Python27\lib\site-packages\django\utils\", line 75, in gettext_noop
return _trans.gettext_noop(message)
File "C:\Python27\lib\site-packages\django\utils\", line 48, in getattr
if settings.USE_I18N:
File "C:\Python27\lib\site-packages\django\utils\", line 184, in inner
File "C:\Python27\lib\site-packages\django\", line 42, in _setup
self._wrapped = Settings(settings_module)
File "C:\Python27\lib\site-packages\django\", line 95, in init
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'DjangoProjectRGT.settings' (Is it on sys.path?): No module named DjangoProjectRGT.settings

The Django code is:
class Settings(BaseSettings):
    def __init__(self, settings_module):
        # update this dict from global settings (but only for ALL_CAPS settings)
        for setting in dir(global_settings):
            if setting == setting.upper():
                setattr(self, setting, getattr(global_settings, setting))

        # store the settings module in case someone later cares
        self.SETTINGS_MODULE = settings_module

            mod = importlib.import_module(self.SETTINGS_MODULE)
        except ImportError, e:
            raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
More tracing in the Django (1.4.3) code shows this in C:\Python27\Lib\site-packages\django\core\
def setup_environ(settings_mod, original_settings_path=None):
    Configures the runtime environment. This can also be used by external
    scripts wanting to set up a similar environment to
    Returns the project directory (assuming the passed settings module is
    directly in the project directory).

    The "original_settings_path" parameter is optional, but recommended, since
    trying to work out the original path from the module can be problematic.
Ok, this reminds me of the import settings problem.
Tracing back, I find:
def execute_manager(settings_mod, argv=None):
    Like execute_from_command_line(), but for use by, a
    project-specific utility.
        "The 'execute_manager' function is deprecated, "
        "you likely need to update your ''; "
        "please see the Django 1.4 release notes "
Ok, so I replace execute_manager in with:
But, sys.argv has:
print sys.argv
['c:\users\dan\documents\visual studio 2012\Projects\DjangoProject1\DjangoProject1\DjangoProject1\src\RGT\', 'runserver', '--noreload', '--settings', 'DjangoProjectRGT.settings', '1934']
This results in the same error as initially. Do you know why sys.argv got the 'DjangoProjectRGT.settings' value?
Sep 21, 2013 at 3:01 PM
Here is an update: I had some more time to look into this and I found the issue: sys.argv takes the path to settings from the properties of the project. So, if someone has a similar issue: go to the properties of the project (not solution), click on Debug, change 'settings module' to indicate where the settings module is (it should be the same as the value for DJANGO_SETTINGS_MODULE).