6

I have written a rather large module which is automatically compiled into a .pyc file when I import it.

When I want to test features of the module in the interpreter, e.g., class methods, I use the reload() function from the imp package.

The problem is that it reloads the .pyc file, not the .py file.

For example I try a function in the interpreter, figure out that it is not working properly, I would make changes to the .py file. However, if I reload the module in the interpreter, it reloads the .pyc file so that the changes are not reflected in the interpreter. I would have to quit the interpreter, start it again and use import to load the module (and create the .pyc file from the .py file). Or alternatively I would have to delete the .pyc file each time.

Is there any better way? E.g., to make reload() prefer .py files over .pyc files?

Here is an except from the interpreter session that shows that reload() loads the .pyc file.

>>> reload(pdb)
<module 'pdb' from 'pdb.pyc'>

EDIT: And even if I delete the .pyc file, another .pyc file will be created each time I use reload, so that I have to delete the .pyc file each time I use reload.

>>> reload(pdb)
<module 'pdb' from 'pdb.py'>
>>> reload(pdb)
<module 'pdb' from 'pdb.pyc'>
  • Delete the `.pyc` file? – kindall Jul 15 '13 at 20:54
  • Yes, I mentioned it in my post, however, sometimes I need/want to open another interactive shell with this module to try something. And then a new pyc file will be created. –  Jul 15 '13 at 20:56
  • possible duplicate of [How to avoid .pyc files?](http://stackoverflow.com/questions/154443/how-to-avoid-pyc-files) – dnozay Jul 15 '13 at 21:09

3 Answers3

3

yes. here are things you can use the -B command line option:

python -B

or use the PYTHONDONTWRITEBYTECODE environment option:

export PYTHONDONTWRITEBYTECODE=1

these make sure the .pyc files are not generated in the first place.

dnozay
  • 23,846
  • 6
  • 82
  • 104
0

if you're using ipython, you can can do a shell command by prefixing it with !

So you could do

>>> !rm some_file.pyc
>>> reload(some_file)

Alternatively, you could define a quick function in your current shell:

>>> import os
>>> def reload(module_name):
...     os.system('rm ' + module_name + '.pyc')
...     reload(module_name)
...

and just call it whenever you want to reload your module.

astrognocci
  • 1,057
  • 7
  • 16
  • I don't like iPython (it might be good for teaching, but not what I prefer for personal usage), I simply open python in a bash terminal. But thx. –  Jul 15 '13 at 21:03
  • @astrognocci, won't these two reload keywords clashed? I think it will call itself, instead of the system reload keyword. – Zen Nov 05 '15 at 01:34
  • and the second line in your function should be `'rm ' + module_name.__name__ + '.pyc'` – Zen Nov 05 '15 at 03:46
0

You don't need to remove obsoleted *.pyc, because reload(module) does this automatically.

For this purpose I usually use something like:

    import module
    def reinit():
        try:
            reload(module)
        except:
            import traceback
            traceback.print_exc()

        call_later(1, reinit)

    reinit()
Vladyslav Savchenko
  • 1,282
  • 13
  • 10