How to use Django 1.8.2 with Python 2.7?

66 views
Skip to first unread message

jsa...@nvidia.com

unread,
Aug 11, 2015, 12:57:44 PM8/11/15
to Django users
I'm studying the Django Project tutorial using Python 2.7 (because that's my department's current standard) and Django 1.8.2 (because that's the current stable version).

The tutorial says, "If you are still using Python 2.7, you will need to adjust the code samples slightly, as described in comments." What comments does it mean?

The section "Playing with the API" instructs me to run the command:

$ python manage.py shell
or
>>> import django
>>> django.setup()

Being a methodical sort, I tried both. The first works. The second gives me a bunch of errors, beginning with:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python27\lib\site-packages\django\__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)

I think it's complaining about an incompatibility between Python 2.7 and Python 3.2.

I can evade the immediate problem by using "python manage.py shell" instead, but I expect further problems in short order, and I need to know how to fix them.

So, where are these "comments" that tell me what to do? I don't see any in that part of the tutorial. I opened __init__.py, and there are no helpful comments there, either.

I could figure this out myself, but I can't see debugging my way through the entire Django codebase. I'm being paid to write an application, not fix up Django.

Either I'm missing the comments that the tutorial promised me, or it has left me in the lurch. Can someone explain, please?

Andrew Farrell

unread,
Aug 11, 2015, 1:14:31 PM8/11/15
to django...@googlegroups.com
Hi jsachs,

Could you reply to the list with a couple copy-pasted outputs to help diagnose the problem?
1) From the window shell, could you run `python manage.py shell` and paste the output? Could you also please run `dir` and paste that output?
2) Could you also trigger that same error the full error you are getting, starting with "Traceback (most recent call last):" and ending where she shell prints out the next line of '>>>' ?
3) After you run that, within the same python shell, can you get the result of the python statement `import os; print os.environ['DJANGO_SETTINGS_MODULE']`

-- Andrew


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/98c81681-2008-44f3-9f27-937359768f49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Carl Meyer

unread,
Aug 11, 2015, 1:33:17 PM8/11/15
to django...@googlegroups.com
Hi,

On 08/11/2015 11:56 AM, jsa...@nvidia.com wrote:
> I'm studying the Django Project tutorial
> <https://docs.djangoproject.com/en/1.8/intro/tutorial01/> using Python
> 2.7 (because that's my department's current standard) and Django 1.8.2
> (because that's the current stable version).
>
> The tutorial says, "If you are still using Python 2.7, you will need to
> adjust the code samples slightly, as described in comments." What
> comments does it mean?

Python code comments, in the code samples that need adjusting. The first
example I see is in the `polls/models.py` code sample in this section:
https://docs.djangoproject.com/en/1.8/intro/tutorial01/#playing-with-the-api

See the "__unicode__ on Python 2" comments?

> The section "Playing with the API" instructs me to run the command:
>
> $ python manage.py shell
> or
>>>> import django
>>>> django.setup()
>
> Being a methodical sort, I tried both. The first works. The second gives
> me a bunch of errors, beginning with:
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "c:\Python27\lib\site-packages\django\__init__.py", line 17, in setup
> configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
>
> I think it's complaining about an incompatibility between Python 2.7 and
> Python 3.2.

Can't say without seeing more of the traceback. I doubt it though, since
this only executes code in Django itself, which is fully compatible with
both Python 2 and 3.

> I can evade the immediate problem by using "python manage.py shell"
> instead, but I expect further problems in short order, and I need to
> know how to fix them.
>
> So, where are these "comments" that tell me what to do? I don't see any
> in that part of the tutorial. I opened __init__.py, and there are no
> helpful comments there, either.

You don't need to adjust anything at all in Django itself to adapt to
Python 2; Django fully supports both 2 and 3. You just need to make sure
that the code you write yourself is Python 2 compatible. Where the
tutorial tells you to type some code in a file, it gives the Python 3
version, but has comments showing what needs to be adjusted for Python
2. There's nothing special you need to do until you get to those code
samples.

> I could figure this out myself, but I can't see debugging my way through
> the entire Django codebase. I'm being paid to write an application, not
> fix up Django.

You don't need to fix up Django.

> Either I'm missing the comments that the tutorial promised me, or it has
> left me in the lurch. Can someone explain, please?

Just continue with the tutorial, and read the code samples (including
comments) carefully, when you reach them.

Your issue when running `django.setup()` is probably entirely unrelated,
but in order to debug it we'd need to see the full traceback.

Carl

signature.asc
Reply all
Reply to author
Forward
0 new messages