How to import a python module with the same file name that is already imported?

I have a problem with importing a python module. I installed django (it can be any other module). I am using this module:

from django.template import Context # other imports # use Context 

I have a folder in my application named "utilities". In this folder I created the file "django.py" - this file contains some functions for working with django. Therefore, I import django into my module and get an error: File "... \ utilities \ django.py", line 1, from django.template import Context ImportError: there is no module named template

+4
source share
2 answers

You need to enable absolute import at the top of the file:

 from __future__ import absolute_import 

Then you will need to convert the import into the module into absolute or relative import as needed.

+8
source

Use namespaces, create __init__.py in your utilities directory so that it is a package, and then import utilities.django . By the way, it is bad practice to name your module in the same way as the std libraries, or the name of the package that you know you are going to use. You never know when you are going to confuse yourself which module to use.

0
source

Source: https://habr.com/ru/post/1338820/


All Articles