Cannot Find Relative Import Within Module Python


I coudn’t find a solution to this problem online. I’m pretty sure this doesn’t have anything to do with relative imports but how pypi packages up a directory.

I have my directory structure like this:

├── types
│   ├──
│   ├──
│   ├──
│   ├──
│   ├──
│   ├──
│   └──

When I have the autocord directory inside my workspace and import it using import autocord everything works fine. However, once I published it to pypi, I get this error when I install it on another workspace.

Traceback (most recent call last):
  File "", line 1, in <module>
    import autocord
  File "/home/runner/autocord-test/venv/lib/python3.10/site-packages/autocord/", line 1, in <module>
    from .client import Client
  File "/home/runner/autocord-test/venv/lib/python3.10/site-packages/autocord/", line 9, in <module>
    from .types.Me import Me
ModuleNotFoundError: No module named 'autocord.types'

Here are the imports in

from .types.Me import Me
from .types.Member import Member
from .types.Message import Message
from .types.Deleted import Deleted
from .types.DmChannel import DmChannel

Here is

from setuptools import setup

with open("", encoding="utf-8") as f:
    readme =

    description="Discord API wrapper centered around automation",
    install_requires=["requests", "websocket_client"],

You can use pip install autocord to test it out for yourself.

Asked By: Walker



The error message you’re seeing indicates that Python is unable to find the "autocord.types" module. This is likely because of the way the package is structured and installed.

When you’re developing locally, your code is able to find the "types" package because it’s in the same directory as the "client" module that’s importing it. However, when you publish your package to PyPI and install it on another machine, Python is looking for the "types" package in the site-packages directory where your package is installed, but it’s unable to find it there.

To fix this, you can update your file to include the "types" package in the list of packages to be installed. You can do this by changing the packages line to:

packages=['autocord', 'autocord.types'],

This will tell setuptools to include the "types" package when installing your package.

After making this change, you can re-upload your package to PyPI and try installing it again. This should resolve the "ModuleNotFoundError" issue you’re seeing.

Answered By: Ajee
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.