Search code examples
pythontwitterpex

Problems creating entrypoint on PEX file


I am currently trying to create a pex file with an Pythonfile entrypoint.

My Folder structure looks like the following:

├── readme.md
├── requirements.txt
├── setup.py
├── stv
│   ├── adminuser.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── adminuser.cpython-39.pyc
│   │   ├── __init__.cpython-39.pyc
│   │   ├── sequenceGenerator.cpython-39.pyc
│   │   ├── servicetracevisualizer.cpython-39.pyc
│   │   └── traceloader.cpython-39.pyc
│   ├── sequenceGenerator.py
│   ├── servicetracevisualizer.py
│   └── traceloader.py
└── stvcli.py

So I tried making a pex file of all the files which I afterwards verified to hold all the files by unzipping it.

My Problem is that I cant get the entry point set to this file:

stvcli.py

Here ist the Implementation of stvcli.py:

import argparse
import logging
from stv import servicetracevisualizer
import os
import sys
    
#print args
parser = argparse.ArgumentParser(description='Service Trace Visualizer interface.')

parser.add_argument('--renderurls', dest="renderurls", action="store_true",
                   help='do not try to render operations basend on mapping-table but print urls directly [True, False (default)]')
parser.add_argument('--includeuseless', dest="includeuseless", action="store_true",
                   help='include insignificant requests from sequence chart (e.g. successful Unified Auth) [True, False (default)]')
parser.add_argument('--outfolder', metavar='outfolder', type=str,
                   help='folder for saving resulting files (default is folder of inputfile)')
parser.add_argument('--loglevel', metavar='loglevel', type=str, default="INFO",
                   help='select loglevel: [CRITICAL, ERROR, WARNING, INFO (default), DEBUG, NOTSET]')
parser.add_argument('--filename', metavar='filename', type=str,  required=True,
                   help='input filename')

args = parser.parse_args()
    
#set args
renderOperations = not args.renderurls
removeUseless = not args.includeuseless
filename = args.filename
if args.outfolder is None:
    outfolder = os.path.dirname(filename)
else:
    outfolder = args.outfolder
loglevel = logging._nameToLevel[args.loglevel]

#add trailing "/" to outpath
if outfolder[:-1] != "/":
    outfolder = outfolder + "/"
#create dirs if outpath does not exist
if not os.path.exists(outfolder):
    os.makedirs(outfolder)

#initialize logger
logging.basicConfig(level=loglevel, filename = outfolder + os.path.basename(filename)+".log")
log = logging.getLogger(__name__)
try:
    log.info("init stv")
    stv = servicetracevisualizer.serviceTraceVisualizer(renderOperations=renderOperations, removeUseless=removeUseless, outfolder=outfolder)
    log.info("render file " + filename)
    exitcode = stv.renderFile(filename)
except Exception as e:
    log.error(e)
    exitcode = 100 #TODO custom Error codes
log.info("exitcode " + str(exitcode))
sys.exit(exitcode)

My attempt of creating a PEX File with stvcli.py are the following:

pex -e stvcli:stvcli -r requirements.txt -o stvcli.pex

But on Startup it only gives me a ModuleNotFoundError: No module named 'stvcli', when stvcli clearly exists!

How can I make my Python File as my Entrypoint? If you have any questions, I will answer them immediatley!


Solution

  • I found an answer to my problem!

    To set an entry point to a .py file on a pex file you use the -c command!

    e.g. pex . -r requirements.txt -c main.py -o test.pex

    The official documentation is really complicated and and clunky!

    Here is a blog that in great detail describes the steps you need to take to succesfully create a pex file!

    https://www.shearn89.com/2021/04/15/pex-file-creation

    This helped me a lot and I hope this helps you as well!