DEV Community


Posted on • Updated on

I love pipenv's custom script shortcuts

Update: I decided the below documentation is a bit naff, so I submitted a PR with some changes, and they were accepted! 🎉 Check it out -

From the documentation:

☤ Custom Script Shortcuts

Pipenv supports to customize shortcuts in the scripts section. pipenv run will automatically load it and find the correct command to replace with. Given the Pipfile:

    printfoo = "python -c \"print('foo')\""
Enter fullscreen mode Exit fullscreen mode

You can type in your terminal to run:

    $ pipenv run printfoo
Enter fullscreen mode Exit fullscreen mode

/end documentation

And it's really that simple! It runs the script you give it with the pipenv virtual environment context whether you have your pipenv shell activated or not. I have found this super useful for running tests, individual modules, and custom commands.

For example, I use the autopep8 library so I can keep my whitespacing beautiful without the elbow grease of adding extra lines manually, so I have this line in the [scripts] section of my Pipfile:

pep8 = "autopep8 -riv --max-line-length 150"
Enter fullscreen mode Exit fullscreen mode

With this, I can run pipenv run pep8 <file or directory name> and poof! one line at the end of each file, two lines between functions, one line between methods, etc. (Note: I do not actually think 150 is a good max line length, I just don't love where the tool auto-breaks lines, so I'd rather make those choices manually.)


As far as I can work out, anything that you could run in your terminal with your pipenv shell activated will work, with the notable exeption that you can't chain commands. I have not been able to get &&, ||, or ; to work. This doesn't really bother me, because if you really need to make a multiple command script, you can make an actual script file and run that from [scripts].

For example, I have a somewhat complex script running my test suite (mostly to add color) and I have a file in the scripts/ directory. This is what I have in my Pipfile:

tests = "./scripts/"
Enter fullscreen mode Exit fullscreen mode

I type pipenv run tests and so it does.

If you've not been using this feature of pipenv, give it a try! Let me know what you end up using it for =)

If you've not switch to using pipenv yet, consider this a bit of incentive. I just switched this project from requirements.txt and virtualenv/pyenv and it was not bad at all. It took a bit of persistence to hunt down all the necessary changes in our continuous integration / deployment system, but the changes themselves were not difficult.

Cheers 🥂🐍🙆🏻‍♀️

Top comments (6)

elanorigby profile image

Update! I submitted a PR with better documentation about this feature and it was accepted!

It's a pretty rad feeling to show someone the official documentation page and be like "These words right here I wrote them it was me."

Open source ftw 💗

tunaxor profile image
Angel Daniel Munoz Gonzalez

I love pipenv in general, I think it made me go back to enjoy some python development again, some time ago I switched to node, because of dependency management plus the language appealed to me, but since pipenv is in the scene it feels quite good

thanks for shedding some light into this feature!

motlib profile image

I like using pipenv, but didn't know that. Thanks for the post!

snregales profile image
Sharlon Regales • Edited

is it possible to use a placeholder in a custom script?
app="./ startapp <appname> src"

python -m pipenv run app account

this will run the following
./ start app account src

this will place the newly created app in the src directory

kylegalbraith profile image
Kyle Galbraith

Nice post Sharla. I am curious what your opinion is on using pipenv inside of VSCode. Have you had any issues when it comes to debugging?

elanorigby profile image

Sadly I can't really say; I've never used VSCode. I use Vim and occasionally PyCharm.