Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4076060
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:22:38+00:00 2026-05-20T17:22:38+00:00

Why does Python compile libraries that are used in a script, but not the

  • 0

Why does Python compile libraries that are used in a script, but not the script being called itself?

For instance,

If there is main.py and module.py, and Python is run by doing python main.py, there will be a compiled file module.pyc but not one for main. Why?

  1. If the response is potential disk permissions for the directory of main.py, why does Python compile modules? They are just as likely (if not more likely) to appear in a location where the user does not have write access. Python could compile main if it is writable, or alternatively in another directory.

  2. If the reason is that benefits will be minimal, consider the situation when the script will be used a large number of times (such as in a CGI application).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:22:39+00:00Added an answer on May 20, 2026 at 5:22 pm

    Files are compiled upon import. It isn’t a security thing. It is simply that if you import it python saves the output. See this post by Fredrik Lundh on Effbot.

    >>>import main
    # main.pyc is created
    

    When running a script python will not use the *.pyc file.
    If you have some other reason you want your script pre-compiled you can use the compileall module.

    python -m compileall .
    

    compileall Usage

    python -m compileall --help
    option --help not recognized
    usage: python compileall.py [-l] [-f] [-q] [-d destdir] [-x regexp] [directory ...]
    -l: don't recurse down
    -f: force rebuild even if timestamps are up-to-date
    -q: quiet operation
    -d destdir: purported directory name for error messages
       if no directory arguments, -l sys.path is assumed
    -x regexp: skip files matching the regular expression regexp
       the regexp is searched for in the full path of the file
    

    If the response is potential disk permissions for the directory of main.py, why does Python compile modules?

    Modules and scripts are treated the same. Importing is what triggers the output to be saved.

    If the reason is that benefits will be minimal, consider the situation when the script will be used a large number of times (such as in a CGI application).

    Using compileall does not solve this. Scripts executed by python will not use the *.pyc unless explicitly called. This has negative side effects, well stated by Glenn Maynard in his answer.

    The example given of a CGI application should really be addressed by using a technique like FastCGI. If you want to eliminate the overhead of compiling your script you may want eliminate the overhead of starting up python too, not to mention database connection overhead.

    A light bootstrap script can be used or even python -c "import script", but these have questionable style.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Get this simple python code, same matching with re.compile instance. I noticed that even
When I compile the Python code below, I get IndentationError: unindent does not match
Does Python have a pool of all strings and are they (strings) singletons there?
(I'm sure this is a FAQ, but also hard to google) Why does Python
EPD python does not know to look for the Ubuntu repository python so installing
Python does not print traceback messages from exceptions raised in daemon threads. For example,
If you compile a regex inside a function, and that function gets called multiple
So I wrote a Python script which does some simple stuff. It was originally
Does there exist a parser that generates an AST/parse tree at runtime? Kind of
dir(re.compile(pattern)) does not return pattern as one of the lists's elements. Namely it returns:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.