Skip to content

Instantly share code, notes, and snippets.

@rochacbruno
Last active July 22, 2024 19:03

Revisions

  1. rochacbruno revised this gist Apr 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mainpython.md
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ also you can easily obfuscate it and turn in to a **binary** like program

    Now you can delete myproject.zip and run

    python myprogram
    ./myprogram


    Sometimes it is better than the hackkish ``if __name__ == "__main__"``
  2. rochacbruno revised this gist Apr 23, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions mainpython.md
    Original file line number Diff line number Diff line change
    @@ -40,6 +40,9 @@ Now you can delete myproject.zip and run

    python myprogram


    Sometimes it is better than the hackkish ``if __name__ == "__main__"``

    as seen on https://www.youtube.com/watch?v=0oTh1CXRaQ0


  3. rochacbruno revised this gist Apr 23, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions mainpython.md
    Original file line number Diff line number Diff line change
    @@ -22,10 +22,14 @@ You can also zip the folder

    python -m zipfile -c myproject.zip myprojectfolder

    > YES Python can Zip files and folders!
    and now you can get rid of myprojectfolder source code and run

    python myproject.zip

    > YES! Python can execute zipped files (remember the .egg)
    also you can easily obfuscate it and turn in to a **binary** like program

    echo '#!/usr/bin/env python' >> myprogram
  4. rochacbruno revised this gist Apr 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mainpython.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ if you do
    python myprojectfolder
    Hello

    > it can also work in submodules if you have a subfolder just use the dot ``python myprojectfolder.subfolder`` and the main inside that folder will be executed
    > it can also work in submodules if you have a subfolder just use the dot ``python myprojectfolder.subfolder`` and the ``__main__.py`` inside that folder will be executed
    Python will execute the code in ``__main__.py`` as this file is an entry point.

  5. rochacbruno revised this gist Apr 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mainpython.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ if you do
    python myprojectfolder
    Hello

    > it can also work in submodules if you have a subfolder just use the dot ``python myprojectfolder.subfolder and the main inside that folder will be executed
    > it can also work in submodules if you have a subfolder just use the dot ``python myprojectfolder.subfolder`` and the main inside that folder will be executed
    Python will execute the code in ``__main__.py`` as this file is an entry point.

  6. rochacbruno revised this gist Apr 23, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion mainpython.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@ The use of ``__main__.py`` to create executables
    myprojectfolder/
    |_ __main__.py
    |_ __init__.py
    |_ my_program.py


    Being ``__main__.py``:
  7. rochacbruno revised this gist Apr 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mainpython.md
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ Being ``__main__.py``:
    if you do

    python myprojectfolder
    >>> Hello
    Hello

    > it can also work in submodules if you have a subfolder just use the dot ``python myprojectfolder.subfolder and the main inside that folder will be executed
  8. rochacbruno created this gist Apr 23, 2015.
    42 changes: 42 additions & 0 deletions mainpython.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    The use of ``__main__.py`` to create executables

    myprojectfolder/
    |_ __main__.py
    |_ __init__.py
    |_ my_program.py


    Being ``__main__.py``:

    print("Hello")

    if you do

    python myprojectfolder
    >>> Hello

    > it can also work in submodules if you have a subfolder just use the dot ``python myprojectfolder.subfolder and the main inside that folder will be executed
    Python will execute the code in ``__main__.py`` as this file is an entry point.

    You can also zip the folder

    python -m zipfile -c myproject.zip myprojectfolder

    and now you can get rid of myprojectfolder source code and run

    python myproject.zip

    also you can easily obfuscate it and turn in to a **binary** like program

    echo '#!/usr/bin/env python' >> myprogram
    cat myproject.zip >>myprogram
    chmod +x myprogram

    Now you can delete myproject.zip and run

    python myprogram

    as seen on https://www.youtube.com/watch?v=0oTh1CXRaQ0