Cannot Find Relative Import Within Module Python
Question:
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:
autocord
├── types
│ ├── Message.py
│ ├── Attachment.py
│ ├── Deleted.py
│ ├── DmChannel.py
│ ├── Embed.py
│ ├── Me.py
│ └── Member.py
├── __init__.py
├── client.py
├── errors.py
└── op.py
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 "main.py", line 1, in <module>
import autocord
File "/home/runner/autocord-test/venv/lib/python3.10/site-packages/autocord/__init__.py", line 1, in <module>
from .client import Client
File "/home/runner/autocord-test/venv/lib/python3.10/site-packages/autocord/client.py", line 9, in <module>
from .types.Me import Me
ModuleNotFoundError: No module named 'autocord.types'
Here are the imports in client.py:
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 setup.py:
from setuptools import setup
with open("README.md", encoding="utf-8") as f:
readme = f.read()
setup(
name="autocord",
version="1.4.8",
author="walker",
description="Discord API wrapper centered around automation",
long_description=readme,
long_description_content_type="text/markdown",
packages=['autocord'],
url="https://github.com/wa1ker38552/autocord",
install_requires=["requests", "websocket_client"],
python_requires=">=3.7",
py_modules=["autocord"]
)
You can use pip install autocord
to test it out for yourself.
Answers:
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 setup.py 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.
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:
autocord
├── types
│ ├── Message.py
│ ├── Attachment.py
│ ├── Deleted.py
│ ├── DmChannel.py
│ ├── Embed.py
│ ├── Me.py
│ └── Member.py
├── __init__.py
├── client.py
├── errors.py
└── op.py
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 "main.py", line 1, in <module>
import autocord
File "/home/runner/autocord-test/venv/lib/python3.10/site-packages/autocord/__init__.py", line 1, in <module>
from .client import Client
File "/home/runner/autocord-test/venv/lib/python3.10/site-packages/autocord/client.py", line 9, in <module>
from .types.Me import Me
ModuleNotFoundError: No module named 'autocord.types'
Here are the imports in client.py:
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 setup.py:
from setuptools import setup
with open("README.md", encoding="utf-8") as f:
readme = f.read()
setup(
name="autocord",
version="1.4.8",
author="walker",
description="Discord API wrapper centered around automation",
long_description=readme,
long_description_content_type="text/markdown",
packages=['autocord'],
url="https://github.com/wa1ker38552/autocord",
install_requires=["requests", "websocket_client"],
python_requires=">=3.7",
py_modules=["autocord"]
)
You can use pip install autocord
to test it out for yourself.
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 setup.py 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.