/ Bit by bit / posts

Python Code Structure and Distribution

Jul 13, 2018

Probably it’s due to my inexperience in Python’s community/ecosystem, I find it amusing that Python community has no agreed standard in code organization. Yeah, The Hitchhiker’s Guide to Python! attempts to recommend some structure, but the popular pytest recommends something entirely different.

By adopting pytest’s recommendation–i.e., putting code in src directory–I need to make the some changes, changes that are not obvious to a noob like me:

 1# Assuming the filename is setup.py
 2import os.path
 3import sys
 4from cx_Freeze import setup, Executable
 5
 6PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
 7os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
 8os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
 9
10base = 'Win32GUI'
11
12options={'build_exe': {
13   'includes': ['tkinter'],
14   'include_files': [
15       './resources/tcl86t.dll',
16       './resources/tk86t.dll',
17       './resources/vcruntime140.dll'],
18    'path': sys.path + [os.path.abspath('./src')]}}
19
20setup(  name = 'my_awesome_app',
21       version = '0.1.0',
22       description = 'My Super, Duper Awesome App',
23       executables = [Executable(script='./src/main.py', base=base,
24           targetName='awesome.exe',
25           copyright='Copyright © 2018 Shaolang Ai')],
26       options = options)

Yes, I develop apps to run in Windows. The code above also shows the things I need to include to ensure that the packaged app can run TkInter without issues. With these in place, running python setup.py build will create my redistributable standalone app correctly.