Isolating Python Tools

Python has a wonderful package and tool ecosystem, but installing things willy-nilly is a recipe for heartache down the road, as version conflicts start to come up.

Virtual Environments

Python tools are typically just packages with commands that are intended to be invoked from a command line. As such, you can isolate them in a virtual environment, just like any other installation.

The tool of choice for simple isolation is virtualenv, which you may already have included in your distribution.

An Example

Let's say that you want to improve your writing, and decide to use the handy proselint to do so.

Here is what that might look like.

# first off, let's create the virtual environment
# if you have virtualenv, run it this way
virtualenv plenv
# if not, you can invoke this directly with python3 - it has a good subset
python3 -m venv plenv

# now activate the virtual environment; this sets up env variables
# you can also not activate and use full path to executables - this also works
source plenv/bin/activate
# for Windows, use plenv\scripts\activate.bat

# install proselint - isolated to 'plenv'
python3 -m pip install proselint

# run it and rejoice
plenv/bin/proselint MY-FILE
# on Windows, you can also do this
python3 -m proselint MY-FILE

# leave the virtual environment if you activated it
deactivate

That's really all there is to it.

Happy linting!

Tags:  python

Home