Search code examples
pythonpackagingpypi

custom python package not importing


I wrote a package to collect data and now im trying to interact with it to store and serve said data, but it's not importing, even though I have installed it on pip. This is the PyPI page: https://pypi.org/project/smog-usage-stats/ and the repo is available here https://github.com/Stu-Gotz/smog_usage_stats sorry it's kind of against the minimally reproducable rule, but theres no short way to include an entire package.

pip list and import error message

from smog_usage_stats import UsageStatsLookup
import requests

if requests:
    print("yes")
else:
    print("no")

Testing with this prints "yes" (when the first line is commented out) so other packages are working correctly. I dotted all my t's and crossed all my i's and I have no prior experience writing Python packages, so I am not sure what I may have done wrong.

This is the project's file structure and below that I have pasted in my pyproject.toml

project file structure

# pyproject.toml

[build-system]
requires=["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "smog_usage_stats"
version = "1.0.4"
dependencies = [
    "beautifulsoup4",
    "pathlib",
    "psycopg==3.1.12",
    "psycopg-binary==3.1.12",
    "psycopg2==2.9.5",
    "python-dateutil",
    "python-dotenv",
    "requests",
    "soupsieve",
    "typing_extensions",
]
readme = "README.md"
authors = [{ name = "stu.gotz.dev", email = "[email protected]" }]
license = { file = "LICENSE" }
classifiers = [
    "License :: OSI Approved :: MIT License",
    "Intended Audience :: Developers",
    "Programming Language :: Python :: 3",
    "Operating System :: OS Independent"
]
keywords = ["pokemon", "usage", "pokemon showdown", "smogon"]
requires-python = ">=3.7"

[project.optional-dependencies]
dev = ["black", "bumpver", "isort", "pip-tools", "pytest"]

[tool.bumpver]
current_version = "1.0.4"
version_pattern = "MAJOR.MINOR.PATCH"
commit_message = "bump version {old_version} -> {new_version}"
commit = true
tag = true
push = true

[tool.bumpver.file_patterns]
"pyproject.toml" = [
    'current_version = "{version}"',
    'version = "{version}"'
]
"src/smog_usage_stats/__init__.py" = ["{version}"]
"setup.py" = [
    "{version}",
    "{pep440_version}",
]
"README.md" = [
    "{version}",
    "{pep440_version}",
]


and here is the packages smog_usage_stats/src/smog_usage_stats/__init__.py contents:

import sys
import os

__version__ = "1.0.4"
__author__ = ""

# Get the parent directory
parent_dir = os.path.dirname(os.path.realpath(__file__))

# Add the parent directory to sys.path
sys.path.append(parent_dir)

EDIT

I took some people's advice from comments and replies and it just seemed to break it worse, so I am not sure what is happening, but it does import, however when I run it from the "play button" in VS Code, I get ModuleNotFoundError, but when i run py script.py in the terminal (venv is active always) it gives me a printout and no ModuleNotFoundError.

(venv) PS C:\dev\gssp> & c:/dev/gssp/.venv/Scripts/python.exe c:/dev/gssp/data_collection.py
Traceback (most recent call last):
  File "c:\dev\gssp\data_collection.py", line 1, in <module>
    from smog_usage_stats import Search
ModuleNotFoundError: No module named 'smog_usage_stats'
(venv) PS C:\dev\gssp> py data_collection.py
yes

and as another reply suggested, this is my setup.py file:

from setuptools import setup

setup()

EDIT 2:

I am a moron, it wasn't working because it was pointing to the wrong venv.

from smog_usage_stats import Usage


Solution

  • Your Project Structure seems to be incorrect. Makes sure to follow the project structure.

    smog_usage_stats/
    ├── src/
    │   └── smog_usage_stats/
    │       ├── __init__.py
    │       ├── (Other Package Files)
    ├── pyproject.toml
    └── setup.py
    

    Next Step is to ensure setup.py is configured correctly along with all dependences and proper python version.

    Finally you can try to install it using pip install . command. Please share more error details if you still encounter the issue.