Skip to content

entry_point in setup.cfg with a src-layout project #3206

entry_point in setup.cfg with a src-layout project #3206
Mar 25, 2022 · 2 answers · 1 reply

My folders and files are structured with the src-layout. I tried to adapt the setuptools-docu about entry-points to it but fail.

hyperorg
├── src
│   └── hyperorg
│       ├── __init__.py
│       └── __main__.py
├── setup.cfg
└── setup.py

One point is that you locate the def main() in the __init__.py file instead of the __main__.py file. IMHO the latter is reserved for package code (see https://docs.python.org/3/library/__main__.html#main-py-in-python-packages). I do not want to move all my code from __main__.py into __init__.py. I hope there is a solution for this.

My __init__.py is nearly empty. There is no code just some global variables like this

$ cat __init__.py
__version__ = '0.0.1a1'

Ìn my __main__.py I created a def main() because of your documentation. The structure is like this:

def main():
    do_something()

if __name__ == '__main__':
    main()

The setup.cfg looks like this

[metadata]
name = Hyperorg
version = attr: hyperorg.__version__

[options]
package_dir=
    =src
packages = find:
zip_safe = False
install_requires =
    orgparse

[options.packages.find]
where = src

[options.entry_points]
console_scripts =
    hyperorg = hyperorg:main

I understand that this won't work because setuptools (or some of its components) are looking in __init__.py for a main() but it is in __main__.py in my case.

How can I solve this and leave my code in __main__.py?

If you are interested you can find the full code in the original repository: https://codeberg.org/buhtz/hyperorg/src/commit/3d2cc57061151db92cba47c13cf59631587bb070

To use a main entrypoint in hyperorg.__main__, simply write:

console_scripts =
    hyperorg = hyperorg.__main__:main

Replies

2 suggested answers
·
1 reply

To use a main entrypoint in hyperorg.__main__, simply write:

console_scripts =
    hyperorg = hyperorg.__main__:main
0 replies

Thanks a lot this helped. I was going back to your docu to find out what I misunderstood.

I think the example in the docu is a bit unusual. You do not put a def main() inside an __init__.py if your project has an __init__.py and a __main__.py. You will use __main__.py for that. Am I wrong?

1 reply
@abravalheri

Well, that is left for the programmer to decide... I you look around probably you are going to find many different examples of both.

Please feel free to submit proposals for improving the docs, they are always appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants