4

Env:

Windows 10
python 3.6.6
cx-Freeze 5.0.2

Git hub example

It contails .msi for installing

Example project structure:

/package_name
        /some_packet
            /__init.py
            /module_name.py  # for an example contains valiable in code "module_1"
    /main.py
    /setup.py
    /some_module.py  # for an example contains valiable "module_2"
    /some_other_module.py  # for an example contains valiable "module_3"

Example of setup.py(simplified)

import cx_Freeze

cx_Freeze.setup(
    name="example",
    options={
        "build_exe": {
            "packages": ["asyncio"],
            "include_files": ["static\some_static_file.png"]
        },
        "bdist_msi": {
            "upgrade_code": "{492de237-1853-4599-a707-c283d567699f}"
        }
    },
    executables=[cx_Freeze.Executable("main.py")]
)

Current behavior

For creating .msi install file -> run command python setup.py bdist_msi. It will generate .msi files for installing application.

After installing this application: directory(where application is installed) will contain:

  • main.exe
  • lib\some_packet directory
  • lib\some_packet\module_name.pyc file
  • other files

There are following statements:

1) From root directory(where application is installed) i start search(via grep -Rna command under Ubuntu guest system, it's just more convenient for me) and valiable module_1 could be found in directories(in lib\some_packet\module_name.pyc) and module_2/module_3 couldn't be found.
Details:

(v_decompile) any@any-pc:/mnt/hgfs/shar/example_installed$ grep -Rna "module_1" 
lib/some_packet/module_name.pyc:2:B�!]�@dZdS)module_1N)r�rr�PG:\heroes\installer-with-cx_Freeze\sources_of_project\some_packet\module_name.py<module>s 
(v_decompile) any@any-pc:/mnt/hgfs/shar/example_installed$ grep -Rna -a "module_2" 
(v_decompile) any@any-pc:/mnt/hgfs/shar/example_installed$ grep -Rna -a "module_3"

2) File lib\some_packet\module_name.pyc could be easily converted to original file(without comments) by e.g. python-uncompyle6.
Details:

(v_decompile) any@any-pc:/mnt/hgfs/shar/example_installed$ uncompyle6 lib/some_packet/module_name.pyc 
# uncompyle6 version 3.3.3 
# Python bytecode 3.6 (3379) 
# Decompiled from: Python 3.6.6 (default, Jul 20 2018, 15:39:05) 
# [GCC 4.8.4] 
# Embedded file name: G:\heroes\installer-with-cx_Freeze\sources_of_project\some_packet\module_name.py 
# Compiled at: 2019-07-07 11:28:50 
module_1 = 'module_1' 
# okay decompiling lib/some_packet/module_name.pyc

3) (solved with this question) In both points: file contains source path G:\heroes\installer-with-cx_Freeze\sources_of_project\some_packet\module_name.py It confuses me a bit. Application was installed from .msi and (as I understand) should not know about source directories (regarding path) which was used for creating last one.

Questions:

  1. Is there any way to recover some_module.py and some_other_module.py to original files from main.exe?(like it could be done with lib\some_packet\module_name.pyc)

  2. How to "hide" some other files in application into main.exe or somehow avoid converting .pyc to original files.(maybe some properties in cx_Freeze?)

Note:

It should be done with cx_Freeze.

PS: I don't want to create single .exe. I try to find convenient way for specifying which files should be stored in main.exe like it was done with some_module.py and some_other_module.py

PSS: At this moment I see only I way: put all files on main.py level :) But it will look weird for big project.

Yuriy Leonov
  • 536
  • 1
  • 9
  • 33
  • 1
    From the [documentation](https://cx-freeze.readthedocs.io/en/latest/faq.html#single-file-executables), `cx_freeze` does not support building a single file exe. – Henry Yik Jun 26 '19 at 15:31
  • @HenryYik yes, i've read that, but if I put all files on `main.py` level - it will be included in `.exe` :) Therefore I assume that there is some convenient way to do same for all files. – Yuriy Leonov Jun 26 '19 at 16:56
  • Your own words: "At this moment I see only I way: put all files on `main.py` level" reflect my opinion on your question as well. – jpeg Jul 08 '19 at 05:23
  • @jpeg Yep, but there is one more interesting question -> #1, about recowering files. Maybe putting files for including them in `main.exe` doesn't have any sense, in case when it could be recovered from `.exe` :) Because main point of this question is "hiding" source code from end user. – Yuriy Leonov Jul 08 '19 at 06:56
  • 1
    Regarding your point 3) "Application [...] should not know about source directories (regarding path)": maybe the cx_Freeze option `replace_paths` described in [this answer](https://stackoverflow.com/a/56592344/8516269) can help you on this point? – jpeg Jul 08 '19 at 08:32
  • 1
    @jpeg yeap :) it helps for #3 – Yuriy Leonov Jul 08 '19 at 08:48
  • **Mod Note** Do not use voting fraud to place bounties. The votes will be reversed and you will not have the rep to apply the bounty. –  Jul 08 '19 at 13:05

2 Answers2

2

Quoting How to obfuscate Python source code:

These two methods [using pyobfuscate and distributing bytecode] are really just a deterrent, not a secure way of hiding the code.

If you want something a bit more robust, you should take a look at Nuitka, which compiles Python code to C++, so you can compile that and just distribute the executable. It seems to be broadly compatible with different libraries and different versions of Python.

See also Python Code Obfuscation

Community
  • 1
  • 1
jpeg
  • 2,372
  • 4
  • 18
  • 31
0

This video might help. It talks about cxfreeze and how you can use cxfreeze to make a excutable, and I know it works for 3.4+ because the video uses python 3.4, but really your method should be fine...

Song
  • 298
  • 5
  • 20
  • Well, this video is just general for topic of "creating an executable file". It not very useful for my current question. But anyway thanks for this link, will be useful for beginners. – Yuriy Leonov Jul 16 '19 at 11:55
  • I do not really get your question. – Song Jul 16 '19 at 11:58
  • Point is hide several source files during creation executalbe file. As I mentioned in `Note` section: I don't look how to craete single exetable files with all libraries, because it's not possible to be done with cx_Freeze. – Yuriy Leonov Jul 16 '19 at 12:02
  • under "hide" file I don't mean mark files as "hidden", I mean include source `.py` into `.exe` file. So second link is not about topic at all. – Yuriy Leonov Jul 16 '19 at 12:16