質問

私は Python モジュールをパッケージ化しているのですが、ユーザーがいくつかのカスタム オプションを使用してモジュールを構築できるようにしたいと考えています。具体的には、パッケージが使用できる特定の実行可能ファイルを提供すると、パッケージは特別な魔法を実行します。

理想的には、ユーザーは次のように実行します。 setup.py install または setup.py install --magic-doer=/path/to/executable. 。彼らが 2 番目のオプションを使用した場合、私はコード内のどこかに変数を設定し、そこから作業を進めることになります。

これはPythonでも可能でしょうか? setuptools?もしそうなら、どうすればいいですか?

役に立ちましたか?

解決

できるようです...読む これ.

記事からの抜粋:

コマンドは setuptools.Command から派生する単純なクラスであり、次のような最小限の要素を定義します。

description: describe the command
user_options: a list of options
initialize_options(): called at startup
finalize_options(): called at the end
run(): called to run the command

setuptools ドキュメントには、Command のサブクラス化に関する情報がまだ含まれていませんが、最小限のクラスは次のようになります。

 class MyCommand(Command):
     """setuptools Command"""
     description = "run my command"
     user_options = tuple()
     def initialize_options(self):
         """init options"""
         pass

     def finalize_options(self):
         """finalize options"""
         pass

     def run(self):
         """runner"""
         XXX DO THE JOB HERE

次に、setup.py ファイル内のエントリ ポイントを使用して、クラスをコマンドとしてフックできます。

 setup(
     # ...
     entry_points = {
     "distutils.commands": [
     "my_command = mypackage.some_module:MyCommand"]}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top