# Python packaging, setup.py, console_scripts entrypoints, and __main__.py
```python
!ls **
```
examplenb.ipynb requirements.txt setup.py
example:
__init__.py __main__.py thecode.py
```python
!cat setup.py
```
from setuptools import setup
setup(name='example',
version='0.1',
description='Example',
url='http://github.com/.../...',
author='Example',
author_email='[email protected]',
license='...',
packages=['example'],
zip_safe=False,
entry_points={
'console_scripts': [
'example = example.thecode:main'
]}
)
```python
!cat example/thecode.py
```
import sys
def main(argv=None):
if argv is None:
argv = sys.argv
print("argparse here", argv)
```python
!cat example/__main__.py
```
import sys
from .thecode import main
def othermain():
print("othermain")
return main(sys.argv)
if __name__ == "__main__":
othermain()
```python
!cat ./requirements.txt
```
-e .
```python
!pip install -r ./requirements.txt
# (this is equivalent to `pip install -e .`)
```
Obtaining file:///home/user/-wrk/-ve37/s_example/src/s_example
(from -r ./requirements.txt (line 1))
Installing collected packages: example
Found existing installation: example 0.1
Uninstalling example-0.1:
Successfully uninstalled example-0.1
Running setup.py develop for example
Successfully installed example
```python
!ls **
```
examplenb.ipynb requirements.txt setup.py
example:
__init__.py __main__.py thecode.py
example.egg-info:
dependency_links.txt not-zip-safe SOURCES.txt
entry_points.txt PKG-INFO top_level.txt
```python
# This is generated when you `pip install -e`
!cat ./example.egg-info/entry_points.txt
```
[console_scripts]
example = example.thecode:main
```python
# this runs the 'example' console_scripts entry_point
!example
```
argparse here ['/home/user/-wrk/-ve37/s_example/bin/example']
```python
# this runs example/__main__
!python -m example
```
othermain
argparse here
['/home/user/-wrk/-ve37/s_example/src/s_example/example/__main__.py']
...
When you want the project to be reproducible:
- you have tests (eg in tests/)
- you have a requirements.txt that lists exact versions of all
dependencies
- you build the entire environment (including any requisite OS packages)
from zero (with a virtualenv and/or docker), then run the tests
There are a number of cookiecutters (project templates)
which have the whole project setup:
-
https://cookiecutter.readthedocs.io/en/latest/readme.html#available-cookiecutters
- cookiecutter-pypackage includes pytest setup
- https://cookiecutter.readthedocs.io/en/latest/readme.html#data-science
- https://cookiecutter.readthedocs.io/en/latest/readme.html#reproducible-science
I prepared this as a Jupyter notebook, then did 'Save as' > 'Markdown'.
Because it has a requirements.txt, if I put this code in a git repo
and launch it in
mybinder.org (repo2docker), it will build a docker container (also
containing jupyter)
and launch a free cloud instance; so that others can review the code
and notebooks
in a read/write environment (where changes are not persisted because
the instance is later just deleted)
- REES: Reproducible Environment Specification
https://repo2docker.readthedocs.io/en/latest/specification.html
- environment.yml - Install a Python environment
- Pipfile and/or Pipfile.lock - Install a Python environment
- requirements.txt - Install a Python environment
- setup.py - Install Python packages
- Project.toml - Install a Julia environment
- REQUIRE - Install a Julia environment (legacy)
- install.R - Install an R/RStudio environment
- apt.txt - Install packages with apt-get
- DESCRIPTION - Install an R package
- manifest.xml - Install Stencila
- postBuild - Run code after installing the environment
- start - Run code before the user sessions starts
- runtime.txt - Specifying runtimes
- default.nix - the nix package manager
- Dockerfile - Advanced environments
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/E3OTEWVZZES7QRKDZXTHS26W3IO4XTPR/
Code of Conduct: http://python.org/psf/codeofconduct/