How to compile directories of python code - python

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

Related

Python compile all files to another structure

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/

Having both cython compiled .so and source .py in the same folder.

Is it possible to include both .so (cython compiled) and .py files in a folder with same name. Which one will python use if I use import ?

Compile python code then place it somewhere else

I have python file, for example, named blah.py, that I would like to have compiled then placed into another folder using cmake. Right now, I am capable of doing this with the following code in my cmake file:
ADD_CUSTOM_TARGET(output ALL /usr/bin/python -m py_compile src/blah.py
COMMAND /bin/mv src/blah.pyc build VERBATIM)
This is on ubuntu 12.04. This code works as intended; the only problem is that the python file is being compiled in the source directory, then being put in the build directory.
However, I can't assume that this src directory will have read AND write privileges, meaning what I need to do is combine these two commands into one (compile the python file and place the compiled python file into my build directory, instead of compiling it in the src directory then moving it)
I'm sure there must be some way I could be using to specify where I would like this compiled code to be placed, but I can't find any. Help would be greatly appreciated! :)
EDIT: This link may have a solution..not sure:
Can compiled bytecode files (.pyc) get generated in different directory?
I was typing out this answer, and then looked at your edited link. This same answer is given in one of the unaccepted answers: https://stackoverflow.com/a/611995/496445
import py_compile
py_compile.compile('/path/to/source/code.py', cfile='/path/to/build/code.pyc')
To call this via a basic shell command you can format it like this:
python -c "import py_compile; py_compile.compile('/path/to/source/code.py', cfile='/path/to/build/code.pyc')"

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