0

I have a python project consisting of three files: file1.py, file2.py and main.py. I can start main.py from cmd with some arguments, which imports and uses file.py and file2.py as follow:

from file1 import *
from file2 import *
# some other imports

if __name__ == "__main__":
    arg1 =  sys.argv[1]
    arg2 = sys.argv[2]
    arg3 = sys.argv[3]
    RumMyLogic(arg1, arg2, arg3) # a function defined in file1 or so...

Now, I want to export all these files in pyc format(seperate pyc files or all in one, no matter) and hence, be able to run it from command line/terminal with some command like follow(supposing that python is installed on target machine):

main.pyc "arg1" "arg2" "arg3"

I couldn't find any straight forward solution on the internet. Any simple solution would be appreciated.

PS: This is not for code protection reasons.

Efe
  • 800
  • 10
  • 32
  • `python -m compileall /path/to/files`? – hoefling Dec 12 '17 at 22:28
  • Also see [`py_compile`](https://docs.python.org/3/library/py_compile.html) and [limitations](https://www.curiousefficiency.org/posts/2011/04/benefits-and-limitations-of-pyc-only.html). – Galen Dec 12 '17 at 22:31
  • On Unix, you can make the script executable by adding a shebang line `#!/usr/bin/env python` and changing the executable bit: `chmod +x main.py`. Then move the script to a directory in your `PATH`, preferably `$HOME/.local/bin` on Linux. If you want to do it the proper way, you could dive into packaging with `setuptools` - here's a [tutorial page](http://python-packaging.readthedocs.io/en/latest/), with a good [explanation for distributing command line scripts](http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html). – hoefling Dec 12 '17 at 22:34
  • @hoefling the shebang only works for text files. `pyc` are binary. – Peter Wood Dec 12 '17 at 23:09
  • 1
    You can zip all the `pyc` into one file along with a `__main__.pyc` entry point, and give the zip an extension of `.pyc`. – Peter Wood Dec 12 '17 at 23:11
  • @hoefling I know this command but the problem is mentioned in next comment – Efe Dec 12 '17 at 23:53
  • @Peter: I used hoefling command to create pyc file. I got three files named file1.cpython-36.pyc, file2.cpython-36.pyc and main.cpython-36.pyc. Then I zip them into myprogram.zip and change the file name to myprogram.pyc. running [myprogram.pyc "arg1" arg2" ]from cmd, I get this error: can't find '__main__' module in 'C:\\test\\myprogram.pyc'. Am I taking wrong steps? Seems it doesn't recognize the entry point in main.py – Efe Dec 12 '17 at 23:53
  • 1
    @Efe See related question: [What is `__main__.py`?](https://stackoverflow.com/questions/4042905/what-is-main-py) – Peter Wood Dec 12 '17 at 23:58
  • @Galen : Usefull link but not exactly what i'm looking for. Thanks – Efe Dec 13 '17 at 00:50
  • 1
    @Peter: Great solution. I removed .cpython-36 tail from all files, zipped them all together and changed the extension to pyc. Worked like a charm. Lot's of thanks – Efe Dec 13 '17 at 00:52

0 Answers0