Do I need an __init__.py if I want to build a standalone program and not a package?

人走茶凉 提交于 2019-12-08 03:13:35

问题


I am working on a standalone Python-GUI-program. When I run

python3 setup.py sdist

I get the following warning:

package init file 'main-application-folder/__init__.py' not found (or not a regular file)

Is this warning only meant for packages or should a standalone Python program also have an __init__.py instead of something like main-window.py or my-first-program.py?

Should I rename my main file (in my case ìbk-st.py to __init__.py) or would it be good to retain a certain structure in the __init__.py (i.e. make a separate file that calls the ibk-st.py file)

Link to project:

  • https://github.com/tobias47n9e/innsbruck-stereographic

回答1:


You should rename the ibk-st into a valid package identifier for python (ibk_st maybe); then ibk-st.py into something like main_ui.py; then have an __init__.py for that whole package (alternatively you can rename ibk-st.py to __init__.py).

Do note that setup.py can install command line scripts; you can provide a thin wrapper as file bin/ibk-st-ui with contents

#!/usr/bin/env python

from ibk_st.main_ui import main
main()

The module installation will ensure that the script is runnable on whatever platform the user is using.

Then in your setup.py you should have

...
packages = [ 'ibk_st' ],
scripts=[ 'bin/ibk-st-ui' ],
...

Now when you run the setup.py install or install the package, the modules can be embedded into other programs and the command ibk-st-ui will be installed in the the bin folder (be it the bin of a virtualenv or the system /usr/local/bin), that can run the UI.



来源:https://stackoverflow.com/questions/28613439/do-i-need-an-init-py-if-i-want-to-build-a-standalone-program-and-not-a-packa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!