LoginSignup
19
6

More than 5 years have passed since last update.

Pipenvでnpm-scriptsみたいにPipfileへコマンドを書く

Last updated at Posted at 2018-03-13

概要

  • pipenvの隠し機能で、npm-scriptsみたいな機能が実装されていた。
  • Pipfileに[scripts]セクションを追加すると、npm run <コマンド名>pipenv run <コマンド名>で実行できる!
  • npm-scriptsでできることすべてができるわけではないし、なぜ隠したのかは知らない

★追記★
ドキュメントにも追記されました。
https://pipenv-ja.readthedocs.io/ja/translate-ja/advanced.html#custom-script-shortcuts

背景

pypa/pipenvに、npm-scriptsのような機能がほしいというissueは以前(2017/10/4)からあった。
Scripts section in Pipfile #826

ないならしかたないし、自分で作ることも考えていたところ、
pypa/pipenvのWikiが更新され、IdeasからHidden Featuresに変わっていた!

試してみた

環境

項目 内容
OS macOS High Sierra
python 3.6.4
pipenv 11.6.3

初期化

まずは、Pipfileを作る。

> pipenv install --three

PipfileとPipfile.lockが作られた。

Pipfile
[[source]]

url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"


[packages]



[dev-packages]

もし、この状態でpipenv run startと実行しても、startコマンドは存在しないため、当然エラーとなる。

> pipenv run start
Error: the command start could not be found within PATH or Pipfile's [scripts].

[scripts]セクションを追加する

Pipfile
[[source]]

url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"


[packages]



[dev-packages]

[scripts]

start = "echo aa"

そして、再実行すると、echo aaが実行される。

> pipenv run start
aa

いつ頃実装されてたのだろう?

該当のソースを追ってみたところ、2018/2/16の修正っぽい

なので、v9.1.0~なら使える。

いいね!

19
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
6