1

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

4
  • Just adding a note that I tried installing your package on google colab and was able to run your code snippet without an issue. Maybe there is some residual package files from when you were developing your package? And it's interfering somehow? Commented Jul 31, 2024 at 23:52
  • Please do not upload images of code/data/errors. Your console output and directory trees would be better included as code-formatted text. Commented Aug 1, 2024 at 5:50
  • Using sys.path.append in the init file is unnecessary, and might well be the source for problems. Other than that, your package looks good. Maybe a problem with venv or multiple python versions? Commented Aug 1, 2024 at 6:05
  • I added that in because I was having issues importing the modules around the other package files, and that seemed to cure it (after a rather extensive google scouring), because I also have a blank init.py in the package root folder. (Im not sure if its necessary but when I was making it ages ago that was guidance I found) Commented Aug 1, 2024 at 19:15

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.