Search code examples
python-3.xsetuptoolsimporterrortoml

Python can't import the module I made


I created a python class file that I wanted to modulize for windows machines, so I have been following this tutorial.

Structure:

projfolder/
    mymodule/
        mymodule.py
        __init__.py
        __main__.py
    tests/
    LICENSE
    MANIFEST.in
    README.md
    pyproject.toml

pyproject.toml

[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "mymodule"
version = "1.0"
authors = [
    {name="author", email="author@gmail.com"}
]
description = "description"
readme = "README.md"
license = {file="LICENSE"}
requires-python = ">=3.6"
classifiers = [
     'Development Status :: status',
     'Intended Audience :: for Author',
     'Operating System :: OS Independent',
     'Programming Language :: Python3',
     'Topic :: topic :: topic',
]

as per the tutorial, after I have configured my MANIFEST, README, and LICENSE, I open a command prompt and run

py -m pip install --upgrade build
py -m build
pip install ./dist/mymodule-1.0.tar.gz

which outputs no errors or warnings.

When I run pip show mymodule, it can find it, shows me the path, etc. When I travel to the path in a file explorer, it shows me these files in the mymodule folder: __init__.py, __main__.py, mymodule.py. However, when I run

python
import mymodule
mymodule.myclass

it can't find "myclass" associated with it. What are the potential reasons why this is happening and how can it be fixed?

Thank you!


Solution

  • Doi! You need to use __init__.py to import the module into the namespace! (Everything I've ever read has left it empty... I just opened up another module and explored it to figure that out)

    __init__.py:

    from . import mymodule