تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
هناك طريقتان لتحديد التبعيات الخاصة بـ Cloud Functions المكتوبة بلغة Python: استخدام ملف requirements.txt الخاص بأداة إدارة الحزم pip أو تجميع التبعيات المحلية مع الدالة.
لا تتوافق مواصفات التبعية التي تستخدم معيار Pipfile/Pipfile.lock مع هذه الميزة. يجب ألا يتضمّن مشروعك هذه الملفات.
تحديد التبعيات باستخدام pip
تتم إدارة التبعيات في Python باستخدام pip ويتم التعبير عنها في ملف بيانات وصفية يُسمى requirements.txt.
يجب أن يكون هذا الملف في الدليل نفسه الذي يتضمّن ملف main.py الذي يحتوي على رمز الدالة.
عند نشر الدالة أو إعادة نشرها، تستخدم Cloud Functions أداة pip لتنزيل أحدث إصدار من التبعيات وتثبيته كما هو موضّح في ملف requirements.txt.
يحتوي الملف requirements.txt على سطر واحد لكل حزمة. يحتوي كل سطر على اسم الحزمة، ويمكن أن يحتوي أيضًا على الإصدار المطلوب. لمزيد من التفاصيل، يُرجى الاطّلاع على requirements.txt.
لمنع تأثُّر الإصدار بتغييرات إصدار الحزمة التابعة، ننصحك بتثبيت حِزمك التابعة على إصدار معيّن.
في ما يلي مثال على ملف requirements.txt:
functions-framework
requests==2.20.0
numpy
تجميع المهام التابعة المحلية
يمكنك أيضًا تجميع التبعيات ونشرها مع الدالة. يكون هذا الأسلوب مفيدًا إذا لم تكن التبعية متاحة من خلال أداة إدارة الحِزم pip أو إذا كان الوصول إلى الإنترنت في بيئة Cloud Functions محدودًا.
على سبيل المثال، يمكنك استخدام بنية دليل مثل ما يلي:
يمكنك بعد ذلك استيراد الرمز كالمعتاد من localpackage باستخدام عبارة import التالية.
# Code in main.py
from localpackage import script
يُرجى العِلم أنّ هذا الأسلوب لن يشغّل أي ملفات setup.py. سيظلّ بإمكانك تجميع الحِزم التي تتضمّن هذه الملفات، ولكن قد لا تعمل بشكلٍ صحيح على Cloud Functions.
تاريخ التعديل الأخير: 2025-09-06 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-09-06 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["\u003cbr /\u003e\n\nNode.js Python \n\n\u003cbr /\u003e\n\nThere are two ways to specify dependencies for Cloud Functions written in\nPython: using the [pip](https://pip.pypa.io/en/stable/) package manager's\n`requirements.txt` file or packaging local dependencies alongside your function.\n\nDependency specification using the Pipfile/Pipfile.lock standard is\nnot supported. Your project should not include these files.\n\nSpecifying dependencies with pip\n\nDependencies in Python are managed with pip and expressed in a metadata file\ncalled\n[`requirements.txt`](https://pip.pypa.io/en/stable/user_guide/#requirements-files).\nThis file must be in the same directory as the `main.py` file that contains your\nfunction code.\n\nWhen you deploy or redeploy your function, Cloud Functions\nuses pip to download and install the latest version of your\ndependencies as declared in the `requirements.txt` file.\nThe `requirements.txt` file contains one line per package. Each line contains\nthe package name, and optionally, the requested version. For more details, see\nthe [`requirements.txt`\nreference](https://pip.pypa.io/en/stable/user_guide/#requirements-files).\n\nTo prevent your build from being affected by dependency version changes,\nconsider pinning your dependency packages to a specific version.\n\nThe following is an example `requirements.txt` file: \n\n```\nfunctions-framework\nrequests==2.20.0\nnumpy\n```\n\nPackaging local dependencies\n\nYou can also package and deploy dependencies alongside your function. This\napproach is useful if your dependency is not available via the pip\npackage manager or if your Cloud Functions environment's internet\naccess is restricted.\n| **Note:** You can still use a `requirements.txt` file to specify additional dependencies you haven't packaged alongside your function.\n\nFor example, you might use a directory structure\nsuch as the following: \n\n```\nmyfunction/\n├── main.py\n└── localpackage/\n ├── __init__.py\n └── script.py\n```\n\nYou can then import the code as usual from `localpackage` using the following\n`import` statement. \n\n```py\n# Code in main.py\nfrom localpackage import script\n```\n\nNote that this approach will *not* run any `setup.py` files. Packages with those\nfiles can still be bundled, but may not run correctly on Cloud Functions."]]