Skip to content

Commit 717ea43

Browse files
committed
refactoring setup (#6590)
* refactoring setup * . * docs * flake8 (cherry picked from commit 1fae10a)
1 parent 5a4529a commit 717ea43

File tree

6 files changed

+101
-93
lines changed

6 files changed

+101
-93
lines changed

docs/source/conf.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# documentation root, use os.path.abspath to make it absolute, like shown here.
1414

1515
# import m2r
16-
import builtins
1716
import glob
1817
import os
1918
import shutil
@@ -27,10 +26,13 @@
2726

2827
FOLDER_GENERATED = 'generated'
2928
SPHINX_MOCK_REQUIREMENTS = int(os.environ.get('SPHINX_MOCK_REQUIREMENTS', True))
30-
if SPHINX_MOCK_REQUIREMENTS:
31-
builtins.__LIGHTNING_SETUP__ = True
3229

33-
import pytorch_lightning # noqa: E402
30+
try:
31+
from pytorch_lightning import info
32+
except ImportError:
33+
# alternative https://stackoverflow.com/a/67692/4521646
34+
sys.path.append(os.path.join(PATH_ROOT, "pytorch_lightning"))
35+
import info
3436

3537
# -- Project documents -------------------------------------------------------
3638

@@ -79,13 +81,13 @@ def _transform_changelog(path_in: str, path_out: str) -> None:
7981
# -- Project information -----------------------------------------------------
8082

8183
project = 'PyTorch Lightning'
82-
copyright = pytorch_lightning.__copyright__
83-
author = pytorch_lightning.__author__
84+
copyright = info.__copyright__
85+
author = info.__author__
8486

8587
# The short X.Y version
86-
version = pytorch_lightning.__version__
88+
version = info.__version__
8789
# The full version, including alpha/beta/rc tags
88-
release = pytorch_lightning.__version__
90+
release = info.__version__
8991

9092
# -- General configuration ---------------------------------------------------
9193

@@ -176,8 +178,8 @@ def _transform_changelog(path_in: str, path_out: str) -> None:
176178
# documentation.
177179

178180
html_theme_options = {
179-
'pytorch_project': pytorch_lightning.__homepage__,
180-
'canonical_url': pytorch_lightning.__homepage__,
181+
'pytorch_project': info.__homepage__,
182+
'canonical_url': info.__homepage__,
181183
'collapse_navigation': False,
182184
'display_version': True,
183185
'logo_only': False,
@@ -279,6 +281,7 @@ def _transform_changelog(path_in: str, path_out: str) -> None:
279281
'torch': ('https://pytorch.org/docs/stable/', None),
280282
'numpy': ('https://numpy.org/doc/stable/', None),
281283
'PIL': ('https://pillow.readthedocs.io/en/stable/', None),
284+
'torchmetrics': ('https://torchmetrics.readthedocs.io/en/stable/', None),
282285
}
283286

284287
# -- Options for todo extension ----------------------------------------------

pytorch_lightning/__init__.py

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,17 @@
22

33
import logging
44
import os
5-
import sys
6-
import time
75

8-
_this_year = time.strftime("%Y")
9-
__version__ = '1.2.4'
10-
__author__ = 'William Falcon et al.'
11-
__author_email__ = '[email protected]'
12-
__license__ = 'Apache-2.0'
13-
__copyright__ = f'Copyright (c) 2018-{_this_year}, {__author__}.'
14-
__homepage__ = 'https://github.com/PyTorchLightning/pytorch-lightning'
15-
# this has to be simple string, see: https://github.com/pypa/twine/issues/522
16-
__docs__ = (
17-
"PyTorch Lightning is the lightweight PyTorch wrapper for ML researchers."
18-
" Scale your models. Write less boilerplate."
6+
from pytorch_lightning.info import ( # noqa: F401
7+
__author__,
8+
__author_email__,
9+
__copyright__,
10+
__docs__,
11+
__homepage__,
12+
__license__,
13+
__version__,
1914
)
20-
__long_docs__ = """
21-
Lightning is a way to organize your PyTorch code to decouple the science code from the engineering.
22-
It's more of a style-guide than a framework.
2315

24-
In Lightning, you organize your code into 3 distinct categories:
25-
26-
1. Research code (goes in the LightningModule).
27-
2. Engineering code (you delete, and is handled by the Trainer).
28-
3. Non-essential research code (logging, etc. this goes in Callbacks).
29-
30-
Although your research/production project might start simple, once you add things like GPU AND TPU training,
31-
16-bit precision, etc, you end up spending more time engineering than researching.
32-
Lightning automates AND rigorously tests those parts for you.
33-
34-
Overall, Lightning guarantees rigorously tested, correct, modern best practices for the automated parts.
35-
36-
Documentation
37-
-------------
38-
- https://pytorch-lightning.readthedocs.io/en/latest
39-
- https://pytorch-lightning.readthedocs.io/en/stable
40-
"""
4116
_root_logger = logging.getLogger()
4217
_logger = logging.getLogger(__name__)
4318
_logger.setLevel(logging.INFO)
@@ -50,32 +25,20 @@
5025
_PACKAGE_ROOT = os.path.dirname(__file__)
5126
_PROJECT_ROOT = os.path.dirname(_PACKAGE_ROOT)
5227

53-
try:
54-
# This variable is injected in the __builtins__ by the build
55-
# process. It used to enable importing subpackages of skimage when
56-
# the binaries are not built
57-
_ = None if __LIGHTNING_SETUP__ else None
58-
except NameError:
59-
__LIGHTNING_SETUP__: bool = False
60-
61-
if __LIGHTNING_SETUP__: # pragma: no-cover
62-
sys.stdout.write(f'Partial import of `{__name__}` during the build process.\n') # pragma: no-cover
63-
# We are not importing the rest of the lightning during the build process, as it may not be compiled yet
64-
else:
65-
from pytorch_lightning import metrics
66-
from pytorch_lightning.callbacks import Callback
67-
from pytorch_lightning.core import LightningDataModule, LightningModule
68-
from pytorch_lightning.trainer import Trainer
69-
from pytorch_lightning.utilities.seed import seed_everything
70-
71-
__all__ = [
72-
'Trainer',
73-
'LightningDataModule',
74-
'LightningModule',
75-
'Callback',
76-
'seed_everything',
77-
'metrics',
78-
]
28+
from pytorch_lightning import metrics # noqa: E402
29+
from pytorch_lightning.callbacks import Callback # noqa: E402
30+
from pytorch_lightning.core import LightningDataModule, LightningModule # noqa: E402
31+
from pytorch_lightning.trainer import Trainer # noqa: E402
32+
from pytorch_lightning.utilities.seed import seed_everything # noqa: E402
33+
34+
__all__ = [
35+
'Trainer',
36+
'LightningDataModule',
37+
'LightningModule',
38+
'Callback',
39+
'seed_everything',
40+
'metrics',
41+
]
7942

8043
# for compatibility with namespace packages
8144
__import__('pkg_resources').declare_namespace(__name__)

pytorch_lightning/callbacks/progress.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737

3838
class tqdm(_tqdm):
3939
"""
40-
Custom tqdm progressbar where we append 0 to floating points/strings to
41-
prevent the progress bar from flickering
40+
Custom tqdm progressbar where we append 0 to floating points/strings to prevent the progress bar from flickering
4241
"""
4342

4443
@staticmethod

pytorch_lightning/info.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import time
2+
3+
_this_year = time.strftime("%Y")
4+
__version__ = '1.2.4'
5+
__author__ = 'William Falcon et al.'
6+
__author_email__ = '[email protected]'
7+
__license__ = 'Apache-2.0'
8+
__copyright__ = f'Copyright (c) 2018-{_this_year}, {__author__}.'
9+
__homepage__ = 'https://github.com/PyTorchLightning/pytorch-lightning'
10+
# this has to be simple string, see: https://github.com/pypa/twine/issues/522
11+
__docs__ = (
12+
"PyTorch Lightning is the lightweight PyTorch wrapper for ML researchers."
13+
" Scale your models. Write less boilerplate."
14+
)
15+
__long_docs__ = """
16+
Lightning is a way to organize your PyTorch code to decouple the science code from the engineering.
17+
It's more of a style-guide than a framework.
18+
19+
In Lightning, you organize your code into 3 distinct categories:
20+
21+
1. Research code (goes in the LightningModule).
22+
2. Engineering code (you delete, and is handled by the Trainer).
23+
3. Non-essential research code (logging, etc. this goes in Callbacks).
24+
25+
Although your research/production project might start simple, once you add things like GPU AND TPU training,
26+
16-bit precision, etc, you end up spending more time engineering than researching.
27+
Lightning automates AND rigorously tests those parts for you.
28+
29+
Overall, Lightning guarantees rigorously tested, correct, modern best practices for the automated parts.
30+
31+
Documentation
32+
-------------
33+
- https://pytorch-lightning.readthedocs.io/en/latest
34+
- https://pytorch-lightning.readthedocs.io/en/stable
35+
"""

pytorch_lightning/setup_tools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import re
1717
from typing import List
1818

19-
from pytorch_lightning import __homepage__, __version__, _PROJECT_ROOT
19+
_PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
2020

2121

2222
def _load_requirements(path_dir: str, file_name: str = 'requirements.txt', comment_char: str = '#') -> List[str]:
@@ -40,10 +40,10 @@ def _load_requirements(path_dir: str, file_name: str = 'requirements.txt', comme
4040
return reqs
4141

4242

43-
def _load_readme_description(path_dir: str, homepage: str = __homepage__, version: str = __version__) -> str:
43+
def _load_readme_description(path_dir: str, homepage: str, version: str) -> str:
4444
"""Load readme as decribtion
4545
46-
>>> _load_readme_description(_PROJECT_ROOT) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
46+
>>> _load_readme_description(_PROJECT_ROOT, "", "") # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
4747
'<div align="center">...'
4848
"""
4949
path_readme = os.path.join(path_dir, "README.md")

setup.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,33 @@
1616
import os
1717

1818
# Always prefer setuptools over distutils
19+
import sys
20+
1921
from setuptools import find_packages, setup
2022

2123
try:
22-
import builtins
24+
from pytorch_lightning import info, setup_tools
2325
except ImportError:
24-
import __builtin__ as builtins
26+
# alternative https://stackoverflow.com/a/67692/4521646
27+
sys.path.append("pytorch_lightning")
28+
import info
29+
import setup_tools
2530

2631
# https://packaging.python.org/guides/single-sourcing-package-version/
2732
# http://blog.ionelmc.ro/2014/05/25/python-packaging/
28-
PATH_ROOT = os.path.dirname(__file__)
29-
builtins.__LIGHTNING_SETUP__ = True
30-
31-
import pytorch_lightning # noqa: E402
32-
from pytorch_lightning.setup_tools import _load_readme_description, _load_requirements # noqa: E402
33+
_PATH_ROOT = os.path.dirname(__file__)
34+
_PATH_REQUIRE = os.path.join(_PATH_ROOT, 'requirements')
3335

3436
# https://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-extras
3537
# Define package extras. These are only installed if you specify them.
3638
# From remote, use like `pip install pytorch-lightning[dev, docs]`
3739
# From local copy of repo, use like `pip install ".[dev, docs]"`
3840
extras = {
3941
# 'docs': load_requirements(file_name='docs.txt'),
40-
'examples': _load_requirements(path_dir=os.path.join(PATH_ROOT, 'requirements'), file_name='examples.txt'),
41-
'loggers': _load_requirements(path_dir=os.path.join(PATH_ROOT, 'requirements'), file_name='loggers.txt'),
42-
'extra': _load_requirements(path_dir=os.path.join(PATH_ROOT, 'requirements'), file_name='extra.txt'),
43-
'test': _load_requirements(path_dir=os.path.join(PATH_ROOT, 'requirements'), file_name='test.txt')
42+
'examples': setup_tools._load_requirements(path_dir=_PATH_REQUIRE, file_name='examples.txt'),
43+
'loggers': setup_tools._load_requirements(path_dir=_PATH_REQUIRE, file_name='loggers.txt'),
44+
'extra': setup_tools._load_requirements(path_dir=_PATH_REQUIRE, file_name='extra.txt'),
45+
'test': setup_tools._load_requirements(path_dir=_PATH_REQUIRE, file_name='test.txt')
4446
}
4547
extras['dev'] = extras['extra'] + extras['loggers'] + extras['test']
4648
extras['all'] = extras['dev'] + extras['examples'] # + extras['docs']
@@ -53,29 +55,35 @@
5355
# filter cpu only packages
5456
extras[ex] = [pkg for pkg in extras[kw] if not any(pgpu.lower() in pkg.lower() for pgpu in PACKAGES_GPU_ONLY)]
5557

58+
long_description = setup_tools._load_readme_description(
59+
_PATH_ROOT,
60+
homepage=info.__homepage__,
61+
version=info.__version__,
62+
)
63+
5664
# https://packaging.python.org/discussions/install-requires-vs-requirements /
5765
# keep the meta-data here for simplicity in reading this file... it's not obvious
5866
# what happens and to non-engineers they won't know to look in init ...
5967
# the goal of the project is simplicity for researchers, don't want to add too much
6068
# engineer specific practices
6169
setup(
6270
name="pytorch-lightning",
63-
version=pytorch_lightning.__version__,
64-
description=pytorch_lightning.__docs__,
65-
author=pytorch_lightning.__author__,
66-
author_email=pytorch_lightning.__author_email__,
67-
url=pytorch_lightning.__homepage__,
71+
version=info.__version__,
72+
description=info.__docs__,
73+
author=info.__author__,
74+
author_email=info.__author_email__,
75+
url=info.__homepage__,
6876
download_url='https://github.com/PyTorchLightning/pytorch-lightning',
69-
license=pytorch_lightning.__license__,
77+
license=info.__license__,
7078
packages=find_packages(exclude=['tests', 'tests/*', 'benchmarks', 'legacy', 'legacy/*']),
71-
long_description=_load_readme_description(PATH_ROOT),
79+
long_description=long_description,
7280
long_description_content_type='text/markdown',
7381
include_package_data=True,
7482
zip_safe=False,
7583
keywords=['deep learning', 'pytorch', 'AI'],
7684
python_requires='>=3.6',
7785
setup_requires=[],
78-
install_requires=_load_requirements(PATH_ROOT),
86+
install_requires=setup_tools._load_requirements(_PATH_ROOT),
7987
extras_require=extras,
8088
project_urls={
8189
"Bug Tracker": "https://github.com/PyTorchLightning/pytorch-lightning/issues",

0 commit comments

Comments
 (0)