Search code examples
pythonmodulepip

Importing local packages in python


Python: 3.10.4 Windows: 10

Ok so I have been struggling with this for while now.

I've been testing module imports in python. I'm using https://github.com/tomchen/example_pypi_package as a (very useful) example. I run the following:

git clone https://github.com/tomchen/example_pypi_package
cd example_pypi_package
python -m setup.py develop
pip install -e .

Now, I expect to be able to import example_pypi_package in the REPL of that environment.

import example_pypi_package.module1

Yet I get ModuleNotFoundError.

How? I checked that my sys.path is correct and in my Lib\site-packages there is an example_pypi_package.egg-info file. I checked that my sys.execuatble is the correct environment and installed it with pip within that environment. The package shows up in pip list. What am I missing? I can't import the package not even import subpackages of that package.


Solution

  • The importable package (directory with __init__.py) for this distribution package is examplepy. See https://github.com/tomchen/example_pypi_package/tree/main/src/examplepy

    So try import examplepy or

    import examplepy.module1
    

    PS. The name of a distribution/package/repository could be completely different from importable names. See my examples at https://stackoverflow.com/a/54599368/7976758