Add custom action to setup.py

南楼画角 提交于 2021-02-10 15:44:43

问题


I have the project with such structure:

Project/
    Project/
        __init__.py
        config.py
    setup.py
    .gitignore

config.py contains two variables (LOGIN, PASS) and is added to .gitignore.

I would like to add custom action to setup.py then run python setup.py install than triggered creating config.py with some inputs("Please write your login/pass") prior to installation of package.

How to do it right?


回答1:


It is not a good idea to do any customization at install time. It is good practice to do customization at run time, usually at the start of the first run.

At the start of your program, you should check if login and pass are somehow available. If login and pass are not available, then ask the user to enter them and save the values in a file. Usually such files should be saved in user configuration directory. Typically you would use the appdirs library to get the right location for such a file.

Something like that:

import pathlib

import appdirs

user_config_dir = appdirs.user_config_dir('MyApp', 'tibhar940')
user_config_path = pathlib.Path(user_config_dir, 'config.cfg')

if user_config_path.is_file():
    # read
else:
    # prompt the user and save in file

Related:

  • How to setup application to personalize it?
  • How to install writable shared and user specific data files with setuptools?


来源:https://stackoverflow.com/questions/64450134/add-custom-action-to-setup-py

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