[Django]-Importing a module which have the same name with a system module


See the PEP on absolute and relative imports for the semantics. You probably want

from .utils.custom_modules import some_function

if you’re in a file at the top level of your package.

Edit: This can only be done from inside a package. This is for a good reason — if you’re importing something that is part of your project, then you’re already treating it like a Python package, and you should actually make it one. You do this by adding an __init__.py file to project directory.

Edit 2: You’ve completely changed the question. It may be possible to work around the problem, but the correct thing to do is not refer to your packed the same way as a builtin package. You either need to rename utils, or make it a subpackage of another package, so you can refer to it by a non-conflicting name (like from mydjangoapp.utils.custom_modules import some_function).



I’m not going to bother telling you to try and make sure you don’t name your own modules after the stdlib modules;

If you want to leave the name like this, you’ll have to use something like this in everything that imports your own utils module:

import sys, imp

utils = sys.modules.get('utils')
if not utils: utils = imp.load_module('utils',*imp.find_module('utils/utils'))

However, unless there’s a lot of stuff that you’d have to change after renaming it, I’d suggest you rename it.


Leave a comment