Python裂变创建包和导入库

2024-10-03 09:15:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我是裂变新手,我创建了一个裂变函数,将一个元素插入到甲骨文中。因为它是python,所以我需要安装cx_oracle。我遵循了working with dependencies.上的文档页面,基本上我需要在一个包中提供一个requirements.txt文件、一个shell脚本和我的python代码,该包将由构建器部署。这里的问题是,包的状态为running,而不是successfull。我需要这方面的帮助,我找不到任何关于如何使用cx_oracle或任何其他依赖项部署python裂变的示例

下面是我的代码、需求文件、脚本和目录

import cx_Oracle

def main():
    try:
        sql = 'INSERT INTO MYTABLE(NID,CRT_DATE,INDICATOR) VALUES (0,SYSDATE,1)';
        con = cx_Oracle.connect('oracle/oracle@localhost/orcl',encoding="UTF-8", nencoding="UTF-8")
        cur = con.cursor()
        cur.execute(sql)
        cur.close()
        con.commit()
        return 'successfully inserted on db'
    except Exception as e:
        return str(e)

脚本:

#!/bin/sh
pip3 install -r ${SRC_PKG}/requirements.txt -t ${SRC_PKG} && cp -r ${SRC_PKG} ${DEPLOY_PKG}

文件夹结构(zip文件)

./insertintodb/requirements.txt
./insertintodb/__init__.py
./insertintodb/build.sh
./insertintodb/insert_into_db.py

这就是我得到的结果

>>fission pkg info --name insertintodb-zip-3cnd
Name:        insertintodb-zip-3cnd
Environment: python
Status:      running
Build Logs:

更新:

我一开始就犯了这个错误

Building wheels for collected packages: cx-Oracle
  Building wheel for cx-Oracle (PEP 517): started
  Building wheel for cx-Oracle (PEP 517): finished with status 'done'
  Created wheel for cx-Oracle: filename=cx_Oracle-8.1.0-cp37-cp37m-linux_x86_64.whl size=703054 sha256=a1a93a38f4da072ba7ca654c27e27eacb238067d0ebe7a43e9cec8fc8147e682
  Stored in directory: /root/.cache/pip/wheels/2d/bf/23/ca09f9d03408edc72ba4d2870ce1f0362e5b218df46e159f78
Successfully built cx-Oracle
Installing collected packages: cx-Oracle
Successfully installed cx-Oracle-8.1.0
WARNING: You are using pip version 20.2.3; however, version 21.0.1 is available.
You should consider upgrading via the '/usr/bin/python3.7 -m pip install --upgrade pip' command.
Error uploading deployment package: Internal error - error uploading zip file: Post http://storagesvc.fission/v1/archive: dial tcp 10.152.183.49:80: i/o timeout

我注意到pod storagesvc被标记为挂起。因此,我使用values.yaml文件和Helm配置了一个存储类,从而使它能够运行。然后包最终成功构建,但现在我无法执行该函数

这是我的包输出

Name:        insertintodb-zip-ttr2
Environment: python
Status:      succeeded
Build Logs:
Collecting cx-Oracle==8.1.0
  Downloading cx_Oracle-8.1.0.tar.gz (343 kB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
    Preparing wheel metadata: started
    Preparing wheel metadata: finished with status 'done'
Building wheels for collected packages: cx-Oracle
  Building wheel for cx-Oracle (PEP 517): started
  Building wheel for cx-Oracle (PEP 517): finished with status 'done'
  Created wheel for cx-Oracle: filename=cx_Oracle-8.1.0-cp37-cp37m-linux_x86_64.whl size=703031 sha256=468676055d1c632b539fe7f99577e87b8702e19c7272b743e6bb946fd2626fd5
  Stored in directory: /root/.cache/pip/wheels/2d/bf/23/ca09f9d03408edc72ba4d2870ce1f0362e5b218df46e159f78
Successfully built cx-Oracle
Installing collected packages: cx-Oracle
Successfully installed cx-Oracle-8.1.0
WARNING: You are using pip version 20.2.3; however, version 21.0.1 is available.
You should consider upgrading via the '/usr/bin/python3.7 -m pip install --upgrade pip' command.

在裂变页面上的教程之后,我创建了入口点

fission fn create --name insertintodb --pkg insertintodb-zip-ttr2 --entrypoint "insertintodb.main"

然而,现在我尝试测试函数,我得到模块未找到错误

fission fn test --name insertintodb

这是错误跟踪。我是不是遗漏了什么

Error: Error calling function insertintodb: 500; Please try again or fix the error: 
Error: Error getting function logs from controller: error getting logs from controller, status code: '500'. Try to get logs from log database.

[2021-03-09 00:43:43.893591369 +0000 UTC] 2021-03-08T21:14:51.630753375-03:00 stderr F [2021-03-09 00:14:51,630] INFO in server: Starting bjoern based server
[2021-03-09 00:43:43.893594877 +0000 UTC] 2021-03-08T21:14:51.630783769-03:00 stdout F 2021-03-09 00:14:51,630 - INFO - Starting bjoern based server
[2021-03-09 00:43:43.893596001 +0000 UTC] 2021-03-08T21:43:41.390127616-03:00 stderr F [2021-03-09 00:43:41,383] INFO in server: /v2/specialize called with  filepath = \"/userfunc/deployarchive\"   handler = \"insert_into_db.main\"
[2021-03-09 00:43:43.89359711 +0000 UTC] 2021-03-08T21:43:41.390145668-03:00 stderr F [2021-03-09 00:43:41,384] DEBUG in server: moduleName = \"insert_into_db\"    funcName = \"main\"
[2021-03-09 00:43:43.893598047 +0000 UTC] 2021-03-08T21:43:41.390151953-03:00 stderr F [2021-03-09 00:43:41,384] DEBUG in server: __package__ = \"None\"
[2021-03-09 00:43:43.893599028 +0000 UTC] 2021-03-08T21:43:41.390159305-03:00 stderr F [2021-03-09 00:43:41,386] ERROR in app: Exception on /v2/specialize [POST]
[2021-03-09 00:43:43.893599959 +0000 UTC] 2021-03-08T21:43:41.390166007-03:00 stderr F Traceback (most recent call last):
[2021-03-09 00:43:43.893600959 +0000 UTC] 2021-03-08T21:43:41.39016734-03:00 stdout F 2021-03-09 00:43:41,383 - INFO - /v2/specialize called with  filepath = \"/userfunc/deployarchive\"   handler = \"insert_into_db.main\"
[2021-03-09 00:43:43.893601949 +0000 UTC] 2021-03-08T21:43:41.390180835-03:00 stdout F 2021-03-09 00:43:41,384 - DEBUG - moduleName = \"insert_into_db\"    funcName = \"main\"
[2021-03-09 00:43:43.893602866 +0000 UTC] 2021-03-08T21:43:41.390189054-03:00 stdout F 2021-03-09 00:43:41,384 - DEBUG - __package__ = \"None\"
[2021-03-09 00:43:43.893603897 +0000 UTC] 2021-03-08T21:43:41.390193619-03:00 stdout F 2021-03-09 00:43:41,386 - ERROR - Exception on /v2/specialize [POST]
[2021-03-09 00:43:43.893604848 +0000 UTC] 2021-03-08T21:43:41.390197682-03:00 stdout F Traceback (most recent call last):
[2021-03-09 00:43:43.893605786 +0000 UTC] 2021-03-08T21:43:41.390201317-03:00 stdout F   File \"/usr/lib/python3.8/site-packages/flask/app.py\", line 2317, in wsgi_app
[2021-03-09 00:43:43.893606715 +0000 UTC] 2021-03-08T21:43:41.390171938-03:00 stderr F   File \"/usr/lib/python3.8/site-packages/flask/app.py\", line 2317, in wsgi_app
[2021-03-09 00:43:43.893607618 +0000 UTC] 2021-03-08T21:43:41.390213555-03:00 stderr F     response = self.full_dispatch_request()
[2021-03-09 00:43:43.893608668 +0000 UTC] 2021-03-08T21:43:41.390221357-03:00 stderr F   File \"/usr/lib/python3.8/site-packages/flask/app.py\", line 1840, in full_dispatch_request
[2021-03-09 00:43:43.89360962 +0000 UTC] 2021-03-08T21:43:41.39022746-03:00 stderr F     rv = self.handle_user_exception(e)
[2021-03-09 00:43:43.893610557 +0000 UTC] 2021-03-08T21:43:41.390233197-03:00 stderr F   File \"/usr/lib/python3.8/site-packages/flask/app.py\", line 1743, in handle_user_exception
[2021-03-09 00:43:43.893611516 +0000 UTC] 2021-03-08T21:43:41.390239202-03:00 stderr F     reraise(exc_type, exc_value, tb)
[2021-03-09 00:43:43.893612466 +0000 UTC] 2021-03-08T21:43:41.390286169-03:00 stderr F   File \"/usr/lib/python3.8/site-packages/flask/_compat.py\", line 36, in reraise
[2021-03-09 00:43:43.893613457 +0000 UTC] 2021-03-08T21:43:41.390295156-03:00 stderr F     raise value
[2021-03-09 00:43:43.893614406 +0000 UTC] 2021-03-08T21:43:41.39030263-03:00 stderr F   File \"/usr/lib/python3.8/site-packages/flask/app.py\", line 1838, in full_dispatch_request
[2021-03-09 00:43:43.893615396 +0000 UTC] 2021-03-08T21:43:41.390308483-03:00 stderr F     rv = self.dispatch_request()
[2021-03-09 00:43:43.893616328 +0000 UTC] 2021-03-08T21:43:41.390314229-03:00 stderr F   File \"/usr/lib/python3.8/site-packages/flask/app.py\", line 1824, in dispatch_request
[2021-03-09 00:43:43.893617322 +0000 UTC] 2021-03-08T21:43:41.390319231-03:00 stderr F     return self.view_functions[rule.endpoint](**req.view_args)
[2021-03-09 00:43:43.893618267 +0000 UTC] 2021-03-08T21:43:41.390325285-03:00 stderr F   File \"server.py\", line 97, in loadv2
[2021-03-09 00:43:43.893619201 +0000 UTC] 2021-03-08T21:43:41.390205503-03:00 stdout F     response = self.full_dispatch_request()
[2021-03-09 00:43:43.893620159 +0000 UTC] 2021-03-08T21:43:41.390350669-03:00 stdout F   File \"/usr/lib/python3.8/site-packages/flask/app.py\", line 1840, in full_dispatch_request
[2021-03-09 00:43:43.893621096 +0000 UTC] 2021-03-08T21:43:41.390357341-03:00 stdout F     rv = self.handle_user_exception(e)
[2021-03-09 00:43:43.893622 +0000 UTC] 2021-03-08T21:43:41.390362677-03:00 stdout F   File \"/usr/lib/python3.8/site-packages/flask/app.py\", line 1743, in handle_user_exception
[2021-03-09 00:43:43.893622938 +0000 UTC] 2021-03-08T21:43:41.39036768-03:00 stdout F     reraise(exc_type, exc_value, tb)
[2021-03-09 00:43:43.893623832 +0000 UTC] 2021-03-08T21:43:41.390373414-03:00 stdout F   File \"/usr/lib/python3.8/site-packages/flask/_compat.py\", line 36, in reraise
[2021-03-09 00:43:43.893624759 +0000 UTC] 2021-03-08T21:43:41.390379066-03:00 stdout F     raise value
[2021-03-09 00:43:43.893625668 +0000 UTC] 2021-03-08T21:43:41.390384622-03:00 stdout F   File \"/usr/lib/python3.8/site-packages/flask/app.py\", line 1838, in full_dispatch_request
[2021-03-09 00:43:43.893626604 +0000 UTC] 2021-03-08T21:43:41.390389855-03:00 stdout F     rv = self.dispatch_request()
[2021-03-09 00:43:43.893627514 +0000 UTC] 2021-03-08T21:43:41.390395064-03:00 stdout F   File \"/usr/lib/python3.8/site-packages/flask/app.py\", line 1824, in dispatch_request
[2021-03-09 00:43:43.89362843 +0000 UTC] 2021-03-08T21:43:41.390400097-03:00 stdout F     return self.view_functions[rule.endpoint](**req.view_args)
[2021-03-09 00:43:43.89362934 +0000 UTC] 2021-03-08T21:43:41.390422367-03:00 stdout F   File \"server.py\", line 97, in loadv2
[2021-03-09 00:43:43.893630288 +0000 UTC] 2021-03-08T21:43:41.390428578-03:00 stdout F     mod = importlib.import_module(moduleName)
[2021-03-09 00:43:43.893631167 +0000 UTC] 2021-03-08T21:43:41.39043377-03:00 stdout F   File \"/usr/lib/python3.8/importlib/__init__.py\", line 127, in import_module
[2021-03-09 00:43:43.893632536 +0000 UTC] 2021-03-08T21:43:41.390438601-03:00 stdout F     return _bootstrap._gcd_import(name[level:], package, level)
[2021-03-09 00:43:43.893633547 +0000 UTC] 2021-03-08T21:43:41.390443485-03:00 stdout F   File \"<frozen importlib._bootstrap>\", line 1014, in _gcd_import
[2021-03-09 00:43:43.893634518 +0000 UTC] 2021-03-08T21:43:41.390448327-03:00 stdout F   File \"<frozen importlib._bootstrap>\", line 991, in _find_and_load
[2021-03-09 00:43:43.893635457 +0000 UTC] 2021-03-08T21:43:41.390453255-03:00 stdout F   File \"<frozen importlib._bootstrap>\", line 975, in _find_and_load_unlocked
[2021-03-09 00:43:43.893636349 +0000 UTC] 2021-03-08T21:43:41.390509883-03:00 stdout F   File \"<frozen importlib._bootstrap>\", line 671, in _load_unlocked
[2021-03-09 00:43:43.893637295 +0000 UTC] 2021-03-08T21:43:41.390517758-03:00 stdout F   File \"<frozen importlib._bootstrap_external>\", line 783, in exec_module
[2021-03-09 00:43:43.893638218 +0000 UTC] 2021-03-08T21:43:41.390524689-03:00 stdout F   File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed
[2021-03-09 00:43:43.893639149 +0000 UTC] 2021-03-08T21:43:41.390529847-03:00 stdout F   File \"/userfunc/deployarchive/insert_into_db.py\", line 3, in <module>
[2021-03-09 00:43:43.893640105 +0000 UTC] 2021-03-08T21:43:41.390534764-03:00 stdout F     import cx_Oracle
[2021-03-09 00:43:43.893641022 +0000 UTC] 2021-03-08T21:43:41.390539526-03:00 stdout F ModuleNotFoundError: No module named 'cx_Oracle'
[2021-03-09 00:43:43.893641958 +0000 UTC] 2021-03-08T21:43:41.390331203-03:00 stderr F     mod = importlib.import_module(moduleName)
[2021-03-09 00:43:43.893642866 +0000 UTC] 2021-03-08T21:43:41.390568904-03:00 stderr F   File \"/usr/lib/python3.8/importlib/__init__.py\", line 127, in import_module
[2021-03-09 00:43:43.893643802 +0000 UTC] 2021-03-08T21:43:41.390574902-03:00 stderr F     return _bootstrap._gcd_import(name[level:], package, level)
[2021-03-09 00:43:43.893644732 +0000 UTC] 2021-03-08T21:43:41.390580247-03:00 stderr F   File \"<frozen importlib._bootstrap>\", line 1014, in _gcd_import
[2021-03-09 00:43:43.893645659 +0000 UTC] 2021-03-08T21:43:41.390585315-03:00 stderr F   File \"<frozen importlib._bootstrap>\", line 991, in _find_and_load
[2021-03-09 00:43:43.893646583 +0000 UTC] 2021-03-08T21:43:41.390590706-03:00 stderr F   File \"<frozen importlib._bootstrap>\", line 975, in _find_and_load_unlocked

Tags: inpyapplibpackagesusrstderrstdout