Here's a link to the problematic code in Github: https://github.com/ddmee/moduleerrorpython
I'm using Python 3.6. I have a set of directories that look like this.
root/
....lib/
........__init__.py
........shared/
..............___init__.py
..............file1.py
..............file2.py
From the root directory, when I try to run file1.py with the interpreter (python lib/shared/file1.py), I get this error:
Traceback (most recent call last):
File "lib/shared/file1.py", line 1, in <module>
import lib.shared.file2 as file2
ModuleNotFoundError: No module named 'lib.shared'
However, if I start the interpreter and just import file1, there is no such error. E.g.
> python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import lib.shared.file1
>>> import lib.shared.file1 as file1
>>> dir(file1)
['Hello', 'Server', 'TrioII', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']