0

I've added one folder into my project called folderb as follows:

mainproject
           foldera
                  __init__.py (empty)
                  filea.py    (module)
           folderb
                  __init__.py (empty)
                  fileb.py    (module)

My project is located under:

/root/Documents/Repositories/mainproject/

Inside filea.py module i want to use module's functions of fileb.py from folderb therefore i import as follows:

from folderb.fileb import myfunctionb

Nevertheless i am getting this:

Exception has occurred: ModuleNotFoundError
No module named 'folderb'

What am i doing wrong?

Arie
  • 3,041
  • 7
  • 32
  • 63
  • 1
    do you have any folders above mainproject? – Karol Milewczyk Sep 07 '22 at 13:39
  • mainproject is the python project main folder. I open it in vscode and work on it. In vscode when i do from folderb.fileb import myfunctionb then it's even highlited correctly by the tool. – Arie Sep 07 '22 at 13:46
  • @just print out the sys.path (env variable ) and check root path included or not, i think it might spell error – Joseph Sep 07 '22 at 13:52
  • @JobishJose it prints out me the entire path like /usr/bin/python3.8 /root/Documents/Repositories/mainproject/foldera/filea.py – Arie Sep 07 '22 at 13:58
  • This should just work if you **import** `folderb.filea` from a script inside the `mainproject` directory. However, the error you are getting is expected when you would try tro **run** `foldera/filea.py`, because in that case the `folderb` module is not in the Python path. The [Python import system is explained here](https://docs.python.org/3/reference/import.html) and I'm 100% sure this question has been asked and answered many times on SO as well. – wovano Sep 07 '22 at 14:18
  • Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – wovano Sep 07 '22 at 14:19
  • @wovano u said "because in that case the folderb module is not in the Python path" so how can i add it there so then i can use it in fileA? – Arie Sep 07 '22 at 14:27
  • You could add it to the Python path (`sys.path` in Python, or I think PYTHONPATH on the command-line) but it's not the nicest solution. I suspect this is an XY-problem. What are you trying to achieve? Since `foldera` is a Python package, I don't understand why you want to execute the file `foldera/filea.py`. If it's a module, there's generally no need to run/execute it. NB: [this answer](https://stackoverflow.com/a/14132912/10669875) contains much useful information about this problem. – wovano Sep 07 '22 at 14:43

1 Answers1

0

The issue is set up of sys.path with two different way of execution of the script. Go to mainproject folder. This would work:

python -m foldera.filea

and this would not

python foldera/filea.py

You can see why it is so by adding this to beginning of foldera/filea.py before any other import:

import sys
print(sys.path)

Invocation using the path will add /root/Documents/Repositories/mainproject/foldera to the path where Python interpreter looks for modules. Invocation using the package and module name (with -m option) would add /root/Documents/Repositories/mainproject/.

You can make the path variant working by augmenting the sys.path, either with

PYTHONPATH=. python foldera/filea.py

or by adding this ugly code to the beginning of filea.py:

current = os.path.dirname(os.path.realpath(__file__))
parent = os.path.dirname(current)
sys.path.insert(0, parent)

Don't do that, use the first option. More information:
PEP 338 – Executing modules as scripts
Python documentation - Command line and environment, option -m

Roman Pavelka
  • 3,736
  • 2
  • 11
  • 28
  • i understand but how for instance in my vscode tool or any other to be able to debug if this error happens when trying to debug ? :/ – Arie Sep 07 '22 at 14:39
  • I don't use vscode, but you should be able to customize what is executed when you press Run or Debug... – Roman Pavelka Sep 07 '22 at 14:47