1

I feel embarrassed just writing this question.

I've just installed python 3.4 and django 1.8

If I enter the python interpreter and enter:

>>> import django    
>>> print(django.get_version())

I get the answer 1.8 which is fine.

If I create a file django.py and in it enter:

import django
print(django.get_version())

Two things happen:

  1. When I run it I get the error

    Traceback (most recent call last):
      File "django.py", line 1, in <module>
        import django
      File "C:\Python_Mongo\django.py", line 2, in <module>
        print(django.get_version())
    AttributeError: 'module' object has no attribute 'get_version'
    
  2. It then creates a cache folder __pycache__ and if I simply start python interpreter and enter the command: import django it runs my django.py file! (any other command is fine).

Please help :(

0

2 Answers 2

6

You have a file django.py in your local directory, it masks the Django package. Rename it to something else.

See your traceback:

  File "django.py", line 1, in <module>
    import django

Note how the line import django is run from a file named django.py?

Sign up to request clarification or add additional context in comments.

2 Comments

ah, how embarrassing :), something I never thought of. btw, thanks for the edit, stackoverflow editor was driving me crazy with >>> removing itself and '2.' changing itself to '1.' on rendering
@Neil: > is syntax for a blockquote, and lists auto-number, but if the list sequence is broken the numbering starts at 1 again.
3

There is nothing surprising. If you invoke the import anything in a file named anything.py it will import itself.

Python's import path resolving searches current directory first and only after it looks for the module in system path.

1 Comment

thanks, I will remember that. Glad things are working for me now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.