在不激活虚拟环境的情况下运行Python3 [英] Run Python3 without activating the virtual environment
问题描述
我的目标是在AWS Lambda服务上运行Python 3代码,该服务目前仅支持Python 2.7.这些是我已经完成的步骤.
My objective is to run Python 3 code on the AWS Lambda Service, which currently only supports Python 2.7. These are the steps I have done.
-
由于我在Mac上工作,因此请设置与相似的 AWS Lambda Linux实例.
Since I work on a Mac, setup a docker image similar to the AWS Lambda Linux instance.
在Docker映像上从源代码构建 Python3.
Build Python3 from source on the docker image.
在docker映像中创建一个虚拟环境并将其复制到我的项目中.
In the docker image create a virtual environment and copy it to my project.
AWS Lambda要求您创建代码的zip并将其上传到他们的服务.对于这个原型,我有一个拉链,其根部有三个工件
AWS Lambda requires you to create a zip of the code and upload it to their service. For this prototype, I have a zip with three artifacts at the root
-
handler.py
:这是一个Python 2.7文件.发生事件时(例如,在S3存储桶中创建新文件时),AWS Lambda服务将执行此文件中的handler
函数.
handler.py
: This is a Python 2.7 file. Thehandler
function in this file will be executed by the AWS Lambda Service when an event occurs (e.g. When a new file is created in a S3 bucket).
def handler(event, context):
execution_uuid = uuid.uuid4()
commands = '''
source venv/bin/activate && venv/bin/python3.6 ./handler_python3.py --execution_uuid {ex_uuid}
'''.format(ex_uuid=str(execution_uuid))
p = Popen('/bin/bash', shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
stdout, stderr = p.communicate(commands)
pprint(stdout)
pprint(stderr)
handler_python3.py
.这是先前的handler.py
文件调用的Python3文件.注意正在读取execution_uuid
.为了简洁起见,我已经删除了使用它的代码,但是我确实需要它,并且我正在使用argparse
提取它.
handler_python3.py
. This is the Python3 file called by the earlier handler.py
file. Note the execution_uuid
being read. I have taken out the code that uses it for brevity but I do need it and I am using argparse
to extract it.
def read_execution_uuid():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--execution_uuid", required=True)
args = parser.parse_args()
return args.execution_uuid
def handler(event, context):
import sys
print(sys.path)
if __name__ == '__main__':
execution_uuid = read_execution_uuid()
handler(event, context)
-
venv
文件夹.这是我从docker映像复制的虚拟环境文件夹.
venv
folder. This is the virtual environment folder that I copied from the docker image.
运行AWS Lambda服务时,出现以下错误
When I run the AWS Lambda Service, I get the following error
Traceback (most recent call last):
File "./handler_python3.py", line 38, in <module>
execution_uuid = read_execution_uuid()
File "./handler_python3.py", line 7, in read_execution_uuid
import argparse
ModuleNotFoundError: No module named \'argparse\'
注意:
-
如果我删除了
argparse
代码并执行了handler_python3.py
中的handler
函数,它将显示sys.path
If I remove the
argparse
code and thehandler
function inhandler_python3.py
executes, it shows the following values forsys.path
['/var/task', '/var/runtime', '/var/task/venv/lib/python36.zip', '/var/task/venv/lib/python3.6', '/var/task/venv/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6', '/var/task/venv/lib/python3.6/site-packages']
注意:
- 我可以显式安装argparse.但是我不想.
- 请注意python 2.7文件
handler.py
中的source venv/bin/activate
命令.尽管它在本地工作,但不适用于Lambda实例.
- I can install argparse explicitly. But I'd like not to.
- Note the
source venv/bin/activate
command in the python 2.7 filehandler.py
. That doesn't work on the Lambda instance though it works locally.
推荐答案
创建虚拟环境不会复制/usr/local/lib/python3.6
目录中的所有模块.我必须将所有文件复制到那里.
Creating a virtual environment does not copy all the modules from the /usr/local/lib/python3.6
directory. I had to copy all the files there.
这篇关于在不激活虚拟环境的情况下运行Python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!