Loading...
 

Python

Python Frozen Binaries

Misc Notes

  • Any 'exit()' calls should be replaced with 'sys.exit(0)'
  • Any 'print()' calls should be replaced with 'sys.stdout.write()'
  • This 'if' statement should be used anywhere the execution path is needed. Specifically using 'pyinstaller' in Onefile mode
    if getattr(sys, 'frozen', False):
        script_path = os.path.dirname(sys.executable)
    else:
        script_path = os.path.dirname(os.path.realpath(__file__))
  • If you are doing any string comparisons of the arguments you may need to strip the quotes off of the string before comparing. I found that 'argparse' included the quotes when parsing the argument. this will strip the leading and trailing quote.
    argument.strip('\'')

Windows

  • Initial Device Setup
    1. Install your preferred Python version, make sure 'pip' is installed.
    2. Set your 'PATH' environment variable to your 'python.exe' path and the Python scripts folder. This isn't required but just makes life easier, only if you have a single version of Python installed.
    3. Install 'setuptools' and 'virtualenv'
  • Creating a Frozen Binary
    1. Create a folder for your build environment
    2. From an elevated command prompt, 'CD' to the directory you created
    3. Initialize a 'virtualenv' environment
      virtualenv venv
    4. Activate the environment by calling the 'activate.bat' file. The command prompt should have '(venv)' at the front of it.
      venv\Scripts\activate.bat
    5. Use 'pip' to install any modules that are needed by your code.
    6. Build your frozen binary. NOTE: There are a couple of important parameters below.
      1. I think there may be a bug in either 'pyinstaller' or 'virtualenv'. For some reason, even though I had activated my virtual environment, when installing modules via 'pip' within the virtual environment, then compiling with 'pyinstaller', the resulting executable would say that it was unable to locate certain modules. This is where the '--paths' parameter pointing to my virtual environment site packages was important.
      2. I had my own module file, that was importing other modules and 'pyinstaller' wasn't able to see those imports. This is where the '--hidden-import' parameter was important. I had to specify one of my own hidden modules, but I also had to specify a module that someone else was importing within one of their modules (boto3 needed HTMLParser).
        pyinstaller --onefile --clean --hidden-import some_module --hidden-import some_other_module --paths=<PathToVirtualEnv>\venv\Lib\site-packages <PathToPythonScript>

Linux

  • Creating a Frozen Binary
    1. Create a folder for your build environment
    2. 'CD' to the directory you created
    3. Initialize a 'virtualenv' environment
      virtualenv venv
    4. Activate the environment by sourcing the 'activate' file. The command prompt should have '(venv)' at the front of it.
      . venv\bin\activate
    5. Use 'pip' to install any modules that are needed by your code.
    6. Build your frozen binary. NOTE: There are a couple of important parameters below.
      1. I think there may be a bug in either 'pyinstaller' or 'virtualenv'. For some reason, even though I had activated my virtual environment, when installing modules via 'pip' within the virtual environment, then compiling with 'pyinstaller', the resulting executable would say that it was unable to locate certain modules. This is where the '--paths' parameter pointing to my virtual environment site packages was important.
      2. I had my own module file, that was importing other modules and 'pyinstaller' wasn't able to see those imports. This is where the '--hidden-import' parameter was important. I had to specify one of my own hidden modules, but I also had to specify a module that someone else was importing within one of their modules (boto3 needed HTMLParser).
        pyinstaller --onefile --clean --hidden-import some_module --hidden-import some_other_module --paths=<PathToVirtualEnv>\venv\Lib\site-packages <PathToPythonScript>

[+] Python WSGI on Apache

Zappa

Notes

  • AWS Component Names
    • It appears that Zappa will take the name of the directory that you ran 'zappa init' from and prepend that directory name to your stage name in your 'zappa_settings.json' file in order to create your AWS cloudformation stack name. Subsequently, your CF stack, API Gateway, Lambda function and probably other things will follow this naming convention.
    • Ex. Zappa init was run from a directory named 'v1' and you app name is 'test_app'. You will end up with your AWS items named 'v1-test_app'


















Show php error messages