Repository: lightly-ai/lightly

Issue:
Cleanup setup.py
## Tasks

- [ ] Move as many options as possible to setup.cfg
- [ ] Update outdated options (for example Python 10 and 11 are missing in classifiers )
- [ ] Remove unused parts


Discussion:

Resolving patch:
diff --git a/lightly/__init__.py b/lightly/__init__.py
--- a/lightly/__init__.py
+++ b/lightly/__init__.py
@@ -79,40 +79,25 @@
 
 import os
 
+# see if torchvision vision transformer is available
 try:
-    # See (https://github.com/PyTorchLightning/pytorch-lightning)
-    # This variable is injected in the __builtins__ by the build
-    # process. It used to enable importing subpackages of skimage when
-    # the binaries are not built
-    __LIGHTLY_SETUP__
-except NameError:
-    __LIGHTLY_SETUP__ = False
-
-
-if __LIGHTLY_SETUP__:
-    # setting up lightly
-    msg = f"Partial import of {__name__}=={__version__} during build process."
-    print(msg)
-else:
-    # see if torchvision vision transformer is available
-    try:
-        import torchvision.models.vision_transformer
-
-        _torchvision_vit_available = True
-    except (
-        RuntimeError,  # Different CUDA versions for torch and torchvision
-        OSError,  # Different CUDA versions for torch and torchvision (old)
-        ImportError,  # No installation or old version of torchvision
-    ):
-        _torchvision_vit_available = False
-
-    if os.getenv("LIGHTLY_DID_VERSION_CHECK", "False") == "False":
-        os.environ["LIGHTLY_DID_VERSION_CHECK"] = "True"
-        import multiprocessing
-
-        if multiprocessing.current_process().name == "MainProcess":
-            from lightly.api import _version_checking
-
-            _version_checking.check_is_latest_version_in_background(
-                current_version=__version__
-            )
+    import torchvision.models.vision_transformer
+
+    _torchvision_vit_available = True
+except (
+    RuntimeError,  # Different CUDA versions for torch and torchvision
+    OSError,  # Different CUDA versions for torch and torchvision (old)
+    ImportError,  # No installation or old version of torchvision
+):
+    _torchvision_vit_available = False
+
+if os.getenv("LIGHTLY_DID_VERSION_CHECK", "False") == "False":
+    os.environ["LIGHTLY_DID_VERSION_CHECK"] = "True"
+    import multiprocessing
+
+    if multiprocessing.current_process().name == "MainProcess":
+        from lightly.api import _version_checking
+
+        _version_checking.check_is_latest_version_in_background(
+            current_version=__version__
+        )
diff --git a/lightly/utils/cropping/__init__.py b/lightly/utils/cropping/__init__.py
new file mode 100644
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -1,36 +1,19 @@
 import os
-import sys
+from pathlib import Path
+from typing import List
 
 import setuptools
 
-try:
-    import builtins
-except ImportError:
-    import __builtin__ as builtins
+_PATH_ROOT = Path(os.path.dirname(__file__))
 
-PATH_ROOT = PATH_ROOT = os.path.dirname(__file__)
-builtins.__LIGHTLY_SETUP__ = True
 
-import lightly
-
-
-def load_description(path_dir=PATH_ROOT, filename="DOCS.md"):
-    """Load long description from readme in the path_dir/ directory"""
-    with open(os.path.join(path_dir, filename)) as f:
-        long_description = f.read()
-    return long_description
-
-
-def load_requirements(path_dir=PATH_ROOT, filename="base.txt", comment_char="#"):
-    """From pytorch-lightning repo: https://github.com/PyTorchLightning/pytorch-lightning.
-    Load requirements from text file in the path_dir/requirements/ directory.
-
-    """
-    with open(os.path.join(path_dir, "requirements", filename), "r") as file:
+def load_requirements(filename: str, comment_char: str = "#") -> List[str]:
+    """Load requirements from text file in the requirements directory."""
+    with (_PATH_ROOT / "requirements" / filename).open() as file:
         lines = [ln.strip() for ln in file.readlines()]
     reqs = []
     for ln in lines:
-        # filer all comments
+        # filter all comments
         if comment_char in ln:
             ln = ln[: ln.index(comment_char)].strip()
         # skip directly installed dependencies
@@ -41,29 +24,44 @@ def load_requirements(path_dir=PATH_ROOT, filename="base.txt", comment_char="#")
     return reqs
 
 
+def load_version() -> str:
+    """Load version from the lightly/__init__.py file.
+
+    Note: We do not want to get the version by accessing `lightly.__version__` because
+    it would require importing `lightly`. Importing `lightly` in setup.py breaks the
+    installation process as the import has side effects and requires dependencies to be
+    installed. As dependencies are not yet available during installation, the `lightly`
+    import fails.
+    """
+    version_filepath = _PATH_ROOT / "lightly" / "__init__.py"
+    with version_filepath.open() as file:
+        for line in file.readlines():
+            if line.startswith("__version__"):
+                version = line.split("=")[-1].strip().strip('"')
+                return version
+    raise RuntimeError("Unable to find version string in '{version_filepath}'.")
+
+
 if __name__ == "__main__":
     name = "lightly"
-    version = lightly.__version__
-    description = lightly.__doc__
-
-    author = "Philipp Wirth & Igor Susmelj"
-    author_email = "philipp@lightly.ai"
+    version = load_version()
+    author = "Lightly Team"
+    author_email = "team@lightly.ai"
     description = "A deep learning package for self-supervised learning"
+    long_description = (_PATH_ROOT / "README.md").read_text()
 
     entry_points = {
         "console_scripts": [
             "lightly-crop = lightly.cli.crop_cli:entry",
-            "lightly-train = lightly.cli.train_cli:entry",
+            "lightly-download = lightly.cli.download_cli:entry",
             "lightly-embed = lightly.cli.embed_cli:entry",
             "lightly-magic = lightly.cli.lightly_cli:entry",
-            "lightly-download = lightly.cli.download_cli:entry",
-            "lightly-version = lightly.cli.version_cli:entry",
             "lightly-serve = lightly.cli.serve_cli:entry",
+            "lightly-train = lightly.cli.train_cli:entry",
+            "lightly-version = lightly.cli.version_cli:entry",
         ]
     }
 
-    long_description = load_description()
-
     python_requires = ">=3.6"
     base_requires = load_requirements(filename="base.txt")
     openapi_requires = load_requirements(filename="openapi.txt")
@@ -79,28 +77,7 @@ def load_requirements(path_dir=PATH_ROOT, filename="base.txt", comment_char="#")
         "all": dev_requires + video_requires,
     }
 
-    packages = [
-        "lightly",
-        "lightly.api",
-        "lightly.cli",
-        "lightly.cli.config",
-        "lightly.data",
-        "lightly.embedding",
-        "lightly.loss",
-        "lightly.loss.regularizer",
-        "lightly.models",
-        "lightly.models.modules",
-        "lightly.transforms",
-        "lightly.utils",
-        "lightly.utils.benchmarking",
-        "lightly.utils.cropping",
-        "lightly.active_learning",
-        "lightly.active_learning.config",
-        "lightly.openapi_generated",
-        "lightly.openapi_generated.swagger_client",
-        "lightly.openapi_generated.swagger_client.api",
-        "lightly.openapi_generated.swagger_client.models",
-    ]
+    packages = setuptools.find_packages(include=["lightly*"])
 
     project_urls = {
         "Homepage": "https://www.lightly.ai",
@@ -116,8 +93,9 @@ def load_requirements(path_dir=PATH_ROOT, filename="base.txt", comment_char="#")
         "Intended Audience :: Education",
         "Intended Audience :: Science/Research",
         "Topic :: Scientific/Engineering",
-        "Topic :: Scientific/Engineering :: Mathematics",
         "Topic :: Scientific/Engineering :: Artificial Intelligence",
+        "Topic :: Scientific/Engineering :: Image Processing",
+        "Topic :: Scientific/Engineering :: Mathematics",
         "Topic :: Software Development",
         "Topic :: Software Development :: Libraries",
         "Topic :: Software Development :: Libraries :: Python Modules",
@@ -126,6 +104,8 @@ def load_requirements(path_dir=PATH_ROOT, filename="base.txt", comment_char="#")
         "Programming Language :: Python :: 3.7",
         "Programming Language :: Python :: 3.8",
         "Programming Language :: Python :: 3.9",
+        "Programming Language :: Python :: 3.10",
+        "Programming Language :: Python :: 3.11",
         "License :: OSI Approved :: MIT License",
     ]
 
@@ -137,6 +117,7 @@ def load_requirements(path_dir=PATH_ROOT, filename="base.txt", comment_char="#")
         description=description,
         entry_points=entry_points,
         license="MIT",
+        license_files=["LICENSE.txt"],
         long_description=long_description,
         long_description_content_type="text/markdown",
         setup_requires=setup_requires,


Test patch: