How to clean Lib and Dll folder - python

I'm making a .dll for an .exe program and embedding python in it. For it to work I need to include the .py programs and the Dll and Lib folders in the .exe folder like it says in here. It works fine on my computer but i want to use the .dll in other computers.
So what I want is to erase everything that isn't necessary from the Lib and Dlls folders (right now the two folders have 210Mb)
I already looked into ModuleFinder(like it says here) and managed to erase some modules. But that's not what I want. ModuleFinder tells me the unused modules but how do I clean them and all the other unneeded files from Lib and Dll when there are thousands of files there?
Anyone knows any good way of doing this?

"Dependency Walker" is a tool which can show you which DLLs are needed. That includes both a static mode (which are needed just to load the EXE?) and a dynamic mode (which are needed at runtime?)

Related

How do I get the list of imports and dependant files from python script?

I have a .py file that imports from other python modules that import from config files, other modules, etc.
I am to move the code needed to run that .py file, but only whatever the py file is reading from (I am not talking about packages installed by pip install, it's more about other python files in the project directory, mostly classes, functions and ini files).
Is there a way to find out only the external files used by that particular python script? Is it something that can be found using PyCharm for example?
Thanks!
Static analysis tools (such as PyCharm's refactoring tools) can (mostly) figure out the module import tree for a program (unless you do dynamic imports using e.g. importlib.import_module()).
However, it's not quite possible to statically definitively know what other files are required for your program to function. You could use Python's audit events (or strace/ptrace or similar OS-level functions) to look at what files are being opened by your program (e.g. during your tests being run (you do have tests, right?), or during regular program use), but it's likely not going to be exhaustive.

Python - When building a binary with cx_Freeze, where is the code saved?

I know its somewhere in the lib folder, and not in the actual exe, but where? I expected that, since in the lib folder there are a bunch of folders for other libraries, it creates a folder for each user-created module and saves the code there. But that is not the case, and nothing i see resembles the names of my modules. What files actually change if i change my code (no new imports etc, just simple additions) between two builds of cx_freeze?
I found the .pyc files in build/exe.win32-3.8/lib/library.zip.

How do I view python modules? They are .so files and PyCharm does not recognize how to open them

I want to read how some of the modules work. I thought they would be .py files which could teach me the inner workings of then. However when I import a module it says it is coming from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload/math.cpython-36m-darwin.so
I went to the lib-dynload folder under external libraries in PyCharm but these module files are all '.so' files.
What is a .so file?
How can I view a .so file and learn about the code within it?
If it isn't obvious I am very new to python so please be descriptive with your answer.
.so files are native shared libraries. They are written in C rather than Python. If you're curious about them, you can look at Python's source code.

Cx_Freeze's extra stuff

Whenever I build an exe with cx_Freeze and Python I get a bunch of extra stuff like the Library.zip and all the .dll files. Is there a way I can make it just one executable file that I can just send over to someone and have them run without having to give them all the extra files also? Python 3.4. Thank you!
Not really1. You're best option for a single-file distribution is probably to create an installer.
You can however append the library.zip to your executable:
params['options'] = {
'append_script_to_exe': True,
'create_shared_zip': False,
...
}
setup(**params)
But this only reduces the number of files by 1.
There are two reasons why you can't do this. The first is that some modules are not "zip safe" (those that contain data files that are read with open()). The second, and more important reason, is that Python requires various DLLs in order to run, and Windows's dynamic linker doesn't know how to find and load those DLLs if they're inside a zip file.
See: http://cx-freeze.readthedocs.org/en/latest/faq.html#single-file-executables
1 If you're really ambitious, you could theoretically create an entirely static build of Python (statically link all of the library source code, and the C runtimes, etc.), and do the same with any C modules that you might be using. That plus appending the Library.zip file to the exe might give you a single-file distribution.
However, tracking down and building all those dependencies would be a very large effort.
Yes, If your on windows this method works.
Run ->> iexpress
Follow the instructions.
This will compile all the files into on exe but first you need to create the exe using cx_freeze then browse to the directory in iexpress and it will do the rest.

python modules difference: .c/.h vs. .py

I'm very new to python, as i'm embedding it (in form of a static lib) in an ios/obj-c project. It's not possible for me to dynamically load python modules, so i would like to compile my modules along with python.
For modules shipped with the python source this works (by modifying setup.py or Module/Setup), but when i downloaded a third party module i noticed, i don't fully understand the mechanism.
The modules shipped with python come with a .c file in the Modules dir as well as a .py file in the Lib dir.
My third party module just comes with .py files.
1.Why do those modules have different file extensions?
2.How to integrate a module coming with .py files in an embedded python version? Obviously pasting them in Modules/Setup does require some .c files.
3.Do these .c files have something to do with the Python C-Api?
I guess i'm missing something essential :) Help is much appreciated.

Categories