将自定义操作添加到 setup.py [英] Add custom action to setup.py

查看:54
本文介绍了将自定义操作添加到 setup.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个结构的项目:

I have the project with such structure:

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

config.py 包含两个变量(LOGINPASS)并添加到.gitignore.

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

我想将自定义操作添加到 setup.py 然后运行 ​​python setup.py install 而不是触发创建 config.pyinputs("Please write your login/pass") 在安装包之前.

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.

如何做对?

推荐答案

安装时进行任何自定义并不是一个好主意.在运行时进行自定义是一种很好的做法,通常是在第一次运行开始时.

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.

在程序开始时,您应该检查 loginpass 是否可用.如果 loginpass 不可用,则要求用户输入它们并将值保存在文件中.通常此类文件应保存在用户配置目录中.通常,您会使用 appdirs 库来获取此类的正确位置一个文件.

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.

类似的东西:

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

相关:

这篇关于将自定义操作添加到 setup.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆