In the ideal case of reversible expansion and compression, the process takes place in a series of steps at equilibria where the external pressure is equal to internal pressure of the gas at any moment. Therefore, the gas law, which is the internal pressure, is applicable and the work is derived by integration.
In practical terms, work by compression or expansion is an irreversible (sudden) process where the gas pressure and external pressure don't equate instantaneously. Therefore, the gas law cannot be applied. Instead the difference between the initial and final ext pressure is used.