Run python zip application in memory?

Viewed 36

In python is possible to run zip application if "main.py" inside a zip. App structure:

myapp.zyp
|
|- Class1
|   |- module.py
|
|-__main__.py

And you can run python3 myapp.zip

My idea is download zip from request or AWS S3 and run code in memory without save.

I'm try run:

import boto3
import io
from zipfile import ZipFile

s3 = boto3.resource(
        service_name = 's3',
        region_name = 'my-region',
        aws_access_key_id = 'my-access',
        aws_secret_access_key = 'my-secret'
    )
my_bucket = s3.Bucket('my-bucket-name')
object = my_bucket.Object('myapp.zip')

# try run in memory
with io.BytesIO(object.get()['Body'].read()) as app:

zip_file = ZipFile(app, "r")

for script_file in zip_file.namelist():
        exec(zip_file.read(script_file))

But an error occur:

NameError Traceback (most recent call last)
      5 for script_file in zip_file.namelist():
----> 6     exec(zip_file.read(script_file))

NameError: name '__file__' is not defined
1 Answers

No. Python cannot natively consume zip files directly. You will need to download the files, unzip them, then run the python command you're looking for.

Related