Creating a Distribution

Believe it or not, we are almost done with our plugin. From the command line, we can use this setup.py file to create a distribution:

 $ ​​cd​​ ​​/path/to/code/ch5/pytest-nice
 $ ​​python​​ ​​setup.py​​ ​​sdist
 running sdist
 running egg_info
 creating pytest_nice.egg-info
 ...
 running check
 creating pytest-nice-0.1.0
 ...
 creating dist
 Creating tar archive
 ...
 $ ​​ls​​ ​​dist
 pytest-nice-0.1.0.tar.gz

(Note that sdist stands for “source distribution.”)

Within pytest-nice, a dist directory contains a new file called pytest-nice-0.1.0.tar.gz. This file can now be used anywhere to install our plugin, even in place:

 $ ​​pip​​ ​​install​​ ​​dist/pytest-nice-0.1.0.tar.gz
 Processing ./dist/pytest-nice-0.1.0.tar.gz
 ...
 Installing collected packages: pytest-nice
 Successfully installed pytest-nice-0.1.0

However, you can put your .tar.gz files anywhere you’ll be able to get at them to use and share.

Distributing Plugins Through a Shared Directory

pip already supports installing packages from shared directories, so all we have to do to distribute our plugin through a shared directory is pick a location we can remember and put the .tar.gz files for our plugins there. Let’s say we put pytest-nice-0.1.0.tar.gz into a directory called myplugins.

To install pytest-nice from myplugins:

 $ ​​pip​​ ​​install​​ ​​--no-index​​ ​​--find-links​​ ​​myplugins​​ ​​pytest-nice

The --no-index tells pip to not go out to PyPI to look for what you want to install. The --find-links myplugins tells PyPI to look in myplugins for packages to install. And of course, pytest-nice is what we want to install.

If you’ve done some bug fixes and there are newer versions in myplugins, you can upgrade by adding --upgrade:

 $ ​​pip​​ ​​install​​ ​​--upgrade​​ ​​--no-index​​ ​​--find-links​​ ​​myplugins​​ ​​pytest-nice

This is just like any other use of pip, but with the --no-index --find-links myplugins added.

Distributing Plugins Through PyPI

If you want to share your plugin with the world, there are a few more steps we need to do. Actually, there are quite a few more steps. However, because this book isn’t focused on contributing to open source, I recommend checking out the thorough instruction found in the Python Packaging User Guide.[16]

When you are contributing a pytest plugin, another great place to start is by using the cookiecutter-pytest-plugin[17]:

 $ ​​pip​​ ​​install​​ ​​cookiecutter
 $ ​​cookiecutter​​ ​​https://github.com/pytest-dev/cookiecutter-pytest-plugin

This project first asks you some questions about your plugin. Then it creates a good directory for you to explore and fill in with your code. Walking through this is beyond the scope of this book; however, please keep this project in mind. It is supported by core pytest folks, and they will make sure this project stays up to date.