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: Command line timeit.py sets sys.path badly
Type: Stage:
Components: Library (Lib) Versions: Python 2.3
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: gvanrossum, paul.moore, rhettinger
Priority: normal Keywords:

Created on 2003-05-09 15:36 by paul.moore, last changed 2022-04-10 16:08 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
timeit.diff paul.moore, 2003-05-19 16:00 Patch to timeit.py
Messages (6)
msg15945 - (view) Author: Paul Moore (paul.moore) * (Python committer) Date: 2003-05-09 15:36
Running timeit.py from the command line results in the 
directory containing timeit.py (ie, the standard Python 
library) being added to sys.path, and the current 
directory *not* being.

To test, create a file mymod.py in the current directory, 
containing

def test():
    "Stupid test function"
    L = []
    for i in range(100):
        L.append(i)

Now, execute

\Apps\Python\Lib\timeit.py -s "import 
mymod" "mymod.test()"

Result is

Traceback (most recent call last):
  File "\Apps\Python\Lib\timeit.py", line 245, in main
    x = t.timeit(number)
  File "\Apps\Python\Lib\timeit.py", line 159, in timeit
    return self.inner(it, self.timer)
  File "<timeit-src>", line 3, in inner
    import mymod
ImportError: No module named mymod

It's possible to work around this, either by setting 
PYTHONPATH in the environment to ".", or by 
adding "import sys; sys.path.insert(0, '.')" to the code in 
the -s option. But neither is a particularly attractive 
solution.
msg15946 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-05-16 19:03
Logged In: YES 
user_id=6380

Good point. Can you provide a patch?
msg15947 - (view) Author: Paul Moore (paul.moore) * (Python committer) Date: 2003-05-19 16:00
Logged In: YES 
user_id=113328

This patch does what I'd expect.
msg15948 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2003-05-19 21:01
Logged In: YES 
user_id=6380

I won't be offended if someone else checks this in and
closes this issue. :)
msg15949 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-05-19 23:52
Logged In: YES 
user_id=80475

I've got it from here.
msg15950 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-05-20 05:00
Logged In: YES 
user_id=80475

Applied as timeit.py 1.12.
Thanks for the report and patch.
History
Date User Action Args
2022-04-10 16:08:40adminsetgithub: 38476
2003-05-09 15:36:27paul.moorecreate