This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: cProfile command-line should accept "-m module_name" as an alternative to script path
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: CuriousLearner, amaury.forgeotdarc, berker.peksag, gennad, georg.brandl, ncoghlan, oquanox, pitrou, rock
Priority: normal Keywords: easy, patch

Created on 2014-06-24 20:06 by pitrou, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
cProfile-add-new-option-module.patch rock, 2014-06-24 21:30 patch review
21862.patch gennad, 2014-07-25 22:08 review
cProfile_module_option.patch oquanox, 2015-01-17 20:00 patch review
cProfile_module_option_2.patch oquanox, 2015-01-27 23:21 patch review
Pull Requests
URL Status Linked Edit
PR 4297 merged CuriousLearner, 2017-11-06 19:42
Messages (20)
msg221488 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-06-24 20:06
As the title says. You should be able to type:

$ python -m cProfile -m my.module.name

to profile execution of my.module.name.
msg221502 - (view) Author: Rock Lee (rock) Date: 2014-06-24 21:30
I tweaked the Lib/cProfile.py a little bit to get the feature done, please review the patch attached.
msg221546 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-06-25 13:13
That's not how -m should work. It should use the runpy facilities (see the runpy module). Otherwise it probably won't work properly with qualified module names ("-m pkg.subpkg.mod").
msg224008 - (view) Author: Gennadiy Zlobin (gennad) * Date: 2014-07-25 22:08
I added runpy import mechanism
msg232932 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2014-12-19 14:07
The patch does not seem to allow parameters after the -m option.
I'm sure this restriction can be lifted.
msg234195 - (view) Author: Mayank Tripathi (oquanox) * Date: 2015-01-17 20:00
Now allows parameters after the -m option.
msg234560 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2015-01-23 15:44
This change will cause the module to be imported twice:
    progname = runpy.run_module(args[0])['__file__']
... and then the runctx() call.

What about something like:
    code = "runpy.run_module(modname, run_name='__main__')"
    globs = { 'runpy': runpy, 'modname': args[0] }
msg234614 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2015-01-24 13:22
I haven't looked into the details of how cProfile handles command line arguments, but potentially relevant is issue #19982, which proposes adding a "target" parameter that lets runpy run in the context of an existing module object, rather than always creating its own.

However, if this can be implemented without that, go ahead - it can always be refactored later.
msg234863 - (view) Author: Mayank Tripathi (oquanox) * Date: 2015-01-27 23:21
Updated the patch and added docs and tests.
msg238195 - (view) Author: Mayank Tripathi (oquanox) * Date: 2015-03-16 11:41
Could someone please review the patch. Thanks.
msg305666 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2017-11-06 18:13
Hey, seems like this bug is not updated for a while. Can I work on this?
msg305667 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-06 18:17
Sanyam, you definitely can.  Step #1 would probably be to port the patch to git master and turn it into a GitHub PR.
msg305668 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2017-11-06 18:22
Thanks for the heads up! I'll do that ;)
msg305714 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-11-07 07:48
Interesting - I'd never looked at how cProfile works before, and the fact it already doesn't support the "-i" (interactive) switch makes it much simpler to handle than the pdb case.

So from a runpy perspective, this approach gets a +1 from me, but I'd prefer if there was someone more familiar with the cProfile module that was willing to handle the merge.
msg305720 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-07 08:29
Le 07/11/2017 à 08:48, Nick Coghlan a écrit :
> 
> So from a runpy perspective, this approach gets a +1 from me, but I'd prefer if there was someone more familiar with the cProfile module that was willing to handle the merge.

Thanks Nick.  Can you confirm the runpy invocation in the PR is sane?
msg305741 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-11-07 12:07
I added an inline comment on the PR - I think what's there now would work fine, but I also suggested a slightly shorter and clearer (at least to me) alternative.
msg305742 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-11-07 12:08
Also, based on reviewing this, I suspect the same approach would also work for the pure Python profile module.
msg305819 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-08 10:51
New changeset 7973e279a21999f134aff92dd6d344ec4591fae9 by Antoine Pitrou (Sanyam Khurana) in branch 'master':
bpo-21862: Add -m option to cProfile for profiling modules (#4297)
https://github.com/python/cpython/commit/7973e279a21999f134aff92dd6d344ec4591fae9
msg305820 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2017-11-08 10:51
This is done.  Thank you Sanyam!
msg305822 - (view) Author: Sanyam Khurana (CuriousLearner) * (Python triager) Date: 2017-11-08 11:21
Thanks a lot Antoine and Nick :)
History
Date User Action Args
2022-04-11 14:58:05adminsetgithub: 66061
2018-01-05 07:34:30ncoghlanlinkissue9325 dependencies
2017-11-08 11:21:16CuriousLearnersetmessages: + msg305822
2017-11-08 10:51:30pitrousetstatus: open -> closed
resolution: fixed
messages: + msg305820

stage: patch review -> resolved
2017-11-08 10:51:02pitrousetmessages: + msg305819
2017-11-07 12:08:20ncoghlansetmessages: + msg305742
2017-11-07 12:07:22ncoghlansetmessages: + msg305741
2017-11-07 08:29:07pitrousetmessages: + msg305720
2017-11-07 07:48:46ncoghlansetmessages: + msg305714
2017-11-06 19:42:34CuriousLearnersetpull_requests: + pull_request4259
2017-11-06 18:22:10CuriousLearnersetmessages: + msg305668
2017-11-06 18:17:19pitrousetmessages: + msg305667
2017-11-06 18:13:47CuriousLearnersetnosy: + CuriousLearner
messages: + msg305666
2017-11-06 14:41:02pitrousetversions: + Python 3.7, - Python 3.5
2015-03-16 11:41:08oquanoxsetmessages: + msg238195
2015-01-27 23:21:11oquanoxsetfiles: + cProfile_module_option_2.patch

messages: + msg234863
2015-01-24 13:22:28ncoghlansetmessages: + msg234614
2015-01-23 15:44:48amaury.forgeotdarcsetmessages: + msg234560
2015-01-22 22:21:33berker.peksagsetnosy: + berker.peksag

stage: needs patch -> patch review
2015-01-17 20:00:29oquanoxsetfiles: + cProfile_module_option.patch
nosy: + oquanox
messages: + msg234195

2014-12-19 14:07:54amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg232932
2014-07-25 22:08:08gennadsetfiles: + 21862.patch
nosy: + gennad
messages: + msg224008

2014-06-25 13:13:21pitrousetmessages: + msg221546
2014-06-24 21:30:21rocksetfiles: + cProfile-add-new-option-module.patch

nosy: + rock
messages: + msg221502

keywords: + patch
2014-06-24 20:06:54pitroucreate