How to clean *.pyc files?

Jun 10 at 4:39 PM
Using the Menu -> Build -> Clean doesn't work and in the Solution Explorer -> Context Menu -> Python there is no relevant option. Is there a way to clean all *.pyc files from a project/solution/folder?
Coordinator
Jun 10 at 5:23 PM
We don't have something exactly like this, but there's the generic MSBuild-driven build process in 2.1 beta that lets you add any steps you want to Build and Clean. First of all, if your project is not a web application, you'll need to enable build in general as described here. Then, define your own Clean target in your .pyproj file. For example, for Python 2.x (which places .pyc files next to the corresponding .py), it would be something like:
  <Target Name="DeletePyc">
    <Delete Files="@(Compile->'%(RelativeDir)%(Filename).pyc') " />
  </Target>

  <PropertyGroup>
    <CleanDependsOn>$(CleanDependsOn);DeletePyc</CleanDependsOn>
  </PropertyGroup>
This will delete .pyc files corresponding to all .py files (strictly speaking, all <Compile> items) in your project, but won't touch any other (such as e.g. those inside virtual envs). If you just want to delete all .pyc files inside your project directory regardless of what produced them, do this instead:
  <ItemGroup>
    <PycFile Include="$(ProjectHome)\**\*.pyc" />
  </ItemGroup>

  <Target Name="DeletePyc">
    <Delete Files="@(PycFile)" />
  </Target>

  <PropertyGroup>
    <CleanDependsOn>$(CleanDependsOn);DeletePyc</CleanDependsOn>
  </PropertyGroup>
Coordinator
Jun 10 at 5:25 PM
Not yet, but that's a great idea. We could also use Build to precompile pyc files, though that isn't worth as much.

The problem is that I don't think we can enable the default Clean menu without also enabling Build and automatic build on F5, which makes starting your project significantly slower. If we can resolve that then it's probably worth doing.
Jun 10 at 6:06 PM
If this ends up being a feature, a build shouldn't be necessary (at least not for .pyc files, maybe for py2exe or PyInstaller, if you will ever want to implement that).

I think an option in the context menu a la PyDev and PyCharm would be great, especially for web projects.
Coordinator
Jun 10 at 6:30 PM
It shouldn't be, but if we want to use the VS commands (which we always do, as it makes things far more intuitive), then we don't really get a choice because VS will enforce it. It's an unfortunate position to be one of very few non-compiled languages in VS - a lot of design decisions don't make sense for us and we don't have the ability to change them.

The build command doesn't have to do anything, but some people want it to do things, which is why there's a way to enable it. However, once you've enabled it, VS is going to try and call it all the time, which can be annoying and slow when you really don't need it. It's possible that there's a way we can enable it and make it fast when it isn't necessary, but that may break those users who do want it to do something.

Simply adding another command is far from preferable, since as you showed with your initial post, you expected the standard command to work. If we add another command we have to maintain it, document it, explain it, and justify ourselves to the Visual Studio team when they complain that we're not using the proper menu.

Ultimately, it's a far more complicated design decision that it appears at first glance. If you're willing to have the command show up somewhere different, you can easily add it as an external tool ("Tools\External Tools", set command to cmd.exe, arguments to /C "del /s *.pyc" and initial directory to $(ProjectDir)), but if/when we do an "official" command for this, we want it to really be a first-class part of Visual Studio and not something that looks tacked onto the side.