Examples of working with PyPI registries
This section provides step-by-step examples for different types of PyPI registries.
Creating and using registries
Local registry
-
Create a registry:
yc cloud-registry registry create \ --name my-pypi-local \ --registry-kind pypi \ --registry-type local \ --description "Local PyPI registry"Result:
id: e5o6a2blpkb6******** name: my-pypi-local kind: PYPI type: LOCAL -
Create a package:
mkdir -p my_package/my_package cd my_packageCreate a file named
my_package/__init__.py:def hello(): return "Hello from my package!"Create a
pyproject.toml:[build-system] requires = ["setuptools"] [project] name = "my_package" version = "0.0.1" authors = [{ name="Example Author", email="author@example.com" }] description = "A small example package" readme = "README.md" requires-python = ">=3.9" classifiers = [ "Programming Language :: Python :: 3", "Operating System :: OS Independent", ]Create a
README.md:# My Package This is an example of a Python package. -
Set up authentication:
Create a
~/.pypirc:[distutils] index-servers = cloud-registry [cloud-registry] repository = https://registry.yandexcloud.net/pypi/e5o6a2b********/legacy/ username = iam password = <IAM_token> -
Build and publish the package:
python3 -m venv my-venv && source my-venv/bin/activate python3 -m pip install build twine python3 -m build python3 -m twine upload --repository cloud-registry dist/* -
Install a package:
pip install my_package \ --index-url https://iam:$(yc iam create-token)@registry.yandexcloud.net/pypi/e5o6a2blpkb6********/simple/ \ --no-cacheCheck:
python3 -c "from my_package import hello; print(hello())" # Hello from my package!
Remote registry
-
Create a registry:
yc cloud-registry registry create \ --name my-pypi-remote \ --registry-kind pypi \ --registry-type remote \ --description "Remote PyPI proxy" \ --properties source=@pypiResult:
id: e5o7b3cmqlc7******** name: my-pypi-remote kind: PYPI type: REMOTE -
Install the package (first time):
When first installed, the package is uploaded from the public PyPI and stored in the cache:
pip install pytest==9.0.1 \ --index-url https://iam:$(yc iam create-token)@registry.yandexcloud.net/pypi/e5o7b3cmqlc7********/simple/ \ --no-cache -
Install the package (once more):
When reinstalling, the package is uploaded from the cache:
pip install pytest==9.0.1 \ --index-url https://iam:$(yc iam create-token)@registry.yandexcloud.net/pypi/e5o7b3cmqlc7********/simple/ \ --no-cache -
Install another package:
pip install requests==2.31.0 \ --index-url https://iam:$(yc iam create-token)@registry.yandexcloud.net/pypi/e5o7b3cmqlc7********/simple/ \ --no-cache
Note
All installed packages are cached in the registry. On next installation they will be uploaded from the cache.
Virtual registry
-
Create a registry:
Merge the local (
cn1mfr50********) and remote (cn1ihsdl********) registries:yc cloud-registry registry create \ --name my-pypi-virtual \ --description "Virtual registry" \ --registry-kind pypi \ --registry-type virtual \ --properties "readOnly=false,deploymentRegistryId=cn1mfr50********,registryIds=cn1mfr50********;cn1ihsdl********"Result:
id: e5o9d5eosne9******** name: my-pypi-virtual kind: PYPI type: VIRTUAL -
Install the internal package:
The virtual registry first searches the local registry:
pip install my_package \ --index-url https://iam:$(yc iam create-token)@registry.yandexcloud.net/pypi/e5o9d5eosne9********/simple/ \ --no-cache -
Install the public package:
If the package is not found in the local registry, searches the remote one:
pip install pytest==9.0.1 \ --index-url https://iam:$(yc iam create-token)@registry.yandexcloud.net/pypi/e5o9d5eosne9********/simple/ \ --no-cache -
Install multiple packages:
pip install my_package pytest==9.0.1 requests==2.31.0 \ --index-url https://iam:$(yc iam create-token)@registry.yandexcloud.net/pypi/e5o9d5eosne9********/simple/ \ --no-cache