pythonsetuptoolspython-packaging

Why is my locally installed Python package not found when installed as an editable package?


I am using Python 3.10.7 64-bit in VSCode. I have a Python package "pxml" built using setuptools. It is in a folder with an empty __init.py__ and a pyproject.toml containing:

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
[project]
name = "pxml"
version = "0.0.1"
dependencies = []

When installed normally with python -m pip install path/to/folder, an import pxml works just fine. However, when installed with python -m pip install -e path/to/folder, I get the error "Import 'pxml' cannot be resolved."

python -m pip list shows the package installed and when I run a simple test file to access one of pxml's methods it runs just fine. Making sure that I am using the same python version in the terminal, python -c "import pxml" reports no errors.

One thing that does show up as an autocomplete option when I type import pxml is __editable___pxml_0_0_1_finder, not sure if that is a clue.

The directory structure is:

 - src
     + XML
         * pxml.py
         * __init__.py 
         * pyproject.toml
     + test.py

I am using test.py to import pxml.

Any ideas?


Solution

  • For the package structure shown in your question, this is import statement is incorrect:

    import pxml
    

    pxml is not a top-level import, it is a submodule of a package named XML.

    If python -c "import pxml" works that is only by accident and you happen to have pxml visible on sys.path for some reason - perhaps the subdirectory XML is your current working directory, and it is resolved that way because the first element of sys.path is the empty string.

    Instead, the import should be one of:

    import XML.pxml
    

    or

    from XML import pxml