pythonpyc

How to tell python to run a .py file when both a .pyc and .py file are present?


I have a project that I have compiled into .pyc files for security, but I want to be able to drop a .py file into my project for debugging purposes as necessary. How can I tell python to use a .py file if both the .py and .pyc files are present in a folder?

i.e. I have:

main:
   |
   run.pyc
   /src/
      |
      dependency1.pyc
      dependency2.pyc
      dependency2.py
      dependency3.pyc

and when both dependency2.pyc and dependency2.py are in /src/ I want run.pyc to use dependency2.py.


Solution

  • With respect to debugging.

    From Python docs:

    Python will ignore all legacy pyc files when a source file exists next to it. In other words, if a foo.pyc file exists next to the foo.py file, the pyc file will be ignored in all cases

    In your case, run.py should be using dependency2.py and ignoring the associated pyc, which in turn should allow you to debug.

    If that's not happening, try to temporarily move the compiled file dependency2.pyc out of the directory.