When I use relative imports, my code runs correctly but pycharm moans. In sum.py, the import line is underlined in red and I have no completion.
If I try to switch to use absolute imports by adding "app." everywhere, pycharm is happy but the code doesn't run anymore. I get: "ModuleNotFoundError: No module named 'app'"
Here is the tree structure of my project. It's the content of my test_import folder:
.
-- app
| -- main.py
| -- mul.py
| -- folder
|-- sum.py
main.py:
from folder.sum import sum, mul_import
from mul import mul
if __name__ == '__main__':
a=1
b=2
print("sum ", sum(a, b))
print("mul ", mul(a, b))
print("mul import ", mul_import(a, b))
mul.py:
def mul(a, b):
return a * b
sum.py:
from mul import mul
def sum(a, b):
return a + b
def mul_import(a, b):
return mul(a, b)
Is it possible to use "app." everywhere to have the completion but make the code work?
In pycharm I have to run with the green arrow and not in command line with "python main.py" or "python app/main.py". This works because the project directory is added to the pythonpath (in the configuration of the run).
For docker I had to add the project directory to the pythonpath.