DEV Community

Sharla
Sharla

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 - https://pipenv.readthedocs.io/en/latest/advanced/#custom-script-shortcuts

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:

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

You can type in your terminal to run:

    $ pipenv run printfoo
    foo
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.)

Caveats

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 test.sh file in the scripts/ directory. This is what I have in my Pipfile:

[scripts]
tests = "./scripts/test.sh"
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)

Collapse
 
elanorigby profile image
Sharla

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 πŸ’—

Collapse
 
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!

Collapse
 
motlib profile image
Motlib

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

Collapse
 
snregales profile image
Sharlon Regales • Edited

is it possible to use a placeholder in a custom script?
example:
[scripts]
app="./manage.py startapp <appname> src"

python -m pipenv run app account

this will run the following
./manage.py start app account src

this will place the newly created app in the src directory

Collapse
 
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?

Collapse
 
elanorigby profile image
Sharla

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