Issue
When I run my code in a virtual environment I am getting a different output than if I run the code in a container outside of a virtual environment. Same file structure, same code in AB.py, and same import statements in the init.py and AB.py files. See the image for more details. Very basic but I would just like to no why python does not recognize one of my modules when the parent container is located in a virtual environment. Thank you!
Container A
>AB.py
>>Package_A
>>>>__init__.py,
>>>>Module_A.py,
>>>>Module_B.py,
>>>>Module_C.py
Virtual Environment Container A
>AB.py
>>Package_A
>>>>__init__.py,
>>>>Module_A.py,
>>>>Module_B.py,
>>>>Module_B.py
[See image here] : https://i.stack.imgur.com/RJGpk.png
## Package AB.py
from Package_A import Mod_A, Mod_B, Mod_C
Module_A.Func_A()
Module_B.Func_B()
Module_C.Func_C()
## __init__.py
from a import func_a
from b import func_b
## Module_A.py
def Func_A():
print("how")
## Module_B.py
def Func_B():
print("are")
## Module_C.py
def Func_C():
print("you")
Running AB.py in container Returns
how
are
you
Running AB.py in VIRTUAL ENVIRONMENT Returns
ModuleNotFoundError: No module named 'a'
Solution
Found a solution, I needed to change my import lin in my init.py file. I changed it from:
from a import func_a
from b import func_b
To
from .a import func_a
from .b import func_b
This was the case for both venv and non venv. I did not specify the parent folder.
Answered By - Joe L. Answer Checked By - David Goodson (WPSolving Volunteer)