2

I am trying to upload python package to PyPi and Anaconda. here's the tree structure of my project example:

.
├── LICENSE
├── MINIFEST.in
├── README.md
├── setup.py
└── example
    ├── __init__.py
    ├── algorithms
    │   ├── __init__.py
    │   ├── calculations
    │   │   ├── __init__.py
    │   │   ├── common.py
    │   │   └── discrete.py
    │   ├── regression
    │   │   ├── __init__.py
    │   │   ├── bezier.py
    │   │   ├── constants.py
    │   │   ├── describe.py
    │   │   ├── funcs.py
    │   │   ├── matrices.py
    │   │   ├── modules.py
    │   │   ├── mono.py
    │   │   └── regress.py
    │   └── sketch
    │       ├── __init__.py
    │       ├── data.py
    │       ├── outliers.py
    │       ├── settings.py
    │       └── theme.py
    └── main.py

setup.py follows by

from setuptools import setup


def readme():
    with open('README.md') as f:
        info = f.read()
    return info


setup(
    name='example',
    version='1.0.0',
    description='example desc',
    long_description=readme(),
    long_description_content_type='text/markdown',
    url='https://github.com/test/test',
    author='test',
    author_email='test@gmail.com',
    license='MIT',
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.8",
    ],
    packages=['example'],
    include_package_data=True,
    install_requires=[],
)

however, after publication, I used pip install, and the directory I got is:

.
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-37.pyc
│   └── main.cpython-37.pyc
└── main.py

I have read

but mine are python files instead of static javascript.


I have read Subfolder in Python package not visible when installed

and changed my setup.py to:

from setuptools import setup, find_packages


def readme():
    with open('README.md') as f:
        info = f.read()
    return info


setup(
    name='example',
    version='1.0.1',
    description='example desc',
    long_description=readme(),
    long_description_content_type='text/markdown',
    url='https://github.com/test/test/',
    author='example',
    author_email='example@gmail.com',
    license='MIT',
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.8",
    ],
    packages=find_packages(),
    include_package_data=True,
    install_requires=[],
)

after publication, I installed my package by pip install, but this time, all my files are there, which the tree looks exactly as what I expected. however, in main.py,

from algorithms.sketch.data import *
# ModuleNotFoundError: No module named 'algorithms'

Then I tried Why do I need to include sub-packages in setup.py

which setup.py

from setuptools import setup


def readme():
    with open('README.md') as f:
        info = f.read()
    return info


setup(
    name='example',
    version='1.0.3',
    description='example',
    long_description=readme(),
    long_description_content_type='text/markdown',
    url='https://github.com/example/example/',
    author='example',
    author_email='example@gmail.com',
    license='MIT',
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.8",
    ],
    packages=['example', 'example.algorithms', 'example.algorithms.calculations', 'example.algorithms.regression', 'example.algorithms.sketch'],
    include_package_data=True,
    install_requires=[],
)

exactly like before:

  • all my directories are there
  • if I run main.py explicitly in side_packages, it works fine.
  • but if I use it as a package, which from example.main import *, and it results the same error as above
from algorithms.sketch.data import *
# ModuleNotFoundError: No module named 'algorithms'

MAINIFEST.in follows by:

include README.md LICENSE

any suggestions will be appreciated

Weilory
  • 2,621
  • 19
  • 35
  • 1
    What you have created is actually a namespace package called `example.algorithms` when you used `packages=find_packages()` as is. You will need to `from example.algorithms.sketch.data import *`. If you want `example/` to be the "root" of the project (even though typically it would be called `src/`), You will need `packages=find_packages('example')` and `package_dir={'': 'example'},` passed to `setup()`. – metatoaster Jun 22 '21 at 05:04
  • 1
    If you want to do relative imports instead, e.g. if the module is inside `example/`, importing the relative `algorithms` subpackage will need to be done using `import .algorithms...` – metatoaster Jun 22 '21 at 05:06

0 Answers0