How to retain an "import" capability after py2exe compile - python

I wrote a wxPython GUI where I currently configure some of the widgets and some default values by using "import data" for a module file containing several lists.
But I need to compile the whole program using py2exe for a user without a python installation.
In so doing, I lose the capability of letting the user edit the data.py file to change
those configuration defaults.
I could put each list as a series of text strings and read and parse the whole thing, but that
seems like a huge waste when python already can do all that by a simple import statement.
I could probably do it with xrc/xml or perhaps ConfigParser but it would seem there should be an easy way to sort of
import data.txt
or something similar and let python do it's thing! Then when py2exe gets hold of it it, it wouldn't create un-editable byte-code for the data.txt file.
Any suggestions?

files that are imported are bundled in the executable by py2exe. The way to go is to use a configuration file that you package with your executable in a zip or with Inno Setup. Configuration files are files made to be changed at some moment, contrarily a user should not be modifying a python script. I tell you because some 'negative' experiences to say something polite.
For my programs (practically all use wxPython GUIs) I use to have a py module with configuration data (directories, etc) and some globals. This module is used to load default parameters if the program does not find my .ini file or if that especific parameter has not been set in the ini. Then I distribute everything (exe and auxiliary files) with inno setup.

Related

How to build a customizable and changeable python module inside an executable?

I've built a project with Python in which a module of functions can be changed by the user. More specifically, functions can be added or deleted inside this module by other processes in the application. Now I have just converted the whole project into an executable file using auto-py-to-exe to run it through a console window, instead of running it through VS Code for instance. I can't change the module if it was not added as an additional file in auto-py-to-exe rather can the application use this module if I do add it as an additional file.
My question is: how can I turn this project into an executable with the possibility of changing this module by the program itself?
This may be tricky, but it is possible with tools like pyinstaller when you bundle your app as a directory, rather than a single file executable. The source files (albeit compiled) will be present in the directory.
In principle, you could edit files in a single-file executable, but it's probably more trouble than it's worth and may be blocked by permissions and other issues.
You could also design your software to read a file from a predefined location (or the bundle directory -- anywhere accessible by the user) and simply exec the string of the code to 'load' it. It could look more or less like an extension/scripting system for your program. One example comes to mind: the iterm2 software Python API.
It should go without saying that you must trust your users/inputs if you give them the ability to arbitrarily change the software code.

Main Python File Using Another Python File (I Want To Convert To .exe)

I Created a Program That Has 2 .py files.
I Want To Make The Program a .exe file, I do it using cx_Freeze.
My Problem Is That I Convert The main.py To .exe but The Second Python File Is Still a .py File.
I Don't Want It Be a .py Because If It Is The User Can See The Code.
If I Also Convert The Second Python File The Program Doesn't Work Because I import The Python File In The Main File.
Any Suggestions?
(I Don't Want To Copy The Second Python File To The Main Python File)
You are chasing the wrong rabbit here. The various tools that generate executable files from Python code are not compilers. They are just tools that embed a Python interpretor with py (or pyc) files to allow users to use the program without a prior Python installation.
Said differently you should not use them to hide your code (except from people not knowing a lot of Python): a pyc does not contain text code but according to the answers to Is it possible to decompile a compiled .pyc file into a .py file? , tools exists that convert back a pyc file into a py file (of course except the comments).
IMHO, you should better google for python obfuscate to find tools dedicated to obfuscation, what neither cx-freeze nor pyinstaller are.
BTW while there are appropriate use cases for obfuscation you should be aware that a determinate attacker can always circumvent it. The reason why the real protection for intellectual property is law and not technics...
I'm not sure how two or more .py files can be converted to .exe.
But in python the easiest way to convert to .exe is a module named pyinstaller .
You can install it using command pip install pyinstaller can get it . After just go to the directory where your project files are and open command prompt in that directory and execute pyinstaller file_name

Making serveral python files to an executable

I have two Python scripts, one as User Interface and one as background process. I need both of them because there is a sub-process and the UI. However I want it together get a executable.
I tried it with py2exe, which can only compile one of them.
Next I used pyinstaller but that has the same problem
pyinstaller user_interface.py
# and with py2exe
from distutils.core import setup
import py2exe
setup(console=['hello.py'])
What you need to do is some kind of packaging. First try to create a package using setup.py which you can use directly through python console by importing that package. and then you can create an executable out of it. Please refer the example in mentioned link.
Please refer this link: real python tutorial
You can write a small launcher script that spawns both processes, and then package that file.
I never used py2exe or pyinstaller, so I don't know how python dependencies are managed. If having code split in several files is a problem, then you can just concatenate you two existing scripts and the launcher in one single file.

how to persist/store data in executable compiled with pyinstaller

I need to locally store some basic data the user inputs into my exe. The program was compiled using pyinstaller and was previously making use of the os open method of saving data to txt files. It is my understanding that macOS(my OS although cross-compatibility with Windows would be great) locks executables so that they may not make any file changes. The program currently does save the data, but when the executable is run again the old data is no longer accessible.
It seems obvious that apps store things locally all the time, how can data be persisted specifically within the python/pyinstaller combination?
Apologies if this is a simple question, it definitely seems simple but I can't find documentation for this anywhere.
You can use
os.path.expanduser('~user')
to get the user home directory in a cross-platform manner, see How to find the real user home directory using python?
Your application should have write permissions in the user home directory, so then you can let it create and modify a data file there following How to store Python application data.

How to convert multiple python files to EXE?

I am trying to convert my python application to an exe. I have seen things like py2exe and cx freeze, but they only compile one single py file. Can anyone help me? Thank you
I currently use pyinstaller for building projects into single-executable files. These projects all contain multiple python (and some non-python) files that are all "built into" the exes.
That being said, even with multiple python files included, Marcus Müller is correct. There is one entry point for a given executable.
In summary, if you have multiple files as part of a single project, pyinstaller along with the other python bundlers will handle this scenario.
If you have multiple files and want them each to be their own executable file, you will need to treat each as its own 'project' and package each individually.
What platform(s) are you targeting? Can you describe the intended purpose of the files? Can you describe the intended usage of the files?
Posting an example of what you currently have, what behavior you are observing, and clarification on what is different between what you are expecting and what you are observing would definitely help others in guiding you towards the answer you desire.
Edit:
Well, I have a main python file that is referenced to a very brief config.py. The main file also accesses a few text files. Could I just combine the config and the main into one py file, and make that an executable, and will that executable still have access to the text files?
Your main python file would be your exe's entry point. If you import your config file into your main, pyinstaller should see the import and include it. On this line, verify your PATH environment variable, and insure your system knows where to find the bits it needs. If the text files are to be included as part of the built executable file, pyinstaller has the ability to include files into the build as well (example, include a database, a config, or a static data set). An example question describing including an icon file for a build: include-pyinstaller-icon
For example in PyInstaller, you can specify additional folders which should be involved in your executable file.

Categories

Resources