Skip to main content

Working with PyPI

Publish PyPI Package​

  1. Edit $HOME/.pypirc to add a package repository like below:

    [distutils]
    index-servers=
    onedev

    [onedev]
    repository=https://onedev.example.com/<project path>/~pypi
    username=<onedev_account_name>
    password=<onedev_account_password>
    ParamExplanation
    <project path>path of a project with package management enabled
    <onedev_account_name>name of OneDev account with package write permission over the project
    <onedev_account_password>password or access token of OneDev account
  2. Build your Python distribution and upload it with Twine. For a project with pyproject.toml:

    python3 -m pip install build twine
    python3 -m build

    Then upload:

    $ python3 -m twine upload --repository onedev /path/to/files_to_upload
  3. For CI/CD job, run below to add package repository via command step

    # Use job token to tell OneDev the build publishing the package
    # Job secret 'access-token' should be defined in project build setting as an access token with package write permission

    cat << EOF > $HOME/.pypirc
    [distutils]
    index-servers=
    onedev

    [onedev]
    repository=https://onedev.example.com/<project path>/~pypi
    username=@job_token@
    password=@secret:access-token@
    EOF

    python3 -m build
    python3 -m twine upload --repository onedev dist/*
    ParamExplanation
    <project path>same as above

Consume Published PyPI Package​

Open the package detail page for its installation instructions. Use the registry's simple endpoint for pip:

python3 -m pip install --index-url https://onedev.example.com/<project-path>/~pypi/simple <package-name>==<version>

Authenticate with an account or token with package read permission, using a protected .netrc or your environment's credential mechanism. .pypirc configures Twine uploads; it does not configure pip authentication. Keep credential files out of source control.

Python package published by a CI build