16

I have a custom module in one of the directories in my PYTHONPATH with the same name as one of the standard library modules, so that when I import module_name, that module gets loaded. If I want to use the original standard library module, is there any way to force Python to import from the standard library rather than from the PYTHONPATH directory, short of renaming the custom module and changing every reference to point to the new name?

jrdioko
  • 32,230
  • 28
  • 81
  • 120
  • What's so bad about renaming your module? – S.Lott Jun 01 '10 at 18:06
  • It would just involve making changes to dozens of files. It sounds like it's the best solution though. – jrdioko Jun 01 '10 at 18:46
  • You have `grep`, so it shouldn't be too hard to find each one and fix them. – S.Lott Jun 01 '10 at 21:15
  • 5
    Please also add to the equation that you might have a module in your program, which in a later version conflicts with a new standard module. With that in mind I think it´s a valid question to write future proof code. – Deleted Apr 23 '12 at 12:16
  • possible duplicate of [How to access a standard-library module in Python when there is a local module with the same name?](http://stackoverflow.com/questions/1900189/how-to-access-a-standard-library-module-in-python-when-there-is-a-local-module-w) – Cristian Ciupitu Feb 23 '15 at 23:36
  • @CristianCiupitu: modules in `PYTHONPATH` are *not local*. Absolute import won't help here. Don't shadow stdlib modules otherwise you'll break any code that doesn't expect your custom module. If you must then you could move the corresponding directory to the beginning of `sys.path` list in a `*.pth` file or in your code but the correct solution is to rename the module [as @S.Lott suggested](http://stackoverflow.com/q/2952045/#comment3009203_2952045) e.g., put it in your custom namespace: `import jrdioko.module_name as module_name`. – jfs Mar 22 '15 at 16:06
  • @J.F.Sebastian, I think you're replying to the wrong guy. All I was saying is that this question is a duplicate of another one. – Cristian Ciupitu Mar 22 '15 at 17:26
  • @CristianCiupitu my comment explains why the question is not a duplicate. – jfs Mar 22 '15 at 17:41
  • @J.F.Sebastian, yet they have the same accepted answer. If you think you have a better answer, why not add it to one of the questions? – Cristian Ciupitu Mar 23 '15 at 13:04
  • @CristianCiupitu: yes. The part about the absolute import in the current accepted answer is wrong. It won't be the first accepted answer that is wrong. I saw wrong accepted answers with 100+ upvotes. My answer is *rename the module* that is already stated in the accepted answer. – jfs Mar 23 '15 at 19:39

3 Answers3

12

The ideal solution would be to rename your module to something not in the standard library.

You can also switch absolute imports on if you're on Python 2.5+:

from __future__ import absolute_import
Luper Rouch
  • 9,304
  • 7
  • 42
  • 56
12

Don't.

If you have accidentally chosen a standard library module name, change your module name to end the conflict.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • This is probably the best solution, both for not causing confusion in visiting coders, and the fact that the behaviour importing a module that wasn't already a relative import won't change with absolute imports turned on. – Matt Joiner Mar 18 '11 at 13:44
  • 2
    This is the obvious answer, but I'm a little miffed that the standard library is allowed to hog the namespace like that by convention. It's huge! And it contains many generic code-related words, like both "types" AND "typing". And it's growing all the time. – mszegedy Jun 09 '19 at 06:31
7

You can select the module you want to import with the imp module:

import imp
mymodule = imp.load_module(name, file, pathname, description) 
joaquin
  • 82,968
  • 29
  • 138
  • 152