Prevent accidental publishing of a private Python package
It may occur that you have a Python package which for one reason or another is private and you don’t want someone to accidentally publish it to PyPi which is a public public repository of Python packages.
This can be achieved in many different ways. Two of the simplest ways to do that are documented bellow. Some of you might argue that those options are ugly, but they are simple and do the trick.
Option 1 - Disallowing “register” and “upload” command
This options simply prevents a user from calling setup.py
with register
and
/ or upload
argument.
For example:
To use it, simply put the following snippet in your setup.py
before the
setup
function call.
Option 2 - Forcing a custom PyPi repository
In some cases you don’t want to fully prevent publishing, but you only want to allow users to publish packages to your private PyPi repository.
You can do that and prevent accidental publishing to a public repository, by
simply injecting --repository=<your repository name>
option to the
register
and upload
command.
Here is the code snippet which accomplishes that:
Same as above, to use it, simply put it in your setup.py
file before the
setup
function call.
Keep in mind that you need to have an entry with key private_repository_1
in
your ~/.pypirc
config file for this code to work. If you don’t have it, you
will receive an error similar to the one bellow:
You can find instructions which show how to add a new entry to your config file here.