Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Django from IDLE

I wish to test my django project form IDLE shell in windows. I run following commands

from django.template import Template, Context
t = Template('test template')

but I get following error.

Traceback (most recent call last):


File "<pyshell#1>", line 1, in <module>
    t = Template('test template')
  File "C:\Program Files\Python26\lib\site-packages\django\template\__init__.py", line 164, in __init__
    if settings.TEMPLATE_DEBUG and origin is None:
  File "C:\Program Files\Python26\lib\site-packages\django\conf\__init__.py", line 28, in __getattr__
    self._import_settings()
  File "C:\Program Files\Python26\lib\site-packages\django\conf\__init__.py", line 59, in _import_settings
    self._target = Settings(settings_module)
  File "C:\Program Files\Python26\lib\site-packages\django\conf\__init__.py", line 94, in __init__
    raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'C:\Program Files\Python26\Lib\site-packages\django\conf' (Is it on sys.path? Does it have syntax errors?): Import by filename is not supported.

Can you help me out?

like image 426
Software Enthusiastic Avatar asked Oct 15 '22 13:10

Software Enthusiastic


2 Answers

Django needs to load several files like your settings.py. To help out with this task, Django comes bundled with its own shell (which can be IDLE). If you have IPython installed, Django will use it instead.

To get to the shell, use the manage.py file in your root directory:

python manage.py shell

If you still really want to use IDLE completely outside the scope of "./manage.py shell", look at the manage.py file to see how Django loads in all the required files.

As a bonus, look into django-command-extensions for shell_plus (it provides some nice additional features like loading all your models automatically).

like image 143
John Paulett Avatar answered Oct 18 '22 08:10

John Paulett


To use idle from the cmd I did the following in Windows 7:

  1. Made sure the Path System Environment Variable included the path to the location of idle.py (like C:\Python27\Lib\idlelib). While I was there I also made sure that both python and the Scripts folder are included (the latter contains django-manage.py when installed by pip)

    Path: C:\Python27\;C:\Python27\Scripts;C:\Python27\Lib\idlelib;...

    The paths shown above may vary depending on your installation of python,

  2. Optional: to avoid typing the .py extension I made sure the PATHTEXT System Environment Variable included the .PY extension.

    PATHTEXT: .COM; .EXE; ...,.PY

Now all I do while working on the cmd is enter the following to run idle:

  • Open a file with idle (being in the same directory):

    idle models.py
    

    this will open the models.py file to edit in idle

  • Open the shell using idle (being in the same directory as manage.py)

    idle -r manage.py shell
    

NOTE: if .PY is not in the PATHTEXT System Environment Variable you may need to add the .py extension to idle

And there you have it, now you can make use of IDLE. Hope that helps.

like image 38
Alexandro Perez Avatar answered Oct 18 '22 07:10

Alexandro Perez