2
Project
    __init__.py
    main.py
    parser.py

From within main, how do I import my my parser module given that parser is already a built-in?

Current, renaming the user-defined module is the worst-case solution.

I've tried:

import parser
from parser import  TextFileParser

from parser import TextFileParser

from . import parser

from .parser import TextFileParser

from __future__ import absolute_import
from . import parser as myParser

as well as a few other combinations.

I've read the answer to this related question, but my understanding is that 3.5 uses absolute imports by default. I also read this article, but it didn't seem to apply.

I'm also happy aliasing the class I need, if that is possible. i.e.

TextParseClass = from parser import TextParser

Community
  • 1
  • 1
Brian H.
  • 2,092
  • 19
  • 39
  • There is some automation code that depends on this being the name. Also, I think the issue will come up again, such as the Python standard library expanding to include a new built-in that overwrites a module I've made – Brian H. Mar 01 '16 at 22:36
  • @Jordy Cuan Petrucci Ugh, I feel embarrassed. So simple not sure how I overlooked it. Thanks. Make it an answer if you want – Brian H. Mar 01 '16 at 22:47
  • `__import__("Project.parser")` will return your module as well. So `foo = __import__("Project.parser")` should work although it's probably not the best way to handle it. – kylieCatt Mar 01 '16 at 22:57

1 Answers1

2

Use this: from Project import parser

Jordy Cuan
  • 467
  • 6
  • 13