0

I have the following folder structure:

enter image description here

I want to access A.py from D.py (3 levels up). I have added an init.py to all the directories in this project to initialize them as packages. When I try the following in D.py:

from test1 import A

A.hi()

I get the following error:

ModuleNotFoundError: No module named 'test1'

I am using Windows 10, Visual Studio Code and python 3.9.6 and I even added all the test1, test2, test3 folders to path variable. I think it's worth mentioning that Visual Studio does seem to recognize these folders as packages as it shows them in the dropdown list when typing module names at import.

enter image description here

EDIT: I have managed to access a Z.py created in test1 from test3 D.py with this:

import sys
import os
sys.path.append(os.path.abspath("C:\\Users\\myusername\\Desktop\\testdbconnection"))

from test1 import Z
Z.hi()

This folder was already in Windows Path, I added it myself. Why did Python not recognize it before?

Also, I can't access anything else than what is in test1 with this.

  • If i want to access A.py and B.py i need to append to path C:\Users\myusername\Desktop,

  • if i want to access test2 C.py i need to append to path C:\Users\myusername\Desktop\testdbconnection\test1

  • and so on...

Sederfo
  • 177
  • 1
  • 11
  • 1
    The `test1` package needs to be installed in the search path in order to be able to import it. – mkrieger1 Nov 17 '21 at 08:19
  • Does this answer your question? [How to locally develop a python package?](https://stackoverflow.com/questions/52248505/how-to-locally-develop-a-python-package) – mkrieger1 Nov 17 '21 at 08:21
  • 1
    Take a look at [this](https://stackoverflow.com/questions/24868733/how-to-access-a-module-from-outside-your-file-folder-in-python) – Alain Bianchini Nov 17 '21 at 08:22
  • How are you running this code? ``test1`` is only available as a package when explicitly added to the search path *or* ``D.py`` is run as ``test1.test2.test3.D`` (i.e. as part of the package). – MisterMiyagi Nov 17 '21 at 08:23
  • Hey, no, https://stackoverflow.com/questions/52248505/how-to-locally-develop-a-python-package did not answer my question. I am just trying to access a script in the root directory for one of my bigger projects. It is a mailing script that i want to reuse. This, however, came a bit closer to solving my issue: https://stackoverflow.com/questions/24868733/how-to-access-a-module-from-outside-your-file-folder-in-python – Sederfo Nov 17 '21 at 09:03

1 Answers1

0

This solved it for me.

from pathlib import Path
desktop = Path(__file__).absolute().parent.parent.parent.parent.parent
sys.path.append(str(desktop))

This appends the containing folder of testdbconnection which is Desktop in my case to the Path variable. The first .parent returns the directory of the file and the remaining 4 .parent return the Desktop folder. I think it is best practice not to include Desktop in the Path variable but to create another parent folder to contain testdbconnection. Finally, convert the Path object to str and append it to sys.path.

Using the absolute path of the file ensures the path is the same even if I run my python script from test3 (python D.py) or from test2 (python test3/D.py) or from anywhere else really.

Sederfo
  • 177
  • 1
  • 11