[Python] When importing python library, package is sometimes found, at other times not found...

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following project structure:

    [​IMG]

    My problem is that when I import mattlibrary inside testme.ipynb the module is not found. However, inside test.ipynb it imports just fine. Now, I understand why the latter works, because the cwd finds mattlibrary. However, I installed the package mattlibrary as an editable package in my virtual environment.

    sys.executable produces the following:

    ['/usr/lib/python313.zip',
    '/usr/lib/python3.13',
    '/usr/lib/python3.13/lib-dynload',
    '',
    '/home/matt/development/python_development/.venv/lib/python3.13/site-packages']


    And here the files in my site-packages directory within the virtual environment:

    -rw-r--r-- 2 matt matt 93 Jun 18 18:12 __editable__.mattlibrary-0.1.0.pth
    -rw-r--r-- 2 matt matt 3918 Jun 18 18:12 __editable___mattlibrary_0_1_0_finder.py


    I spent the entire day tinkering with different ideas, but still struggle to import the library from any other location other than above the mattlibrary directory structure. I strictly do not want to use sys.path.insert each time I want to import my library.

    Edit: I can successfully import mattlibrary from the python interpreter within my terminal inside the virtual environment regardless of which directory I current am in, so I am sure the package installed successfully.

    The entire point why I installed the package in the virtual environment is so that it can be imported regardless where the file resides within which I import the library. Is my understanding correct that the culprit is the ''in sys.path? Is this where the import aborts because it cannot locate mattlibrary under the cwd? Then why even add the last path '/home/matt/development/python_development/.venv/lib/python3.13/site-packages' if the locator never reaches it and searches that directory for my package?

    What am I doing wrong? This is driving me insane...

    Continue reading...

Compartilhe esta Página