Search code examples
pythonpython-3.xpypi

PyPI package can't find functions in said package


I made a python package called 'pyutils39'. So far it has functions consoleask(question) and reverse(text). It seemed to upload OK and has a page on pypi. (https://pypi.org/project/pyutils39).

My Issue

For testing, I installed my package with .\pip install pyutils39. It seemed to install successfully, and going into \Lib\site-packages\pyutils39 does have the file. However, when I run my testing file, it crashes with

Traceback (most recent call last):
  File "c:\Users\<MY USER>\AppData\Local\Programs\Python\Python39\Scripts\test.py", line 2, in <module>
    x = pyutils39.consoleask("Do you want to reverse hello?")
AttributeError: module 'pyutils39' has no attribute 'consoleask'

Completely removing all traces of the package makes it error with 'Module Not Found' (expected). Putting the package file in the same folder as the testing file works just fine.

What I have Tried

I have reinstalled my package, remade the testing file, and searched both google and stack overflow for this issue when nothing worked.

Code

Main file:

def reverse(data):
    x = str(data)
    datalen = len(data)
    x = x[datalen::-1]
    return x
def consoleask(question):
    while True:
        print(str(question)+' (Y/N)')
        x = input()
        if x.lower().startswith('y'):
            return True
            break
        elif x.lower().startswith('n'):
            return False
            break

Testing File

import pyutils39
x = pyutils39.consoleask("Do you want to reverse hello?")
if x == True:
    print(pyutils39.reverse("hello"))

Setup.py

import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setuptools.setup(
    name="pyutils39",
    version="0.1.3",
    author="Enderbyte09",
    author_email="[email protected]",
    description="A utilities package",
    long_description=long_description,
    py_modules=['/src/pyutils39/pyutils39.py'],
    long_description_content_type="text/markdown",
    url="https://github.com/Enderbyte-Programs/pyutils39",
    project_urls={
        "Bug Tracker": "https://github.com/Enderbyte-Programs/pyutils39/issues",
    },
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    package_dir={"": "src"},
    packages=setuptools.find_packages(where="src"),
    python_requires=">=3.8",

)

I am still new to stackoverflow, so if there is any way to improve my question, please tell me.


Solution

  • The problem is in setup.py using wrong directives for package sources. In setup() call, use py_modules=['pyutils39.py'] - py_modules is used to declare single-file modules. Now it probably uses packages or a similar parameter, declaring the whole directory as a module. It's hard to make a more informed guess because setup.py is missing in the repo.

    With the current layout, this will probably work:

    from pyutils39 import pyutils39