Python compile all files to another structure - python

I have a Python project with files in a directories structure and I would like to get all .pyc files to the same directories to deliver without sources.
I am trying to do this with python -m compileall -d /tmp/new -b . but all pyc files are created in their respective sources directories instead of /tmp/new/somedir/
Any ideas? Will i have to create a script to recreate this structure?

Take a look about distributing *.pyc files:
What are the limitations of distributing .pyc files?
I suggest you use Py2Exe or cx_Freeze:
http://www.py2exe.org/
https://anthony-tuininga.github.io/cx_Freeze/

Related

How to compile directories of python code

I am new to python. I have a directory of *.py files and subdirectory of *.py files, how can I compile them to catch any syntax error?
Thank you.
To compile all .py file in suppose 'Lib/' directory :
import compileall
compileall.compile_dir('Lib/', force=True)
For more information on the python files compilation, please follow this link. https://docs.python.org/3/library/compileall.html

How to pack all the project files and make installer for python project

I have a python project which has below directory structure:
logs
-> app.log
output
-> out.json
src
-> encrypt_data.py
-> decrypt_data.py
app.py
I have above directories logs, output, src which further contains files in them. app.py is the main python script which is dependent on above directory and files. I am using python 3.6 and app.py needs some python libraries in order for it to run.
I am looking for an installer which can create a setup.exe file which when run, perform below actions:
Install python3.6 on user's system (if python is not already there)
Install required python libraries
Ask user to choose installation path (or select default path) so that it can copy all the above directories and files onto the specified path
Convert python file i.e. app.py, encrypt_data.py, decrypt_data.py to .pyc or any other format so that user cannot read it
(Optional) Finally creates shortcut on desktop so that running it can execute app.py
I have looked into pyinstaller python library which converts any app.py to app.exe but it do not packs all the project files & dir into one exe.
Is there any way I can pack complete project into one bundle file which then user can use to install everything and run the application. Please suggest some good options to do this.
Pyinstaller will do this for you, if you configure the spec file correctly, see The manual.
There is also Py2Exe, but do read the manual please.

sdist/bdist_wheel not including pyc in Linux but is included in Windows

I am trying to create a Python distribution where I have to include both the source and the compiled binary. (Yes, I read arguments against/for adding .pyc, but my use case requires the .pyc to be added). Running my steps in Windows, both the source and compiled binaries are added in the output file (I used both sdist and bdist_wheel).
Say I have the following structure:
root
+--folderA
+--alpha
+--beta
+--__init__.py
+--folderB
folderA contains source codes while folderB contains other files within its subdirectories.
Steps done:
Modules are compiled using compileall
Sources in alpha are removed. Sources in beta are kept.
Run python setup.py sdist|bdist_wheel
I used find_packages() in setup.py to detect the modules. Modules in alpha are not detected while those in beta are found.
In the results .tgz and .whl files in Windows, all the needed files are there. All is good.
However, when the same procedure is done in Linux (Ubuntu to be specific), only the modules in beta are added and some modules in folderB but not other files of different type and the module alpha. sdist will give only the source while bdist and bdist_wheel will give the .pycs only. I understand that sdist is for distributing source files while bdist is for binary files.
My question is why is the behavior different in Windows and is it possible to produce the same output in Linux (source and .pyc along with other files)?
I am using Python 2.7.
To include files in sdist add them to MANIFEST.in:
global-include *.py *.pyc
Including *.pyc into wheels works for me on Linux without any special configuration, I just do
python setup.py build
python -m compileall build
python setup.py sdist
python setup.py bdist_wheel
PS. If you're trying to hide sources from people — you're on the wrong track. *.pyc files can be decompiled. There is a disassembler in standard library and there are many decompilers and uncompilers out there.

cx_Freeze and PYC/PYD files

I'm using cx_Freeze to freeze my python program. On running cx_Freeze, a bunch of PYD files are created, a whole bunch of PYC files are put into a archive named library.zip and a few DLL files are there too.
Could someone tell me the difference between the PYC and the PYD files?
What's the reason for the PYD files not in the library.zip?
Is it possible to put the PYD files into the archive as well?
Thanks.
Disclaimer: I haven't used cx_Freeze in awhile......
.PYD files are DLL machine-code files that contain specific python-required functions.
.PYC files are .py files that have been compiled into bytecode.
so PYDs are machine code and PYCs are bytecode
Now as for why the PYDs aren't in the .zip....I'd imagine it's because those .PYDs are needed by the python interpreter to run the program. What cx_Freeze does is basically this:
compile all .py files and throw the .pyc files in a zip
put all needed .pyd files in the zip
create a stub .py file and put it in the output directory
copy the python.exe and rename to myprogram.exe
copy all .pyd files needed to open the .zip and run the contents
So you're not actually compiling your python file, you're instead renaming the interpeter and freezing all the source files.
I hope this helps.

Which files to distribute using cx_Freeze?

I'm using cx_freeze to freeze a Python script for distribution to other windows systems. I did everything as instructed and cx_freeze generated a build\exe.win32-2.6 folder in the folder containing my sources. This directory now contains a a bunch of PYD files, a library.zip file, the python DLL file and the main executable. Which of these files would I need to distribute? Any help, guys?
Thanks in advance.
You need all of them.

Categories

Resources