Import existing Django project in PTVS

Sep 17, 2013 at 2:23 PM
Hello,
I want to import an existing Django project in PTVS.
Project structure is:
proj
-src
--RGT
---settings.py
---manage.py
-static
-templates

I specified the path to manage.py. 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).

Thanks,
Dan
Coordinator
Sep 17, 2013 at 10:10 PM
Does your RGT folder have an init.py in it? I would expect your manage.py 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 init.py 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 wsgi.py 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 manage.py, 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\visualstudio_py_util.py", line 76, in exec_file
exec(code_obj, global_variables)
File "c:\users\dan\documents\visual studio 2012\Projects\DjangoProject1\DjangoProject1\DjangoProject1\src\RGT\manage.py", line 15, in <module>
execute_manager(settings)
File "C:\Python27\lib\site-packages\django\core\management__init__.py", line 459, in execute_manager
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "C:\Python27\lib\site-packages\django\core\management__init__.py", line 69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in import_module
__import__(name)
File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.py", 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\basehttp.py", line 26, in <module>
from django.views import static
File "C:\Python27\lib\site-packages\django\views\static.py", line 95, in <module>
template_translatable = ugettext_noop(u"Index of %(directory)s")
File "C:\Python27\lib\site-packages\django\utils\translation__init__.py", line 75, in gettext_noop
return _trans.gettext_noop(message)
File "C:\Python27\lib\site-packages\django\utils\translation__init__.py", line 48, in getattr
if settings.USE_I18N:
File "C:\Python27\lib\site-packages\django\utils\functional.py", line 184, in inner
self._setup()
File "C:\Python27\lib\site-packages\django\conf__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "C:\Python27\lib\site-packages\django\conf__init__.py", 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

        try:
            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\management__init__.py:
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 manage.py.
    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:
    execute_manager(settings)
and:
def execute_manager(settings_mod, argv=None):
    """
    Like execute_from_command_line(), but for use by manage.py, a
    project-specific django-admin.py utility.
    """
    warnings.warn(
        "The 'execute_manager' function is deprecated, "
        "you likely need to update your 'manage.py'; "
        "please see the Django 1.4 release notes "
        "(https://docs.djangoproject.com/en/dev/releases/1.4/).",
        PendingDeprecationWarning)
Ok, so I replace execute_manager in manage.py with:
    execute_from_command_line(sys.argv)
But, sys.argv has:
print sys.argv
['c:\users\dan\documents\visual studio 2012\Projects\DjangoProject1\DjangoProject1\DjangoProject1\src\RGT\manage.py', '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).