Search code examples
pythonpyvenv

Python with virtual evironment not working with my own package


My project looks like this:

src
 |--annotation_correction
 |--|--suggestion.py
 |--|--error.py
 |--|--...
venv
 |--...

I installed my package, using pip install -e . while in the main folder. When I type pip freeze, my package "annotation" is in the list and VSCode also seems to recognize it as an installed package.

The problem is that when I run suggestion.py while trying to import e.g. from error.py with from annotation_correction.error import Error, ErrorType, I still get the error:
ModuleNotFoundError: No module named 'annotation_correction.error'; 'annotation_correction' is not a package

All this while using the interpreter that is running in the venv.

My setup.py just calls setup() and my setup.cfg looks like this:

...
packages = 
    annotation_correction
...
package_dir =
    =src

Solution

  • To make my comment an answer:

    If you have a module in a package that imports other modules from the same package with from package import module or from . import module or similar, then you will need to use the -m switch:

    Search sys.path for the named module and execute its contents as the __main__ module.

    Otherwise, Python will have set up sys.path so the directory of the script that is run is in it, which will naturally wreak havoc with imports (since the package is "invisible").

    This stands whether or not you've installed a package (and indeed you don't necessarily need to pip install -e the same package you're working on) or not.

    The other option is to have a standalone script outside the package that acts as the entry point and does e.g. from annotation_correction.suggestion import main (since from that script's point of view, the package is there and all is fine -- and if the package is installed, -e or no -e, the script doesn't even need to be next to the package directory, since the package has been installed and as such on the search path), but that's unnecessary.

    Visual Studio Code apparently supports the same thing with a "module" run configuration.