py_compile — Compile Python source files (2024)

Source code: Lib/py_compile.py

The py_compile module provides a function to generate a byte-code filefrom a source file, and another function used when the module source file isinvoked as a script.

Though not often needed, this function can be useful when installing modules forshared use, especially if some of the users may not have permission to write thebyte-code cache files in the directory containing the source code.

exception py_compile.PyCompileError

Exception raised when an error occurs while attempting to compile the file.

py_compile.compile(file, cfile=None, dfile=None, doraise=False, optimize=-1, invalidation_mode=PycInvalidationMode.TIMESTAMP, quiet=0)

Compile a source file to byte-code and write out the byte-code cache file.The source code is loaded from the file named file. The byte-code iswritten to cfile, which defaults to the PEP 3147/PEP 488 path, endingin .pyc.For example, if file is /foo/bar/baz.py cfile will default to/foo/bar/__pycache__/baz.cpython-32.pyc for Python 3.2. If dfile isspecified, it is used instead of file as the name of the source file fromwhich source lines are obtained for display in exception tracebacks.If doraise is true, a PyCompileError is raisedwhen an error is encountered while compiling file. If doraise is false(the default), an error string is written to sys.stderr, but no exceptionis raised. This function returns the path to byte-compiled file, i.e.whatever cfile value was used.

The doraise and quiet arguments determine how errors are handled whilecompiling file. If quiet is 0 or 1, and doraise is false, the defaultbehaviour is enabled: an error string is written to sys.stderr, and thefunction returns None instead of a path. If doraise is true,a PyCompileError is raised instead. However if quiet is 2,no message is written, and doraise has no effect.

If the path that cfile becomes (either explicitly specified or computed)is a symlink or non-regular file, FileExistsError will be raised.This is to act as a warning that import will turn those paths into regularfiles if it is allowed to write byte-compiled files to those paths. This isa side-effect of import using file renaming to place the final byte-compiledfile into place to prevent concurrent file writing issues.

optimize controls the optimization level and is passed to the built-incompile() function. The default of -1 selects the optimizationlevel of the current interpreter.

invalidation_mode should be a member of the PycInvalidationModeenum and controls how the generated bytecode cache is invalidated atruntime. The default is PycInvalidationMode.CHECKED_HASH ifthe SOURCE_DATE_EPOCH environment variable is set, otherwisethe default is PycInvalidationMode.TIMESTAMP.

Changed in version 3.2: Changed default value of cfile to be PEP 3147-compliant. Previousdefault was file + 'c' ('o' if optimization was enabled).Also added the optimize parameter.

Changed in version 3.4: Changed code to use importlib for the byte-code cache file writing.This means file creation/writing semantics now match what importlibdoes, e.g. permissions, write-and-move semantics, etc. Also added thecaveat that FileExistsError is raised if cfile is a symlink ornon-regular file.

Changed in version 3.7: The invalidation_mode parameter was added as specified in PEP 552.If the SOURCE_DATE_EPOCH environment variable is set,invalidation_mode will be forced toPycInvalidationMode.CHECKED_HASH.

Changed in version 3.7.2: The SOURCE_DATE_EPOCH environment variable no longeroverrides the value of the invalidation_mode argument, and determinesits default value instead.

Changed in version 3.8: The quiet parameter was added.

class py_compile.PycInvalidationMode

A enumeration of possible methods the interpreter can use to determinewhether a bytecode file is up to date with a source file. The .pyc fileindicates the desired invalidation mode in its header. SeeCached bytecode invalidation for more information on how Python invalidates.pyc files at runtime.

New in version 3.7.

TIMESTAMP

The .pyc file includes the timestamp and size of the source file,which Python will compare against the metadata of the source file atruntime to determine if the .pyc file needs to be regenerated.

CHECKED_HASH

The .pyc file includes a hash of the source file content, which Pythonwill compare against the source at runtime to determine if the .pycfile needs to be regenerated.

UNCHECKED_HASH

Like CHECKED_HASH, the .pyc file includes a hash of the sourcefile content. However, Python will at runtime assume the .pyc file isup to date and not validate the .pyc against the source file at all.

This option is useful when the .pycs are kept up to date by somesystem external to Python like a build system.

Command-Line Interface

This module can be invoked as a script to compile several sourcefiles. The files named in filenames are compiled and the resultingbytecode is cached in the normal manner. This program does not searcha directory structure to locate source files; it only compiles filesnamed explicitly. The exit status is nonzero if one of the files couldnot be compiled.

<file> ... <fileN>
-

Positional arguments are files to compile. If - is the onlyparameter, the list of files is taken from standard input.

-q, --quiet

Suppress errors output.

Changed in version 3.2: Added support for -.

Changed in version 3.10: Added support for -q.

See also

Module compileall

Utilities to compile all Python source files in a directory tree.

py_compile — Compile Python source files (2024)
Top Articles
Western Blotting Data Analysis | Bio-Rad
Increase Amazon Sales Online
Craigslist St. Paul
Melson Funeral Services Obituaries
4-Hour Private ATV Riding Experience in Adirondacks 2024 on Cool Destinations
The UPS Store | Ship & Print Here > 400 West Broadway
Weeminuche Smoke Signal
Visitor Information | Medical Center
Faint Citrine Lost Ark
Top Scorers Transfermarkt
Aita Autism
fltimes.com | Finger Lakes Times
Inside California's brutal underground market for puppies: Neglected dogs, deceived owners, big profits
What Time Chase Close Saturday
Sony E 18-200mm F3.5-6.3 OSS LE Review
iLuv Aud Click: Tragbarer Wi-Fi-Lautsprecher für Amazons Alexa - Portable Echo Alternative
Blackwolf Run Pro Shop
Mzinchaleft
Sonic Fan Games Hq
Plan Z - Nazi Shipbuilding Plans
Why Does Lawrence Jones Have Ptsd
Why do rebates take so long to process?
Rimworld Prison Break
Teen Vogue Video Series
Www.dunkinbaskinrunsonyou.con
Jermiyah Pryear
Yale College Confidential 2027
Ordensfrau: Der Tod ist die Geburt in ein Leben bei Gott
Astro Seek Asteroid Chart
Yu-Gi-Oh Card Database
Pay Stub Portal
Filmy Met
United E Gift Card
Teenbeautyfitness
Upstate Ny Craigslist Pets
Save on Games, Flamingo, Toys Games & Novelties
All Things Algebra Unit 3 Homework 2 Answer Key
Rogers Centre is getting a $300M reno. Here's what the Blue Jays ballpark will look like | CBC News
Hannibal Mo Craigslist Pets
Best Restaurant In Glendale Az
The Thing About ‘Dateline’
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
Download Diablo 2 From Blizzard
Craigslist Food And Beverage Jobs Chicago
Arnesons Webcam
Spurs Basketball Reference
Dobratz Hantge Funeral Chapel Obituaries
De Donde Es El Area +63
Vcuapi
How to Find Mugshots: 11 Steps (with Pictures) - wikiHow
Cheryl Mchenry Retirement
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5974

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.