2

I'm trying to use the line_profiler library to profile some Python code. However, when I run the command:

kernprof -l -v bots\main.py

I get the following error:

Traceback (most recent call last):
  File "C:\Python34\Scripts\kernprof-script.py", line 9, in <module>
    load_entry_point('line-profiler==1.0', 'console_scripts', 'kernprof')()
  File "C:\Python34\lib\site-packages\kernprof.py", line 221, in main
    execfile(script_file, ns, ns)
  File "C:\Python34\lib\site-packages\kernprof.py", line 34, in execfile
    exec_(compile(f.read(), filename, 'exec'), globals, locals)
  File "bots\main.py", line 2, in <module>
    from bots.scheduler import *
ImportError: No module named 'bots'

Clearly the line_profiler isn't recognising the __init__.py file I have in the directory /bots (where all my scripts are). When I run main.py normally it works fine. How can I get line_profiler to acknowledge the __init__.py module and behave appropriately?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
BitOfABeginner
  • 122
  • 1
  • 10
  • 1
    Is `/bots` on your Python path? – jonrsharpe Feb 18 '15 at 15:30
  • It isn't, however if I run the same command whilst in the bots directory, i.e. kernprof -l -v main.py , I get the same error – BitOfABeginner Feb 18 '15 at 15:36
  • Yes, because it's not looking in the directory where the file is located. You need to make sure you can run `python` from *anywhere* and still successfully `from bots.scheduler import *`. – jonrsharpe Feb 18 '15 at 15:38
  • Ah thanks jon, so is the best solution just to add that directory to my python path or is there another solution I should be using? – BitOfABeginner Feb 18 '15 at 15:42
  • Or move it onto an existing path, yes. – jonrsharpe Feb 18 '15 at 15:43
  • So I've added the bots directory to my pythonpath env variable and restarted my terminal but I'm still getting the same error. What could I be doing wrong? – BitOfABeginner Feb 18 '15 at 15:54
  • What platform are you using? If you just type `from bots.scheduler import *` into a "fresh" interpreter, what happens? What does `sys.path` show you? – jonrsharpe Feb 18 '15 at 15:55
  • `from bots.scheduler import *` gives the same error on a fresh interpreter, oddly sys.path doesn't return the range of values from my PYTHONPATH system variable (it's similar, returning a number of python/anaconda dirs, but not the same), and doesn't return the bots directory which I have added to PYTHONPATH in the Windows tool: System Properties -> Environment Variables. How can I modify the sys.path list of dirs? – BitOfABeginner Feb 18 '15 at 16:15

0 Answers0