Reputation:
I'm building a very simple package, and I managed to upload it to pypi.
I followed this post: https://www.freecodecamp.org/news/build-your-first-python-package/
But when I tried to import it and use it, a weird thing happened.
import testsimpleprinter as tsp
tsp.testsimpleprinter("Hello") # <- does not work
tsp.testsimpleprinter.testsimpleprinter("Hello there!") # <- this works just fine
TestSimplePrinter
├── testsimpleprinter
│ ├── testsimpleprinter.py <- inside this, I have a func called testsimpleprinter again
│ └── __init__.py <- "from testsimpleprinter import testsimpleprinter"
├── setup.py
At first I thought i had created setup.py
somewhere wrong, so I moved it inside testsimpleprinter folder. But when i tried with it I got something like this:
ERROR: File "setup.py" not found for legacy project testsimpleprinter==0.1.0 from https://files.pythonhosted.org/packages/08/36/6446d90277fa8899aaa68b811b/a89ba43807c58be2bab666197ff0e9f41c/testsimpleprinter-0.1.0.tar.gz#sha256=713fc48338620adfd4ef71102478d5e740ad77164fafbc19363f4cf1816922cc.
As in the post, I want to use my package like this
import testsimpleprinter as tsp
tsp.testsimpleprinter("Hello") # I want it to work
Thank you in advance.
Upvotes: 1
Views: 67
Reputation: 680
The issue is with your testsimpleprinter/__init__.py
file. With the import statement from testsimpleprinter import testsimpleprinter
you are reimporting the package, not the local testsimpleprinter.py
file. To specify the local file, you need to add a .
as a prefix, which should be like:
# testsimpleprinter/__init__.py file
from .testsimpleprinter import testsimpleprinter
Upvotes: 2