Menyimpan paket Python di Artifact Registry

Panduan memulai ini menunjukkan cara menyiapkan repositori Python Artifact Registry pribadi, mengupload paket, lalu menginstal paket.

Sebelum memulai

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Artifact Registry API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  9. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.

Meluncurkan Cloud Shell

Dalam panduan memulai ini, Anda akan menggunakan Cloud Shell, yang merupakan lingkungan shell untuk mengelola resource yang dihosting diGoogle Cloud.

Cloud Shell telah diinstal lebih dulu dengan Google Cloud CLI dan Python. gcloud CLI menyediakan antarmuka command line utama untuk Google Cloud.

Meluncurkan Cloud Shell:

  1. Buka konsol Google Cloud .

    Google Cloud console

  2. Di toolbar konsol Google Cloud , klik Activate Cloud Shell:

Sesi Cloud Shell akan terbuka di dalam frame yang lebih rendah di konsol. Anda menggunakan shell ini untuk menjalankan perintah gcloud.

Instal paket yang diperlukan

Twine adalah alat untuk memublikasikan paket Python. Anda akan menggunakan Twine untuk mengupload paket ke Artifact Registry.

Dalam panduan memulai ini, Anda akan menggunakan penginstalan Python yang disertakan dengan Cloud Shell. Penginstalan default ini mencakup backend keyring Artifact Registry: untuk menangani autentikasi dengan Artifact Registry. Jika Anda membuat lingkungan virtual atau menyiapkan Python di luar Cloud Shell, Anda harus menginstal backend keyring untuk autentikasi. Untuk mengetahui detailnya, lihat Mengautentikasi dengan keyring.

Untuk menginstal Twine, jalankan perintah:

pip install twine

Sekarang Anda siap menyiapkan Artifact Registry.

Membuat repositori

Buat repositori untuk paket Anda.

  1. Jalankan perintah berikut untuk membuat repositori paket Python baru di project saat ini bernama quickstart-python-repo di lokasi us-central1.

    gcloud artifacts repositories create quickstart-python-repo \
        --repository-format=python \
        --location=us-central1 \
        --description="Python package repository"
    
  2. Jalankan perintah berikut untuk memverifikasi bahwa repositori Anda telah dibuat:

    gcloud artifacts repositories list
    
  3. Untuk menyederhanakan perintah gcloud, tetapkan repositori default ke quickstart-python-repo dan lokasi default ke us-central1. Setelah nilai ditetapkan, Anda tidak perlu menentukannya dalam perintah gcloud yang memerlukan repositori atau lokasi.

    Untuk menyetel repositori, jalankan perintah:

    gcloud config set artifacts/repository quickstart-python-repo
    

    Untuk menyetel lokasi, jalankan perintah:

    gcloud config set artifacts/location us-central1
    

    Untuk mengetahui informasi selengkapnya tentang perintah ini, lihat dokumentasi gcloud config set.

Mengonfigurasi autentikasi

Backend keyring Artifact Registry menemukan kredensial Anda menggunakan Kredensial Default Aplikasi (ADC), sebuah strategi yang mencari kredensial di lingkungan Anda.

Dalam panduan memulai ini, Anda akan:

  • Buat kredensial pengguna untuk ADC. Di lingkungan produksi, Anda harus menggunakan akun layanan dan memberikan kredensial dengan variabel lingkungan GOOGLE_APPLICATION_CREDENTIALS.
  • Sertakan URL repositori Artifact Registry dalam perintah pip dan twine agar Anda tidak perlu mengonfigurasi pip dan Twine dengan URL repositori.

Untuk membuat kredensial untuk ADC, jalankan perintah berikut:

gcloud auth application-default login

Untuk mengetahui detail tentang metode autentikasi dan cara menambahkan repositori ke pip dan konfigurasi Twine, lihat Menyiapkan autentikasi ke repositori paket Python.

Mendapatkan paket contoh

Saat Anda membangun project Python, file distribusi disimpan di subdirektori dist dalam project Python Anda. Untuk menyederhanakan panduan memulai ini, Anda akan mendownload file paket yang telah dibuat sebelumnya.

  1. Buat folder project Python bernama python-quickstart.

    mkdir python-quickstart
    
  2. Buat subdirektori bernama dist, lalu pindah ke direktori tersebut.

    mkdir python-quickstart/dist
    cd python-quickstart/dist
    
  3. Download paket Python contoh yang digunakan dalam tutorial Panduan Pengguna Pengemasan Python Mengemas Project Python.

    pip download sampleproject
    

    Perintah ini akan mendownload paket sampleproject dan dependensinya, peppercorn.

Mengupload paket ke repositori

Gunakan Twine untuk mengupload paket ke repositori Anda.

  1. Dari direktori dist, ubah ke direktori python-quickstart induk.

    cd ..
    
  2. Upload paket ke repositori dari direktori dist Anda.

    python3 -m twine upload --repository-url https://us-central1-python.pkg.dev/PROJECT_ID/quickstart-python-repo/ dist/*
    

    Saat Anda menjalankan perintah dengan python3 -m, Python akan menemukan twine dan menjalankan perintah. Jika perintah twine ada di jalur sistem, Anda dapat menjalankannya tanpa python3 -m.

Twine mengupload sampleproject dan peppercorn ke repositori Anda.

Melihat paket di repositori

Untuk memverifikasi bahwa paket Anda telah ditambahkan, cantumkan paket di repositori quickstart-python-repo.

Jalankan perintah berikut:

gcloud artifacts packages list --repository=quickstart-python-repo

Untuk melihat versi paket, jalankan perintah berikut:

gcloud artifacts versions list --package=PACKAGE

Dengan PACKAGE adalah ID paket.

Menginstal paket

Jalankan perintah berikut untuk menginstal paket:

pip install --index-url https://us-central1-python.pkg.dev/PROJECT_ID/quickstart-python-repo/simple/ sampleproject

Pemecahan masalah

Secara default, alat seperti pip dan Twine tidak menampilkan pesan error mendetail. Jika Anda mengalami error, jalankan kembali perintah dengan tanda --verbose untuk mendapatkan output yang lebih mendetail. Lihat Pemecahan masalah untuk paket Python untuk mengetahui informasi selengkapnya.

Pembersihan

Agar akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan pada halaman ini, ikuti langkah-langkah berikut.

Sebelum menghapus repositori, pastikan semua paket yang ingin Anda simpan tersedia di lokasi lain.

Untuk menghapus repositori:

  1. Untuk menghapus repositori quickstart-python-repo, jalankan perintah berikut:

    gcloud artifacts repositories delete quickstart-python-repo
    
  2. Jika Anda ingin menghapus setelan repositori dan lokasi default yang Anda konfigurasi untuk konfigurasi gcloud yang aktif, jalankan perintah berikut:

    gcloud config unset artifacts/repository
    gcloud config unset artifacts/location
    

Langkah berikutnya