Python 3 import conflicts with internal "parser"

python cannot import module
python 3 import from parent directory
python import module from directory
python relative import
python absolute import
python import from parent directory
python unable to import
python cannot find module
.
├── gen.py
├── lexer
│   ├── engine.py
|   └── ...
└── parser
    ├── engine.py
    └── ...

I'm writing my compiler project, but now i'm stuck with python import conflicts. In gen.py, I want to import some function for code generation, like

import lexer.engine   # OK
import parser.engine  # ModuleNotFoundError: No module named 'parser.engine'; 'parser' is not a package

After some investigation, I've learned that "parser" is reserved for python internal parser. But I cannot change the directory name "parser", since it has been used everywhere.

How can I solve the problem?

To be accepted as a Python package, a directory must have an __init__.py file. Variables declared in this file can be access as if the package is a regular module. It can also be empty.

tl;dr: Add an empty __init__.py file to the directory and it should work.

Issue 34414: Absolute imports conflict with local files, THE ISSUE --------- Absolute imports seem to conflict with local files (not in spam is imported, the interpreter first searches for a built-in module with that name. Running "python3.6 a/foo.py" therefore adds "a" to the start of  The import system¶ Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way. Functions such as importlib.import_module() and built-in __import__() can also be used to invoke the import machinery.

python have a module parser , and you want to import engine that it doesn't have, if you wiil try to

import parser
print(dir(parser))
print(parser.__file__)

and you will see that parser module is not your parser module.

Simple rename your parser folder and every thing will be all right

Update

can you try to import with from for example:

from parser import engine

Traps for the Unwary in Python's Import System, This particular trap applies to 2.x releases, as well as 3.x releases up to and While Python 3.3+ is able to import the submodule without any problems:. You may use sys.implementation.cache_tag directly starting in Python 3.3. The following functions help interact with the import system’s internal locking mechanism. Locking semantics of imports are an implementation detail which may vary from release to release.

Assuming your project is actually contained in a project directory like so, ...

my_package
| 
├── gen.py
├── lexer
│   ├── __init__.py
│   ├── engine.py
|   └── ...
├── parser
|   ├── __init__.py
|   ├── engine.py
|   └── ...
├── tests  
|   ├── test_thingy.py

in gen.py:

import my_package.lexer.engine
import my_package.parser.engine

in the parent directory of my_package,you can run python -m my_package.gen. This should run exactly as expected without the name conflicts. With similar import statements in your tests, if you run your test modules in the same way, it should work just fine.

I tested this with the following. In E/work/temp/ I have a directory called my_package. It has the following structure.

my_package
    | 
    ├── __init__.py  # needed in python 2, but not 3
    ├── import_test_b.py
    ├── parser
    |   ├── __init__.py
    |   └── import_test_a.py
    └── tests  
        ├── __init__.py  # needed in python 2 but not python 3
        └── test_imports.py

import_test_a:

def test(num):
    return num+3

import_test_b:

from my_package.parser.import_test_a import tst

print(tst(4))

test_imports.py:

from my_package.parser.import_test_a import tst

import unittest

class TestTst(unittest.TestCase):
    def test_one(self):
        self.assertEqual(tst(4), 7)

if __name__ == '__main__':
    unittest.main()

in E/work/temp:

run: python -m my_package.import_test_b - output = 7

run: python -m my_package.tests.test_imports output: .

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Python Module Name Clashes (evanjones.ca), Python Module Name Clashes that conflicts with a built-in module, and then it becomes difficult to import that The problem here is that imports are by default relative to the module (In Python versions <= 2.7.3; see below). from fast_rcnn.nms_wrapper import nms File 'D:Program Files (x86)PortableGitbinFaster-RCNN_TFtools.libfast_ rcnnnms_wrapper.py', line 9, in from nms.gpu_nms import gpu_nms ImportError: Module use of python27.dll conflicts with this version of Python.

types.py conflicts with built in python types module · Issue #4491 , types.py conflicts with built in python types module #4491 in <module> import re File "/Users/dave/matrix/synapse/venv/lib/python3.7/re.py",  This function is a deprecated alias of PyImport_ImportModule(). Changed in version 3.3: This function used to fail immediately when the import lock was held by another thread. In Python 3.3 though, the locking scheme switched to per-module locks for most purposes, so this function’s special behaviour isn’t needed anymore.

The 10 Most Common Mistakes That Python Developers Make, In Python, class variables are internally handled as dictionaries and follow what is Common Mistake #3: Specifying parameters incorrectly for an exception block This can lead to gnarly problems, such as importing another library which in  A Python file called hello.py has the module name of hello that can be imported into other Python files or used on the Python command line interpreter. You can learn about creating your own modules by reading How To Write Modules in Python 3 .

Modules and packages: how to create a Python project, A module is a Python file which contains code, saved with the .py extension. avoid name clashes across different modules: I can easily import the module One dot means the current package, two dots is up one level, three  A relative import specifies the resource to be imported relative to the current location—that is, the location where the import statement is. There are two types of relative imports: implicit and explicit. Implicit relative imports have been deprecated in Python 3, so I won’t be covering them here. Syntax and Practical Examples

Comments
  • Do you have an __init__.py file in the parser directory?
  • you should be able to change your name easily in either an IDE (like pycharm or other. they should have some sort of function allowing renaming of folders in your project) or using sed in linux or a linux-like environment (on windows, if you download git, it comes with a simulated bash shell)
  • @e.s. "parser" is used not only in C++ files, but also many hard-code python generator and even CMakeLists. And I hate the idea to change the folder name.
  • @Neo not yet, I'm not an expert in python.
  • As @Neo says, create an empty parser/__init__.py file (that's two underscores before, and two underscores after init!). This way your parser will be recognized as a module, and import parser will be loading it.
  • Renaming is not an option for me, since it will break many dependencies.
  • @MikeDog try to import with from i update my answer
  • no use... you may try it before updating your answer.
  • i did't it like in your example, and for me work from parser import engine, i create folders and files that you have