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

  • SEARCH
  • Home
  • 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 999215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:18:58+00:00 2026-05-16T07:18:58+00:00

Sorry for the beginner question, but I can’t figure out cProfile (I’m really new

  • 0

Sorry for the beginner question, but I can’t figure out cProfile (I’m really new to Python)

I can run it via my terminal with:

python -m cProfile myscript.py

But I need to run it on a webserver, so I’d like to put the command within the script it will look at. How would I do this? I’ve seen stuff using terms like __init__ and __main__ but I dont really understand what those are.

I know this is simple, I’m just still trying to learn everything and I know there’s someone who will know this.

Thanks in advance! I appreciate it.

  • 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-16T07:18:58+00:00Added an answer on May 16, 2026 at 7:18 am

    I think you’ve been seeing ideas like this:

    if __name__ == "__main__":
        # do something if this script is invoked
        # as python scriptname. Otherwise, gets ignored.
    

    What happens is when you call python on a script, that file has an attribute __name__ set to "__main__" if it is the file being directly called by the python executable. Otherwise, (if it is not directly called) it is imported.

    Now, you can use this trick on your scripts if you need to, for example, assuming you have:

    def somescriptfunc():
        # does something
        pass
    
    
    if __name__ == "__main__":
        # do something if this script is invoked
        # as python scriptname. Otherwise, gets ignored.
    
        import cProfile
        cProfile.run('somescriptfunc()')
    

    This changes your script. When imported, its member functions, classes etc can be used as normal. When run from the command-line, it profiles itself.

    Is this what you’re looking for?


    From the comments I’ve gathered more is perhaps needed, so here goes:

    If you’re running a script from CGI changes are it is of the form:

    # do some stuff to extract the parameters
    # do something with the parameters
    # return the response.
    

    When I say abstract out, you can do this:

    def do_something_with_parameters(param1, param2):
        pass
    
    if __name__ = "__main__":
        import cProfile
        cProfile.run('do_something_with_parameters(param1=\'sometestvalue\')')
    

    Put that file on your python path. When run itself, it will profile the function you want profiling.

    Now, for your CGI script, create a script that does:

    import {insert name of script from above here}
    
    # do something to determine parameter values
    # do something with them *via the function*:
    do_something_with_parameters(param1=..., param2=...)
    # return something
    

    So your cgi script just becomes a little wrapper for your function (which it is anyway) and your function is now self-testing.

    You can then profile the function using made up values on your desktop, away from the production server.

    There are probably neater ways to achieve this, but it would work.

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

Sidebar

Related Questions

Sorry for asking such a beginner question but I can't figure this out. I
Sorry for dumb question but I can't figure it out (beginner) I have a
First sorry, but im a really big beginner, so you can down vote this
Sorry for the beginner python question, but I cant find anywhere how to do
Sorry for question but I can't find answer anywhere on internet. I couldn't find
Sorry for this simple question, but I can't solve it... There is an example:
I know this is a very beginner question, but I've been struggling to figure
Sorry for beginner's questions but I wonder how can I set the root servlet
sorry for may be stupid question, I am a beginner. what can I do
sorry i'm a beginner and i can't determine how good a question this is,

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.