Creating a PEX of a small flask website
Asked Answered
B

1

11

I am attempting to create a PEX file of a simple 3 page flask website. The website works with Gunicorn on python3, the client wants the site compiled to a encapsulated pex file.

pex -r zb1/requirements.txt -e zb1/run -o zb1.pex

also tried

pex -r zb1/requirements.txt -m zb1:run -o zb1.pex

When I run that command it compiles just fine but the trouble is when I attempt to run a pex file.

c:~ c$ ./zb1.pex 
Traceback (most recent call last):
  File ".bootstrap/_pex/pex.py", line 326, in execute
  File ".bootstrap/_pex/pex.py", line 258, in _wrap_coverage
  File ".bootstrap/_pex/pex.py", line 290, in _wrap_profiling
  File ".bootstrap/_pex/pex.py", line 369, in _execute
  File ".bootstrap/_pex/pex.py", line 427, in execute_entry
  File ".bootstrap/_pex/pex.py", line 432, in execute_module
  File "/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/runpy.py", line 192, in run_module
    mod_name, mod_spec, code = _get_module_details(mod_name)
  File "/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/runpy.py", line 127, in _get_module_details
    raise error("No module named %s" % mod_name)
ImportError: No module named zb1/run

I have tried different variations of the pex commands. This is on MacOSX El Capitan with python2.7 and python3.5

Thank you in advance and let me know if you need any extra information.

Project Structure

zb1
├── README.md
├── __init__.py
├── application
│   ├── config.py
│   ├── static
│   ├── templates
│   ├── uploads
│   ├── utilities
│   └── zbo
├── buildup.py
├── env
├── requirements.txt
├── run.py  << ENTRY POINT!
└── setup.sh
Bailsman answered 15/9, 2016 at 20:10 Comment(0)
G
4

The commands you are running are only including the requirements for your project, they don't actually instruct Pex to include your code. To get your code inside the Pex executable you need to first package it and then instruct Pex to include that package.

(for whatever reason Pex did not like the name zb1 so I used mypackage instead)

1. Package your code:

Create a setup.py file at the top level:

#!/usr/bin/env python

from setuptools import setup

setup(
    name='mypackage',
    packages = ['mypackage'],
    version='1.0.0')

Move your code to a subdirectory with the same name as your package:

mypackage
├── mypackage
│   ├── __init__.py
│   ├──application
│   │  ├── config.py
│   │  ├── static
│   │  ├── templates
│   │  ├── uploads
│   │  ├── utilities
│   │  └── zbo
│   ├── buildup.py
│   └── run.py
├── env
├── requirements.txt
├── setup.py
└── setup.sh

Create a wheel file:

pip3 wheel -w . .

This will create a file with all of your code named mypackage-1.0.0-py3-none-any.whl

2. Run Pex including your package:

pex --python=python3 -r requirements.txt -f . mypackage -e mypackage.run -o mypackage.pex

Note that the executable is specified using a dot between the package and module.

Glucose answered 26/11, 2017 at 0:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.