
If you run python3 moduleA.p圓, moduleA is used as the main module, so using the absolute import looks like the right thing to do. Since the name of the main module is always "_main_", modules intended for use as the main module of a Python application must always use absolute imports. Note that relative imports are based on the name of the current module. This is what the docs say about intra-package-references: In addition to md-sabuj-sarker's answer, there is a really good example in the Python modules documentation. So instead of python mypackage/main.py, do it like this: python -m mypackage.main.
#NO MODULE NAMED LIBAVG MOD#
When it comes to running such module in development, Python can be executed with the -m option: -m mod : run library module as a script (terminates option list) Use relative imports, or imports with a package name on the beginning, because you need them like this when your app is installed. For installable Python apps in development Just use absolute imports (without the dot), and everything will be fine. How irritating that could be, the interpreter is right, you don't have such package installed. And again, trying to run this with python mypackage/main.py ends similar: ModuleNotFoundError: No module named 'mypackage' This is not very different from the relative import approach, as long as we do it from the context of mypackage. There is also a third possibility to import the common module: from mon import my_func The solution with an absolute import works well only when you create a typical Python app that is executed with a python command. We don't have such package, that's why it fails. The reason for that is simple: common is no longer a relative import, so Python tries to find it in installed packages. What's worse, when I opened a Python console, and tried to import the main module manually ( import mypackage.main), then I got the same error as above. ModuleNotFoundError: No module named 'common' If we now try to run this as before: python mypackage/main.py, then it works as expected! But, there's a caveat when you, like me, develop something that need to work as a standalone command line tool after installing it with pip. Let's follow the advise from the documentation and change the import statement to something different: from common import my_func So it looks like there's a problem only with running it directly. I was also able to import mypackage.main module in a Python console. and then ran it, it worked perfectly fine. When I installed my package with pip install. Since the name of the main module is always _main_, modules intended for use as the main module of a Python application must always use absolute imports. This is explained in the Python documentation: If we connect this information with the relative import we used, we get what we have in the error message: _main_.common. The cause of this problem is that the main.py was executed directly by python command, thus becoming the main module named _main_.

Unfortunately this gave the following error: ModuleNotFoundError: No module named '_main_.common' '_main_' is not a package I ran my application with simple: python mypackage/main.py. The first what I tried was a relative import: from. I tried different configurations that gave different errors, and I want to share with you with my observations and leave a quick note for future me as well. I tried importing a few functions in main.py from my common.py module. , but hey, who does this every time after changing something in one of the project files? I needed to run the whole thing through simple python mypackage/main.py.

I don't have problems running my project after installing it with pip install.

I'm developing a project which in fact is a Python package that can be installed through pip, but it also exposes a command line interface.
