(following a little discussion in the OP's post)
If what you need is provide clients or users with the compiled application in form of *.pyc or *.pyo, and avoid opening the console at the execution, a good option is to leave a *.pyw wrapper that calls the main application.
Let's assume the main application is in main.pyc
, and the entry point main
, and let's call the wrapper top.pyw
, which would typically look like:
# top.pyw file
import main
if __name__ == "__main__":
import sys
main.main(*sys.argv[1:])
Your main.py file would then look like this:
# main.py file
"""
Main module documentation (optional)
"""
# import modules
# function and class definitions
def main(*argv):
# Parses the options (optional)
import optparse
parser = optparse.OptionParser(usage="%prog [<options>]\n" + __doc__)
parser.add_option(...)
parser.add_option(...)
opt, args = parser.parse_args(list(argv))
# Calls the appropriate function:
my_function(...)
Note also that *.pyc tend to be version-specific. You can check whether the solution above would be compatible with pyInstaller and similar "independent" distribution methods.
Edit => In fact, if you use pyInstaller, you can simply include all your scripts and produce an executable which will be independent of the Python installation, and starts with no console (-w
option when you create the specs). You don't even need to use a wrapper or change your extensions. While it will be a larger file, that could be what you were looking for.
Finally, just in case that's where you are headed: if you don't want someone else to extract source code from the compiled bytecode, don't forget that you will need additional precautions. This is called code obfuscation and there are other threads on SO about that (for example this one). Don't hesitate to post a new question if you only come up with old answers on that one, those things can change fast.