This is a tool for writing python virtuously. Support linting, testing, deploying and good maintenance.
This is a script to help you be virtuous when writing and running Python programs.
I had just come back to python after not using it for a while, and I was annoyed wiith having to set up a virtualenv and activate it for each project. So I wrote a script to make that easier. Mostly because I didn't want to have to remember the steps and commands, and I didn't want to have to remember to pip install or freeze my requirements. (Edit: no longer using pip freeze!)
This script is like a cross between a wrapper and a makefile. It runs your program, but it also creates, activates and cleans up a virtualenv around it. It can run a linter, test suite, and auto-format your code as well. And it can be used to run things in production. (I recommend Docker instead though.)
You can use this in your CI pipeline to run lint and tests for every branch!
- Copy the
run
script to your project directory. - Edit your requirements.txt and add your dependencies.
- Call
run
with your main file as the first argument:
./run my-app.py arg1 arg2 arg3
- Profit!
The run
script has some options which help you write clean code.
--lint
- Run a linter and output warnings about your code. This uses flake8 to give warnings for code not following PEP8 conventions.--test
- Run pytest to evaluate all your tests.--format <path>
- Auto-format your code with autopep8 to fix all your annoying little whitespace inconsistencies.--clean
- removes virtualenv directories so you can re-install all your dependencies.- Without the above options, it runs any python program you tell it.
- Simplifies your development workflow.
- Manages your virtualenv directories for the project so you don't have to.
- Provides built-in configuration for flake8 (lint) and pytest.
- Creates a
.gitignore
for python if you don't already have one. - Automatically installs your requirements and updates your
requirements.txt
.
First I copy run
to my project, either a legacy project or a new one. Then I
follow these steps:
./run my-project-main.py with command line args
- When I run, it sets up my virtualenv, activates it for me, and pip installs my requirements. I don't have to think about that stuff. I see my code is working or I find a bug which I need to fix.- Write code to fix a bug or add a feature.
- Add pytest doctests in the code.
./run --test
- Run all my tests, and see if they pass or fail. Fix and repeat../run --format . && ./run --lint
- Then I fix any lint warnings. Repeat until there are no more warnings.- Repeat until happy.
- Commmit all code and requirements.txt to git.
Just clone this repo and run with it.
- Clone this repo
./run --test