1

I have a couple seemingly simple use cases that I feel like I'm missing a step on.

Basically, I have this project structure:

|- session_storage_base.py
|- aws_session_storage.py
|- README.md
|- setup.py

And here is setup.py:

import setuptools

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

setuptools.setup(
    name="session_data_access", 
    version="0.3",
    author="Colin",
    author_email="colin@asdf.com",
    description="session storage data access.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/...",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)    

I run the command:

python3 setup.py sdist bdist_wheel

...which successfully creates session_data_access-0.3.tar.gz.

I then run:

python3 -m pip install /Users/.../dist/session_data_access-0.3.tar.gz

...and when I follow-up with:

python3 -m pip list

...I see "session-data-access 0.0.3" among the listed packages.

When I run:

which python3

...the value is: "/Library/Frameworks/Python.framework/Versions/3.8/bin/python3".

I also set the Python interpreter in VS Code to make sure it's pointed to the above path.

However, when I do this:

import aws_session_storage

...VS Code has a squiggly line with the error: "Unable to import aws_session_storage".

If I cut & paste the files from my package into the project with the import statement, it works fine.

This is happening on EVERY bit of code I try to locally package.

Where exactly am I going wrong?!?

Colin
  • 4,025
  • 21
  • 40

1 Answers1

2

setuptools.find_packages() is trying to find a package in your repository, which means:

A Python module which can contain other modules or recursively, other packages.

(source: https://packaging.python.org/glossary/#term-Import-Package)

So if you bundle your python script into a module you should be good:

|- session_data_access/
|   |- __init__.py
|   |- session_storage_base.py
|   |- aws_session_storage.py
|- README.md
|- setup.py

After installing it the way you did it you should be able to do:

from session_data_access import aws_session_storage

EDIT:

As I just discovered thanks to the link in your comment, Python 3.3 introduced native namespace packages, which allows you to omit the __init__.py file if you use setuptools.find_namespace_packages() instead of setuptools.find_packages() in the setup.py configuration file.

jeremyr
  • 425
  • 4
  • 12
  • Eureka! Thanks, jeremyr. Before I award the bounty, would you be willing to explain why I need __init__.py when some sources say that it's no longer required after python 3.3? https://stackoverflow.com/questions/448271/what-is-init-py-for – Colin Sep 30 '20 at 23:13
  • Until recently I have been working mainly with Python 2.7 so I actually wasn't aware that Python 3.3 brought [native namespace packages](https://packaging.python.org/guides/packaging-namespace-packages/#native-namespace-packages)! This is very interesting, thanks for sharing this link :) So it seems that if you replace `setuptools.find_packages()` by [setuptools.find_namespace_packages](https://setuptools.readthedocs.io/en/latest/userguide/package_discovery.html#using-find-namespace-or-find-namespace-packages), you should be able to get rid of the `__init__.py` file – jeremyr Sep 30 '20 at 23:57
  • Interestingly enough, it didn’t work till I added __init__.py. Maybe there’s an updated methodology. I’ll research separately. Thanks again!!! – Colin Oct 01 '20 at 04:54