how to setup set up your $HOME/.pypirc
The .pypirc
file is a configuration file used by Python's packaging tools, such as twine
and setuptools
, to store PyPI repository credentials. Setting up this file allows you to upload your packages to PyPI or other package indices smoothly. Here's how you can set up your $HOME/.pypirc
file:
The .pypirc
file typically contains sections for different PyPI repositories. Each section contains the URL of the repository and your credentials for accessing it. Below is an example structure of the .pypirc
file:
[distutils]
index-servers =
pypi
testpypi
[pypi]
repository = https://upload.pypi.org/legacy/
username = your-username
password = your-password
[testpypi]
repository = https://test.pypi.org/legacy/
username = your-username
password = your-password
.pypirc
Create the File: Open a terminal and use a text editor to create or edit the .pypirc
file in your home directory. For example, you can use nano
:
nano ~/.pypirc
Add Configuration: Copy the example structure above into your .pypirc
file.
Replace with Your Credentials:
your-username
with your actual PyPI username.your-password
with your PyPI password.Security Considerations:
Ensure the file permissions are set so that only you can read and write the file. You can set the permissions using:
chmod 600 ~/.pypirc
Consider using environment variables or tools like keyring
for storing passwords securely instead of saving them in plain text.
Test the Configuration:
You can test your configuration by attempting to upload a package using twine
:
twine upload dist/*
If you are testing, use the testpypi
repository to avoid uploading test packages to the official PyPI:
twine upload --repository testpypi dist/*
Token Authentication: PyPI now supports API tokens, which are more secure than using your password. You can replace the username
with __token__
and password
with your token:
[pypi]
repository = https://upload.pypi.org/legacy/
username = __token__
password = pypi-YourTokenHere
Environment Variables: You can also use environment variables in conjunction with .pypirc
for sensitive data like passwords or tokens to enhance security.
By following these steps, you should be able to successfully configure your .pypirc
file for uploading packages to PyPI or TestPyPI.